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,
line_sep = "4pt"
)Arguments
- table
A
typst_tableobject.- 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
TRUEfor 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).
- line_sep
Stroke width of the white separator line between header groups (e.g.,
"4pt","2pt"). Creates a visual gap between groups by overlaying a white vertical line on the horizontal rule at group boundaries. Default"4pt". UseNULLto disable separation.
Examples
# Group columns under headers
tt(mtcars[1:5, 1:6]) |>
tt_header_above(c("Performance" = 3, "Design" = 3))
#> #table(
#> columns: (auto, auto, auto, auto, auto, auto),
#> stroke: none,
#> table.header(
#> table.hline(stroke: 1pt),
#> table.cell(colspan: 3, align: center)[*Performance*], table.cell(colspan: 3, align: center)[*Design*],
#> table.hline(stroke: 0.5pt),
#> table.vline(x: 3, start: 1, end: 2, stroke: 4pt + white),
#> table.cell(inset: (y: 0pt))[], table.cell(inset: (y: 0pt))[], table.cell(inset: (y: 0pt))[], table.cell(inset: (y: 0pt))[], table.cell(inset: (y: 0pt))[], table.cell(inset: (y: 0pt))[],
#> [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]) |>
tt_header_above(c(" " = 1, "Group A" = 2, "Group B" = 3))
#> #table(
#> columns: (auto, auto, auto, auto, auto, auto),
#> stroke: none,
#> table.header(
#> table.hline(stroke: 1pt),
#> table.cell(align: center)[* *], table.cell(colspan: 2, align: center)[*Group A*], table.cell(colspan: 3, align: center)[*Group B*],
#> table.hline(stroke: 0.5pt),
#> table.vline(x: 1, start: 1, end: 2, stroke: 4pt + white),
#> table.vline(x: 3, start: 1, end: 2, stroke: 4pt + white),
#> table.cell(inset: (y: 0pt))[], table.cell(inset: (y: 0pt))[], table.cell(inset: (y: 0pt))[], table.cell(inset: (y: 0pt))[], table.cell(inset: (y: 0pt))[], table.cell(inset: (y: 0pt))[],
#> [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 separator between groups
tt(mtcars[1:5, 1:6]) |>
tt_header_above(c(" " = 1, "Group A" = 2, "Group B" = 3), line_sep = NULL)
#> #table(
#> columns: (auto, auto, auto, auto, auto, auto),
#> stroke: none,
#> table.header(
#> table.hline(stroke: 1pt),
#> table.cell(align: center)[* *], table.cell(colspan: 2, align: center)[*Group A*], table.cell(colspan: 3, align: center)[*Group B*],
#> table.hline(stroke: 0.5pt),
#> [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)
#> )