Skip to contents

Adds a spanning header row above the existing column names to group related columns together.

Usage

tt_header_above(
  table,
  header,
  bold = TRUE,
  align = "center",
  color = NULL,
  fill = NULL,
  italic = NULL,
  font_size = NULL,
  rotate = NULL,
  inset = NULL,
  stroke = NULL,
  line = TRUE,
  gap = "10pt"
)

Arguments

table

A typst_table object.

header

Named vector specifying header groups. Names are the group labels, values are the number of columns each group spans. Use empty string "" for columns without a group header.

bold

Logical. Make header text bold (default TRUE).

align

Header alignment (default "center").

color

Text color.

fill

Fill color.

italic

Logical. Make header text italic.

font_size

Font size.

rotate

Rotation angle (e.g., "90deg", 90, "1.5rad").

inset

Cell padding (e.g., "10pt", "5pt 8pt").

stroke

Stroke (border) specification for the header cell(s). Can be TRUE for default 1pt black, a color, a Typst stroke spec like "2pt + blue", or a Typst dictionary like "(bottom: 1pt)".

line

Logical. Add horizontal line below the header (default TRUE).

gap

Width of visual gap between header groups (e.g., "10pt", "0.5em"). When specified, empty columns are inserted between groups. Default "10pt". Use NULL to disable gaps.

Value

The modified typst_table object.

Examples

# Group columns under headers
tt(mtcars[1:5, 1:6], rownames = FALSE) |>
  tt_header_above(c("Performance" = 3, "Design" = 3))
#> #table(
#>   columns: (1fr, 1fr, 1fr, 10pt, 1fr, 1fr, 1fr),
#>   align: (left, left, left, center, left, left, left),
#>   stroke: none,
#>   table.hline(stroke: 1pt),
#>   table.header(
#>     table.cell(colspan: 3, align: center, stroke: (bottom: 0.5pt))[*Performance*],
#>     [],
#>     table.cell(colspan: 3, align: center, stroke: (bottom: 0.5pt))[*Design*],
#>     [mpg],
#>     [cyl],
#>     [disp],
#>     [],
#>     [hp],
#>     [drat],
#>     [wt]
#>   ),
#>   table.hline(stroke: 0.5pt),
#>   [21], [6], [160], [], [110], [3.9], [2.62],
#>   [21], [6], [160], [], [110], [3.9], [2.875],
#>   [22.8], [4], [108], [], [93], [3.85], [2.32],
#>   [21.4], [6], [258], [], [110], [3.08], [3.215],
#>   [18.7], [8], [360], [], [175], [3.15], [3.44],
#>   table.hline(stroke: 1pt)
#> ) 

# Leave some columns ungrouped
tt(mtcars[1:5, 1:6], rownames = FALSE) |>
  tt_header_above(c(" " = 1, "Group A" = 2, "Group B" = 3))
#> #table(
#>   columns: (1fr, 10pt, 1fr, 1fr, 10pt, 1fr, 1fr, 1fr),
#>   align: (left, center, left, left, center, left, left, left),
#>   stroke: none,
#>   table.hline(stroke: 1pt),
#>   table.header(
#>     table.cell(align: center)[* *],
#>     [],
#>     table.cell(colspan: 2, align: center, stroke: (bottom: 0.5pt))[*Group A*],
#>     [],
#>     table.cell(colspan: 3, align: center, stroke: (bottom: 0.5pt))[*Group B*],
#>     [mpg],
#>     [],
#>     [cyl],
#>     [disp],
#>     [],
#>     [hp],
#>     [drat],
#>     [wt]
#>   ),
#>   table.hline(stroke: 0.5pt),
#>   [21], [], [6], [160], [], [110], [3.9], [2.62],
#>   [21], [], [6], [160], [], [110], [3.9], [2.875],
#>   [22.8], [], [4], [108], [], [93], [3.85], [2.32],
#>   [21.4], [], [6], [258], [], [110], [3.08], [3.215],
#>   [18.7], [], [8], [360], [], [175], [3.15], [3.44],
#>   table.hline(stroke: 1pt)
#> ) 

# Disable gaps between groups
tt(mtcars[1:5, 1:6], rownames = FALSE) |>
  tt_header_above(c(" " = 1, "Group A" = 2, "Group B" = 3), gap = NULL)
#> #table(
#>   columns: (1fr, 1fr, 1fr, 1fr, 1fr, 1fr),
#>   stroke: none,
#>   table.hline(stroke: 1pt),
#>   table.header(
#>     table.cell(align: center)[* *],
#>     table.cell(colspan: 2, align: center, stroke: (bottom: 0.5pt))[*Group A*],
#>     table.cell(colspan: 3, align: center, stroke: (bottom: 0.5pt))[*Group B*],
#>     [mpg],
#>     [cyl],
#>     [disp],
#>     [hp],
#>     [drat],
#>     [wt]
#>   ),
#>   table.hline(stroke: 0.5pt),
#>   [21], [6], [160], [110], [3.9], [2.62],
#>   [21], [6], [160], [110], [3.9], [2.875],
#>   [22.8], [4], [108], [93], [3.85], [2.32],
#>   [21.4], [6], [258], [110], [3.08], [3.215],
#>   [18.7], [8], [360], [175], [3.15], [3.44],
#>   table.hline(stroke: 1pt)
#> )