How to decorrelate heading numberings?

Hello everyone,

Getting back to typst after a while off. I may be rusty and missing something absolutely obvious.

I’m typesetting a book with numbered parts and numbered chapters, but both are separate sequences (i.e. the first chapter of Part 2 is chapter 6, not 2.1).

However, as of now, the first level displays fine, but the first chapter is displayed as “Chapter 11” or “Chapter 1.1.” depending on where I’m messing with. And I would like to have it displayed as “Chapter 1” :

Currently, I have these show rules for the outline:

#show outline.entry.where(level: 1): it => link(
  //text(font:"Josefin Sans")[],
  it.element.location(),
  it.indented(
    if it.element.numbering != none {
    "Partie " + numbering("1.", ..counter(heading).at(it.element.location()))
    } else {it.prefix()},
    it.body()
  ),
)

#show outline.entry.where(level: 2): it => link(
  it.element.location(),
  it.indented(
    if it.element.numbering != none {
    "Chapitre " + numbering("1.", ..counter(heading).at(it.element.location()))
    } else {it.prefix()},
    it.inner()
  ),
)

And for the text:

#show heading.where(level: 1): it => {
  pagebreak(weak: true)
  set align(right)
  set par(justify: false)
  v(20%)
  block(below:2em)[
    #text(font:"Josefin Sans", size: 18pt, weight: "regular", {
      if heading.numbering != none [
        Partie #counter(heading).display()]}) 
        #v(-.5em)
        #line(length: 100%, stroke:1pt)
        #v(.5em)
        #text(font:"Josefin Sans",size: 24pt, weight: "regular",)[#it.body]
    ]
}

#show heading.where(level: 2): it => {
  pagebreak(weak: true)
  set align(center)
  v(20%)
  block(below:2em)[
    #text(size: 12pt, weight: "regular", {
      if heading.numbering != none [
        _Chapitre #counter(heading).display()_ \ ]})
    #text(size: 18pt, weight: "regular",)[#emph[#it.body]]
    ]
}

The “Partie” and “Chapitre” parts are handled in the rules for the sake of the layout.

How do I ensure the two numbering sequences stay decorrelated, both in the text and in the outline (and in the header, but I think fixing the former two will solve that) ? Where did I mess up ?

Thanks in advance for your replies!

EDIT: Forgot to mention. Maybe numbly could help with my case, but is it still compatible ? I see it has not been update for 2 years.

Perhaps it’s easier to define a custom function for parts, and use = heading only for chapters.

Here’s an example adapted from Typst Chinese FAQ:

#let part-counter = counter("topic")
#set heading(numbering: "Chapter 1")
#let part(body) = {
  part-counter.step()
  context heading(part-counter.display("Part 1") + [ ] + body, numbering: none)
}

#outline()
= Chapter
#part[Part]
= Chapter
= Chapter
#part[Part]
= Chapter

As for numbly, yes it’s still working. It provides nothing but a really simple function, so it doesn’t require any update after these years.

Hello there,

First, thank you for your reply !

So I’ve tried to adjust your code to my needs and I am failing doing so.

I can almost display the Part correctly in the text using :

#let part-counter = counter("topic")
#let part(body) = {
  set align(right)
  set par(justify: false)
  v(20%)
  part-counter.step()
  block[
    #text(font:"Josefin Sans", size: 18pt, weight: "bold")[
    Partie #part-counter] //obviously wrong
    #v(-.5em)
    #line(length: 100%, stroke:1.5pt)
    #v(.5em)
    #text(font:"Josefin Sans",size: 24pt, weight: "bold")[#body]
    ]
}

I still have a problem: the #part-counter stuff is obviously broken (image 1). I’ve tried using Partie #context heading(part-counter.display()) at the beginning of the block but the result is messed up (image 2). I think it displays the heading counter as a level 1, hence the Chapitre 1 \ 1…

What is more, due to not using context (I suppose ?), the Part is not displayed in the outline.

What am I missing ?

In line 9, you don’t want to insert the counter object but use its value, like `#part-counter.get().at(0)`.
And this reference wants being wrapped in `context`.

Thank you very much for your reply !

Now the part title displays perfectly. Last thing is to have the part displayed in the outline.

Do I have to insert some kind of heading in the context rule ? When trying to use heading, it was displayed in the outline (although buggy because I’m bad), but with the same font as my chapter, while I’m trying to have something visually looking like:

How can I look for the Parts in the show rule ?

#show outline.entry.where(level: part-counter) doesn’t really work… It’s not even a level right ?

By default, #outline() includes only the heading elements. There are two methods to include part as well:

  1. Let part create a heading.

    My previous reply uses this method. However, if we want different outline styles for parts and chapters, then this method is less convenient.

  2. Let part create a figure of kind: "part", and select both headings and parts when creating the outline:

    #outline(
      target: figure.where(kind: "part").or(heading),
    )
    

    (Relevant docs: outline.target, figure.kind, selector.or)

Full code
// Implement the parts.
// The `part` function is the public interface.
// It creates a figure of kind "part" (`_part`),
// and the real implementation goes in the show rule.
#let _part = figure.where(kind: "part")
#show _part: set figure(supplement: [Partie], numbering: "1")
#show _part: it => {
  // Here's the real implmentation.
  set align(right)
  set par(justify: false)
  set text(font: "Josefin Sans", weight: "bold")
  v(20%)
  context text(18pt, counter(_part).display())
  v(-0.5em)
  line(length: 100%, stroke: 1.5pt)
  v(0.5em)
  text(24pt, it.body)
}
#let part(body) = figure(body, caption: body, kind: "part")

