How to repeat the caption (or a modified caption) of a multi-page table?

In LaTeX, I can write the following:

\documentclass{article}
\usepackage{longtable}
\usepackage[margin=1.5cm]{geometry}

\begin{document}
\vspace*{0.85\textheight}

\begin{longtable}{|c|c|c|}

% Caption and header for the first page
\caption{Caption for the first page} \label{longtable1} \\
\hline
Header 1 & Header 2 & Header 3 \\
\hline
\endfirsthead

% Caption and header for subsequent pages
\caption{Caption for subsequent pages} \\
\hline
Header 1 & Header 2 & Header 3 \\
\hline
\endhead

% Footer
\hline
\endfoot

% Table data
Row 1.1 & Row 1.2 & Row 1.3 \\
Row 2.1 & Row 2.2 & Row 2.3 \\
Row 3.1 & Row 3.2 & Row 3.3 \\
Row 4.1 & Row 4.2 & Row 4.3 \\
Row 5.1 & Row 5.2 & Row 5.3 \\
Row 6.1 & Row 6.2 & Row 6.3 \\
Row 7.1 & Row 7.2 & Row 7.3 \\
Row 8.1 & Row 8.2 & Row 8.3 \\
Row 9.1 & Row 9.2 & Row 9.3 \\
\end{longtable}

\end{document}

Which gives the following output:
image

How to achieve this in Typst? I know how to create a multi-page table, but now to repeat the caption. For example:

#v(90%)
#show figure.where(kind: table): set figure.caption(position: top)
#show figure: set block(breakable: true)
#figure(
  caption: [test],
  table(
    columns: 3,
    table.header(repeat: true)[h1][h2][h3][#table.hline()],
    [1], [2], [3],
    [4], [5], [6],
    [5], [8], [9],
    [a], [b], [c],
  )
)

Gives the following output:
image

My question is, how do modify the above code to get repeated caption in subsequent pages?

1 Like

I found a conversation about repeating footer (caption?) here (start). @PgBiel and I had a few ideas, but it looks like the goal was a bit different: get footer on all pages except last one.

Here is a slightly modified version:

#set page(height: 8em, width: 10em, fill: gray)

#let next-page-table(next-page-content: [], ..table-args) = context {
  let columns = table-args.named().at("columns", default: 1)
  let column-amount = if type(columns) == int {
    columns
  } else if type(columns) == array {
    columns.len()
  } else {
    1
  }

  // Counter of tables so we can create a unique table-part-counter for each table
  let table-counter = counter("table")
  table-counter.step()

  // Counter for the amount of pages in the table
  // It is increased by one for each footer repetition
  let table-part-counter = counter("table-part" + str(table-counter.get().first()))
  show <table-footer>: footer => {
    table-part-counter.step()
    context {
      // if table-part-counter.get() != table-part-counter.final() {
      if table-part-counter.get().first() > 1 {
        // Display the footer only if we aren't at the last page
        footer
      }
    }
  }

  table(
    ..table-args,
    table.footer(
      // The 'next page' content spans all columns and has no stroke
      // Must be selectable by the show rule above which hides it at the last page
      table.cell(colspan: column-amount, stroke: none, [#next-page-content <table-footer>])
    )
  )

  // Compensate for the empty footer at the last page of the table
  v(-measure(next-page-content).height)
}

first page
#pagebreak()

#next-page-table(
  next-page-content: [Next page!],
  columns: 3,
  ..range(3 * 9).map(i => str.from-unicode("a".to-unicode() + i))
)

#pagebreak()
last page

I’m not sure you can get anywhere close to a clean solution (I don’t even know how to correctly change the code to get at least some sort of solution). I also don’t think you can get something like figure.where(kind: table).supplement to recreate the table caption instead of using hard-codded caption-like footer text. And also, I haven’t found any open issue for that:

https://github.com/typst/typst/issues?q=is%3Aissue+caption+continue

https://github.com/typst/typst/issues?q=is%3Aissue+caption+repeat

I also sometimes ideally want a solution for that: “Table 1. …”, “Continuation of Table 1” (something like that). So you can create an issue for this.

I have opened an issue here: Add ability to repeat the caption (or a modified caption) of a multi-page table in subsequent pages · Issue #5057 · typst/typst · GitHub

2 Likes