Adding text to *some* level 1 headings

Hello everyone,

This is a followup on a reddit post I did previously, which answered a few of my questions but added two others.

I am experimenting styling for books layout, and I am trying to add a

Chapter [number]
#linebreak()

before numbered chapters of a book, but not every single one (e.g. the Table of Content, Foreword, etc.)

As of now I have this function :

#show heading.where(level: 1): it => {
  let num = counter(heading).display("1")
  pagebreak(weak: true)
  v(20%)
  align(center)[
  #block[
    #text(
      size: 32pt,
      weight: "regular",
    )[#emph[
    Chapter #num
    #linebreak()
    #it.body]]
    
    #v(1.5em)
    ]]

But the title of the table of content and the foreword get added a “Chapter” before them which just doesn’t work.
Is there a way to segregate the level 1 headings that are numbered chapters from the other ones ?

Thanks in advance !

PS : Also, not the same but related question, for my ToC I use #set heading(numbering: "Chapter 1.", supplement: [Chapter]) but the outline only displays “Cha.”, “Chb.”, “Chc.” and such. Then again, is there a way to fix this and display the long text, and also separate numbered chapters from un-numbered ones ?

The styling you’re going for here specifically would be a duplicate of How to display "Chapter X" above level 1 heading name?.

Since the excluded headings all appear at the start, you could simply have the show rule below them in the code:

= Foreword
...

// The `show` rule goes here

= Lorem
...

Alternatively, don’t change the show rule’s positions in the code, but instead add #set heading(numbering: ...) where necessary, especially for more alterations:

#show heading.where(level: 1).and(heading.where(numbering: "1")): set text(red)

= Black

#set heading(numbering: "1")

= Red

#set heading(numbering: none)

= Black

Notice the added and.


Otherwise, I believe you would need to distinguish the headings through labels.


That’s how the numbering is detected. You would need to pass a function, for example Package subpar0.2.2 - #2 by hpcfzl.

1 Like