How to change background and text collor if there is heading on the page

I want to use a special page background if there is a level 1 heading. Additionally, I need to change the text fill color on the whole page. Does anyone know how to do it? I managed to find this for the background, but I can’t modify it to work for the text color.

#set page(
  background: context if  query(heading.where(level: 1).after(here())).map(h => h.location().page()).at(0, default: 0) == here().page() [
      #box(fill: t-blue, image("pattern.png", width: 50cm))
    ] 
)

If you want the text-color to reset automatically on the next implicit page break, I don’t think this is currently possible. (There are only very limited ways to react to automatic page-breaks at the moment, and no way to insert a set rule “at the beginning of the next page”. See Add an API to React to Page Breaks · Issue #6445 · typst/typst · GitHub) [1]

If you are fine with manually determining where the text of that page ends (for example, if the chapter-heading-page only has the heading and a fixed-size abstract and the actual text starts on the next page anyway), you could create a function which creates such a page for you:

#let chapter(title, abstract) = {
  page(
    background: box(width: 100%, height: 100%, fill: blue),
    {
      set text(fill: luma(90%))
      heading(level: 1, title)
      v(2cm)
      pad(left: 4cm, abstract)
    }
  )
}

#lorem(20)

#chapter[Foo][
  This will show on the chapter-heading-page
]

This is on the next page.

  1. It might actually be possible to use meander – Typst Universe to do this, as this package allows for more control over how text flows and breaks across containers and pages, but this would require wrapping all chapters entirely into a meander.reflow, which probably has quite the performance limitations and might break other functionality ↩︎

1 Like