Applies formatting to one or more columns. Supports both static values and data-driven formatting where styling parameters reference other columns.
Usage
tt_column(
table,
column,
width = NULL,
align = NULL,
bold = NULL,
italic = NULL,
color = NULL,
fill = NULL,
border_left = NULL,
border_right = NULL,
font_size = NULL,
rotate = NULL,
inset = NULL,
stroke = NULL,
.missing = c("warn", "ignore", "error")
)Arguments
- table
A
typst_tableobject.- column
<
tidy-select> Column(s) to style.- width
Column width (e.g.,
"100pt","2fr"). Overrides width set intt().- align
Column alignment:
"left","center","right".- bold
Logical, column name, or pattern string. If logical, applies to all rows. If column name (unquoted), uses that column's values. If pattern containing
{col}(e.g.,"bold_{col}"), expands to column name per styled column.- italic
Logical, column name, or pattern string. Same behavior as
bold.- color
Text color. Can be a color value, column name, or pattern string.
- fill
Fill color. Can be a color value, column name, or pattern string.
- border_left
Left border specification (e.g.,
TRUE,"1pt + gray").- border_right
Right border specification.
- font_size
Font size (e.g.,
"10pt","0.9em"). Can be a column name or pattern string.- rotate
Rotation angle (e.g.,
"90deg",90,"1.5rad"). Can be a column name or pattern string.- inset
Cell padding (e.g.,
"10pt","5pt 8pt"). Can be a column name or pattern string.- stroke
Stroke (border) specification for the cell(s). Can be
TRUEfor default 1pt black, a color, a Typst stroke spec like"2pt + blue", or a Typst dictionary like"(bottom: 1pt)". Can also be a column name or pattern string.- .missing
How to handle missing pattern columns:
"warn"(default) emits a warning,"ignore"silently skips,"error"stops execution.
Details
Data-driven formatting
Style parameters can reference columns in the original data for per-row values:
data |>
mutate(bg_color = ifelse(value > 100, "red", "white")) |>
tt(cols = c(name, value)) |>
tt_column(value, fill = bg_color)The bg_color column is hidden from display but used for styling.
Pattern-based styling
When styling multiple columns with a naming convention, use {col} patterns
to automatically expand column references:
# Instead of repetitive calls:
data |>
tt(cols = c(mpg, qsec)) |>
tt_column(everything(), color = "color_{col}", fill = "bg_{col}")For column mpg, this looks for color_mpg and bg_mpg in the data.
Examples
# Right-align and bold a numeric column
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_column(mpg, align = "right", bold = TRUE)
#> #table(
#> columns: (1fr, 1fr, 1fr),
#> align: (right, left, left),
#> stroke: none,
#> table.hline(stroke: 1pt),
#> [*mpg*], [cyl], [disp],
#> table.hline(stroke: 0.5pt),
#> [*21*], [6], [160],
#> [*21*], [6], [160],
#> [*22.8*], [4], [108],
#> [*21.4*], [6], [258],
#> [*18.7*], [8], [360],
#> table.hline(stroke: 1pt)
#> )
# Style multiple columns at once
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_column(c(mpg, disp), align = "right", color = "blue")
#> #table(
#> columns: (1fr, 1fr, 1fr),
#> align: (right, left, right),
#> stroke: none,
#> table.hline(stroke: 1pt),
#> [#text(fill: blue)[mpg]], [cyl], [#text(fill: blue)[disp]],
#> table.hline(stroke: 0.5pt),
#> [#text(fill: blue)[21]], [6], [#text(fill: blue)[160]],
#> [#text(fill: blue)[21]], [6], [#text(fill: blue)[160]],
#> [#text(fill: blue)[22.8]], [4], [#text(fill: blue)[108]],
#> [#text(fill: blue)[21.4]], [6], [#text(fill: blue)[258]],
#> [#text(fill: blue)[18.7]], [8], [#text(fill: blue)[360]],
#> table.hline(stroke: 1pt)
#> )
# Pattern-based styling (column-specific style columns)
df <- data.frame(
a = 1:3, b = 4:6,
color_a = c("red", "green", "blue"),
color_b = c("black", "gray", "white")
)
tt(df, cols = c(a, b), rownames = FALSE) |>
tt_column(c(a, b), color = "color_{col}")
#> #table(
#> columns: (1fr, 1fr),
#> stroke: none,
#> table.hline(stroke: 1pt),
#> [a], [b],
#> table.hline(stroke: 0.5pt),
#> [#text(fill: red)[1]], [#text(fill: black)[4]],
#> [#text(fill: green)[2]], [#text(fill: gray)[5]],
#> [#text(fill: blue)[3]], [#text(fill: white)[6]],
#> table.hline(stroke: 1pt)
#> )