Page X of Y as part of a table header?

Suppose I have a table (potentially big enough to repeat on multiple pages) and I want to customize the table header on subsequent pages (or maybe the caption within a figure). For example in

#set page(height: 10em)
Table spread over several pages 
#table(
  fill: (_, row) => if ((row == 0) or (row == 1)) { silver } else { white },
  columns: 2,
  table.header(
    repeat: true,
    table.cell(colspan: 2)[#align(center)[Exp X]],
    [Fruits], [Vegetables]),
  [Apple], [Tomato],
  [Oranges],[Potato],
  [Peach],[Brussel Sprouts],
  [Strawberry], [Savoy],
  [Banana],[Arugula]
)

(see also (Typst))
I would like to have the header read “Exp X” if it’s just one page, but “Exp X (Page 1 of 2)” and “Exp X (Page 2 of 2)” if it’s spread out over two pages. How would I do that?

Using a counter will work well, like this. You’ll need a separate counter per table, giving them each their own name is the simplest way. You can then improve this idea by making it conditional - if the final count is 1, don’t show the page numbers.

#set page(height: 10em)
Table spread over several pages 
#let tcounter = counter("my-table")
#table(
  fill: (_, row) => if ((row == 0) or (row == 1)) { silver } else { white },
  columns: 2,
  table.header(
    repeat: true,
    table.cell(colspan: 2, {
      tcounter.step()
      context {
        let current = tcounter.get().first()
        let final = tcounter.final().first()
        align(center)[Exp X (Page #current of #final)]
      }
    }),
    [Fruits], [Vegetables]),
  [Apple], [Tomato],
  [Oranges],[Potato],
  [Peach],[Brussel Sprouts],
  [Strawberry], [Savoy],
  [Banana],[Arugula]
)

1 Like

Thanks, this seems to work perfectly! And if “current” is “final” it means, we aren’t spread of more than one page. In that case, I would just display “Exp X”.

...
let ppart = {
  if current == final and current == 1 {
    []
  } else {
    [(Page #current of #final)]
  }
}
align(center)[Exp X #ppart]
...