How can I format the last row of a table?

Taking @LordBaryhobal’s solution one step further so that it handles cells that span columns and rows:

#let my-table(cells, ..args) = {
  let columns = args.named().at("columns", default: 1)
  let n-cols = if type(columns) == int {
    columns
  } else {
    columns.len()
  }
  let n-cells = cells.fold(
    0,
    (curVal, cell) => curVal + cell.at("colspan", default: 1) * cell.at("rowspan", default: 1)
  )
  let n-rows = calc.ceil(n-cells / n-cols)
  table(
    columns: columns,
    ..args,
    fill: (_, y) => if y == n-rows - 1 {
      gray.lighten(50%)
    } else {
      none
    },
    ..cells
  )
}

Then it can be used as before:

#my-table(
  (
    table.cell(colspan: 2, [A]), table.cell(rowspan: 2, [B]),
    [C], [D],
    table.cell(colspan: 3, [E]),
    [F]
  ),
  columns: 3
)

image

The change was to not trust the length of the cells array, but to use fold to process each one, checking for colspan and rowspan, getting the total number of spaces taken up by the given cells.

4 Likes