How to determine if the current page has a heading of level 1?

Hello everyone,

For a personal project, i want to set a rectangle on the side of a pages in a book based on the section the current page is and based on the oddity of the current page. My code look a little bit like this :

#set page(
  paper: "a6",
  margin: CONFIG.MARGIN,
  numbering: "1",
  number-align: left,
  background: context{
    let current-page = here().page() // Get number of page
    // get chapter (or section) based on number of heading
    let section = query(selector(heading.where(level : 1)).before(here())).len()

    // place rectangle based on the page number & book section
    let r-height = current-layout.height / total-section
    let offset = section* r-height
    let r = rect(fill: black,width: 0.3cm, height: r-height)
    let side = right
    if calc.odd(current-page){side = right} else {side = left}
    place(side, r, dy: offset)
  },
  header: [...]
)

I basically want to prevent the placing of this rectangle on pages where there’s a heading of level 1 and wanted to know if it was possible. Maybe something like :

#let l1-heading = query(selector(heading.where(level: 1)).on-page(current-page)).len()
#if l1-heading > 0 {place(side, r, dy: offset)}

Thank you in advance for your help

You can query all headings and go through them one by one, checking if its location is on the current page:

let has-heading = query(heading.where(level: 1))
  .any(it => it.location().page() == current-page)
2 Likes

Hi @Alex, trying to point you in the right direction, would this match what you are looking for? This seems pretty similar.

1 Like