// Create the outline.
#set outline(indent: 2em)
#show outline.entry: it => {
  let el = it.element
  if el.func() == figure and el.kind == "part" {
    // For parts:
    set text(weight: "bold")
    it
  } else if el.func() == heading {
    if el.level == 1 {
      // For chapters:
      link(el.location(), it.indented(
        if el.numbering != none {
          h(2em) // Increase the indent for chapters
          el.supplement
          [~]
          counter(heading).display(at: el.location())
        } else {
          it.prefix()
        },
        it.inner(),
      ))
    } else {
      // For sections:
      link(el.location(), it.indented(
        {
          hide[#el.supplement~] // Increase the indent for sections
          counter(heading).display(at: el.location())
        },
        it.inner(),
      ))
    }
  } else {
    it
  }
}

// Example usage:

#set text(lang: "fr")

#outline(target: _part.or(heading))

= Intro
A basic test for cross references:
@part:1685-1789, @sec:1685.

#set heading(numbering: "1.1")
#part[1685--1789] <part:1685-1789>
= 1685 <sec:1685>
== Section
= 1789

#part[The second part]
= Chapter

(For simplicity, I didn’t implement all the styles, but I guess that’s sufficient for you to achieve other styles on your own? Please reply if you need further help.)

A while back @Andrew shared a solution for this, their solution uses the heading markup and doesn’t need a custom function.

Sorry for the delayed reply, tough end of week. Thank you both very much for your help !

So I’ve tried both solution, with or without function, and the one with a function suits my needs more (I can have the same global template whether or note there are parts in the book).

Everything is almost set, I have only one tiny question left for one buggy stuff in the outline, plus two technical questions note quite related but I just stumbled upon it.

  • [Could be a bug, but workaround found by Y.D.X below] First, about the outline (in my case a full table of content at the end of the book), everything looks great except for one level 2 title :

The level 2 title in the Introduction has a “0” attached to it, which I do not want.

The ToC code is:
#heading(level: 1, outlined: false, bookmarked:true)[Table des matières]
#show outline.entry: it => {
  let el = it.element
  if el.func() == figure and el.kind == "part" {
    // For parts:
    set text(font:"Josefin Sans", weight: "bold")
    it.prefix()
    text(font:"Josefin Sans", weight: "bold")[. ]
    it.body()
    text(font:"Josefin Sans", weight: "bold")[\ ]
    } else if el.func() == heading {
    if el.level == 1 {
      // For chapters:
      link(el.location(), it.indented(
        if el.numbering != none {
          "Chapitre"
          [~]
          counter(heading).display(at: el.location())
          text()[.]
        } else {
          it.prefix()
        },
        it.inner(),
      ))
    } else {
      // For sections:
      link(el.location(), it.indented(
        {
          counter(heading).display(at: el.location())
        },
        it.inner(),
      ))
    }
  } else {
    it
  }
}

My guess is that I miss an else case somewhere at the end, for un-numbered headings (since the whole introduction is, and probably the conclusion when I get to it), but my brain is kinda fried and this whole document structuration is at level way above mine. I’m just afraid to touch anything haha I’ll need time to assimilate everything and be able to reproduce by myself :sweat_smile:

  • [Again, Y.D.X the goat fixed that here] Second question, still about the outline. The level 4 headings are displayed in italics (that’s by design) but i’d prefer having only the body and not the page numbers displayed in italics.
Current code is
#show outline.entry.where(level: 4): it => link(
  it.element.location(),
  it.indented(it.prefix(),emph[#it.inner()])
)

I suppose I have to change #it.inner with #it.body and use it.page somewhere. I tried with it.indented(it.prefix(),emph[#it.body()], it.page()) but no luck. What am I missing ?

  • [Solved by Chaddai_Fouche below] Last question, though I already have a workaround for this. After the last chapter and before the table of content, I stop to display footer and header.

Using this works perfectly

#set page(
  footer: context {},
  header: context {}
)

But in case I’d have to use it several times, I wanted to create a no-deco function.

#let no-deco={
  set page(
    footer: context {},
    header: context {}
  )
}

But this doesn’t work. If I use #no-deco, nothing happens and both header and footer stay. So my question is simple. Why ?

Thanks a lot in advance for your replies. We’re almost done I swear :grinning_face_with_smiling_eyes:

set rules and show rules are scoped, so your no-deco set page only apply within the block of no-deco (so it apply to nothing since there’s no call to page in there).

If you want to encapsulate set and show rules to apply to the rest of a document from a certain point, the idiom is to create a function that takes the content to apply them to then use a show: your-function like the following:

#let no-deco(body) = {
  set page(
    footer: context {},
    header: context {}
  )

  body
}

...

// then later
#show: no-deco

// here your set page apply

This works because show: function basically apply the function to the rest of the document.

By the way, you really don’t need context here, context only role is to make some information available within its block that are a bit too costly to make available to all code (+ some considerations about the timing of evaluation). context {}is just [], empty content.

One question down, it works perfectly and I’ll know better from now on. Thanks a lot !

Hmm, this looks like a bug of Typst… I’ll inspect it later.

For now, you can skip the number for unnumbered sections by changing the relevant code in #show outline.entry: it => { … } as follows.

      // For sections:
      link(el.location(), it.indented(
        // 👇 This `if … != (0,)` is new.
        if counter(heading).at(el.location()) != (0,) {
          counter(heading).display(at: el.location())
        },
        it.inner(),
      ))

You are in the right direction. This should work:

#show outline.entry.where(level: 4): it => link(
  it.element.location(),
  it.indented(
    it.prefix(),
    [_#it.body()_ #box(width: 1fr, outline.entry.fill) #it.page()],
  ),
)

Perhaps someone will improve the documentation about this. Before that, we can let x = it.inner() and hover x to view the default implementation.

Thank you very much for your help throughout the topic, everything now works perfectly !

All the better if I discovered a bug, if it helps haha