Skip to contents

Creates a typst_table object from a data.frame or tibble that can be rendered to Typst markup. Use pipe-friendly styling functions like tt_style(), tt_column(), tt_row(), and tt_cell() to customize the table appearance.

Usage

tt(
  data,
  cols = tidyselect::everything(),
  col_names = NULL,
  col_widths = "auto",
  align = NULL,
  preamble = NULL,
  escape = TRUE,
  rownames = TRUE
)

Arguments

data

A data.frame or tibble to convert to a table.

cols

<tidy-select> Columns to include in the displayed table. Hidden columns remain available for data-driven formatting. Defaults to all columns.

col_names

Optional character vector of display names for columns. Must match the number of selected columns. If NULL, uses column names from data.

col_widths

Column width specification: "auto" (default) creates equal-width columns that fill the container. Use tt_widths() for custom proportions.

align

Column alignment: single value applied to all columns, or vector of alignments. Valid values: "left"/"l", "center"/"c", "right"/"r".

preamble

Optional character string of raw Typst code to insert before the table. Useful for #set rules, #let bindings, or other Typst directives that should apply to the table (e.g., '#set text(font: "Arial")').

escape

Logical. If TRUE (default), escapes Typst special characters.

rownames

Logical. TRUE (default) includes row names as the first column with an empty header, FALSE excludes them.

Value

A typst_table object that can be further styled and rendered.

Examples

# Basic table (includes row names by default)
tt(mtcars[1:5, 1:3])
#> #table(
#>   columns: (1fr, 1fr, 1fr, 1fr),
#>   stroke: none,
#>   table.hline(stroke: 1pt),
#>   [], [mpg], [cyl], [disp],
#>   table.hline(stroke: 0.5pt),
#>   [Mazda RX4], [21], [6], [160],
#>   [Mazda RX4 Wag], [21], [6], [160],
#>   [Datsun 710], [22.8], [4], [108],
#>   [Hornet 4 Drive], [21.4], [6], [258],
#>   [Hornet Sportabout], [18.7], [8], [360],
#>   table.hline(stroke: 1pt)
#> ) 

# Select specific columns (excludes row names)
tt(mtcars, cols = c(mpg, cyl, hp), rownames = FALSE)
#> #table(
#>   columns: (1fr, 1fr, 1fr),
#>   stroke: none,
#>   table.hline(stroke: 1pt),
#>   [mpg], [cyl], [hp],
#>   table.hline(stroke: 0.5pt),
#>   [21], [6], [110],
#>   [21], [6], [110],
#>   [22.8], [4], [93],
#>   [21.4], [6], [110],
#>   [18.7], [8], [175],
#>   [18.1], [6], [105],
#>   [14.3], [8], [245],
#>   [24.4], [4], [62],
#>   [22.8], [4], [95],
#>   [19.2], [6], [123],
#>   [17.8], [6], [123],
#>   [16.4], [8], [180],
#>   [17.3], [8], [180],
#>   [15.2], [8], [180],
#>   [10.4], [8], [205],
#>   [10.4], [8], [215],
#>   [14.7], [8], [230],
#>   [32.4], [4], [66],
#>   [30.4], [4], [52],
#>   [33.9], [4], [65],
#>   [21.5], [4], [97],
#>   [15.5], [8], [150],
#>   [15.2], [8], [150],
#>   [13.3], [8], [245],
#>   [19.2], [8], [175],
#>   [27.3], [4], [66],
#>   [26], [4], [91],
#>   [30.4], [4], [113],
#>   [15.8], [8], [264],
#>   [19.7], [6], [175],
#>   [15], [8], [335],
#>   [21.4], [4], [109],
#>   table.hline(stroke: 1pt)
#> ) 

# Custom column names
tt(mtcars[1:5, 1:3], col_names = c("Miles/Gallon", "Cylinders", "Horsepower"), rownames = FALSE)
#> #table(
#>   columns: (1fr, 1fr, 1fr),
#>   stroke: none,
#>   table.hline(stroke: 1pt),
#>   [Miles/Gallon], [Cylinders], [Horsepower],
#>   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)
#> ) 

# Right-align numeric columns
tt(mtcars[1:5, 1:3], align = "right")
#> #table(
#>   columns: (1fr, 1fr, 1fr, 1fr),
#>   align: right,
#>   stroke: none,
#>   table.hline(stroke: 1pt),
#>   [], [mpg], [cyl], [disp],
#>   table.hline(stroke: 0.5pt),
#>   [Mazda RX4], [21], [6], [160],
#>   [Mazda RX4 Wag], [21], [6], [160],
#>   [Datsun 710], [22.8], [4], [108],
#>   [Hornet 4 Drive], [21.4], [6], [258],
#>   [Hornet Sportabout], [18.7], [8], [360],
#>   table.hline(stroke: 1pt)
#> )