Why does counter(heading).display() get the wrong heading number after a pagebreak?

I have #show heading.where(level: 1): it => { pagebreak(weak: true); it } and the page is configured to display the current header:

set page(
    margin: 3cm,
    header: context {
      counter(heading).display()
    },
    footer: context {
      here().page()
    }
  )

However, I get the previous heading number even though it is not on the page (as seen in the image). How can I make it show the correct heading?

Hey there, welcome to the forum!

That’s the correct behavior, the heading counter at that location (the header) has not yet advanced, it advances at the next heading (below the header), this means it will display the last heading’s number.

If you wish to show the number of the heading on this page you need to provide the location for this heading to the counter through the use of query/locate.

You may be interested in hydra – Typst Universe (full disclosure, I’m the author), which would take care of the logic for selecting the correct heading and displaying it.

In your case that would be:

#set page(header: context hydra(use-last: true, skip-starting: false))

If you prefer a more manual approach you can take a peek at the source code and write it yourself, but it boils down to query(heading) and filtering out candidates that don’t fit (pages after the current one etc.).

PS: Something I’ve also noticed is that you’re using here().page() in the footer, this will give you the actual page number, not the logical page number of the page counter. You probably meant to use counter(page).display()

2 Likes

Hi @wulfheart, thanks for your question! If you feel the response you got has sufficiently answered your question, be sure to give it a checkmark. This will help others find the solution in the future. Thanks!

@Tinger thanks for your reply. I started using hydra because it was the easiest way. However, I would have thought that it would be simpler with typst. Thanks nevertheless!