How do I prepend "Chapter" to heading names except for unnumbered headings?

Hello,
I am struggling with what I’d like to do: I’d like my level 2 headings to display “Chapter x” before the heading body. And I manage to do so.

But I’d like to be able not to display “Chapter x” on un unnumbered heading of the same level, I mean:

#heading(level: 2, numbering: none)[No number title]

For now, if I do so, I still have “Chapter x”, where x is the number of the previous numbered heading. Here is my current code:

 show heading.where(level: 2): set text(size: 24pt, weight: "bold")
  show heading.where(level: 2): set block(above: 0em, below: 0em)
  show heading.where(level: 2): it => {
    block(above: 2em, below: 1.2em)[
      #if heading.numbering != none [
        #block(above: 1.5em, below: 0.6em)[
          #set text(size: 14pt, weight: "bold")
          Chapter #counter(heading).display(
            (.., last) => last
          )
        ]
      ]
      #it.body
    ]
  }

Thank you very much.

Hello, your question reminds me of Adding text to *some* level 1 headings.

Could you share whether that is useful to you and what more you require for your specific use case?


For now, if I do so, I still have “Chapter x”, where x is the number of the previous numbered heading.

Since you’re using second-level headings, is x the number of the previous second-level numbered heading? For example:

= First-level

== Second-level unnumbered

== Second-level numbered

= First-level

== Second-level unnumbered

== Second-level numbered

What would this be?

Within first-levels?    Across first-levels?

Pt. 1                ||          Pt. 1
  -                  ||            -
  Chapter 1          ||            Chapter 1
                     ||
Pt. 2                ||          Pt. 2
  -                  ||            -
  Chapter 1       <─ or ─>         Chapter 2

Please also consider using typc for the snippet you added.

Hello, thank you very much for your link. Thanks to it, I spotted a mistake on my code:

#if heading.numbering != none

Should be:

#if it.numbering != none

And all is working as expected!

1 Like