typst表格自动分页

#show figure.where(kind: table): set figure.caption(position: top)

#let table-multi-page(
  continue-header-label: [],
  continue-footer-label: [],
  ..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
  }

  // Check as show rule for appearance of a header or a footer in grid if value is specified
  let label-has-content = value => (
    value.has("children") and value.children.len() > 0 or value.has("text")
  )

  // 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
  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()
        and label-has-content(continue-footer-label)
    ) {
      footer
    }
  }

  show <table-header>: header => {
    table-part-counter.step()
    context if (
      (table-part-counter.get().first() != 1)
        and label-has-content(continue-header-label)
    ) {
      header
    }
  }

  grid(
    inset: 0mm,
    row-gutter: 2mm,
    fill: color.hsl(
      92.73deg,
      44%,
      65.69%,
      16.2%,
    ), // Notice, that grid body spreads all the way from top to bottom, so there can be a gap for footer label
    grid.header(grid.cell(align(left + bottom)[
      #continue-header-label <table-header>
    ])),
    ..table-args,
    grid.footer(grid.cell(align(
      right + top,
    )[#continue-footer-label <table-footer> ]))
  )
}

#let tbl = table(
  columns: (2cm, 3cm, 4cm, 1fr),
  row-gutter: (0.6mm, auto),
  inset: (top: 2mm, bottom: 2mm),
  table.header([cell1], [cell2], [cell3], [cell4]),
  ..for value in range(0, 150) {
    ([cell value], [#(150 - value)])
  },
)

表格说明 @ttt2[]
#[
  #show figure: set block(breakable: true)
  #figure(
    kind: table,
    caption: [当我们在处理Excel工作表或Word文档中的表格时,经常会遇到表格名称过长的问题。"某个非常长的表格名称,不应该放在一行文本中 文本 文本 文本"这样的命名方式确实存在显示和管理上的困难。],
    table-multi-page(
      continue-header-label: [接上表 @ttt2[]],
      continue-footer-label: [见下表 @ttt2[]],
      tbl,
    ),
  )
  <ttt2>
]

posted @ 2025-05-27 11:38  卓能文  阅读(77)  评论(0)    收藏  举报