How to Detect a Pagebreak?

I’m currently working on a slide template and I’m a bit stuck when it comes to the handling of the headings.

At each new page I would like to display the current heading, so that I don’t have to manually place it and let Typst do the layout. So far I haven’t found a way to detect a pagebreak though.

Is there a way at all?

Use page.header with hydra – Typst Universe?

For detecting long pagebreaks there is a hack: Skip headers and footers for skipped pages · Issue #2722 · typst/typst · GitHub. I don’t know how you would detect a regular pagebreak, let alone an implicit one. You only have here().page() or counter(page).get().

There are no callbacks that tell you there is a new page found, and you can add to it. You can manually call, and it will add 1 heading max because of on-demand checking.

I’d go with header or background.

There is one element that natively supports doing something special when being pagebroken by an (implicit) new page: tables (and grids), which use this to repeat the table header and/or footer. See more about that here: Allow adding content / configuring breakpoints of broken blocks · Issue #735 · typst/typst · GitHub

You can look into if it can be used, this can be used for “blocks that know when they are placed on more than one page”. It could however be quite limiting to force the user’s content to be placed inside a grid: for example, then regular pagebreak() is suddenly disallowed.