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,
  epilogue = NULL,
  escape = TRUE,
  rownames = FALSE,
  na_string = "-",
  booktabs = TRUE,
  repeat_header = 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. A single value is recycled to all columns. Defaults to "auto" which sizes columns to fit content. Use tt_widths() for proportional widths that fill the container.

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")').

epilogue

Optional character string of raw Typst code to insert after the table. Useful for notes, captions, or other content that should follow the table.

escape

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

rownames

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

na_string

Character. How NA values are displayed in the table (default "-").

booktabs

Logical. If TRUE (default), renders with booktabs-style rules (no cell borders, three horizontal rules: top, mid, bottom). If FALSE, renders a grid-style table with 0.5pt + black borders on all cells.

repeat_header

Logical. If TRUE (default), wraps header rows in table.header() so they repeat on each page in multi-page tables.

Value

A typst_table object that can be further styled and rendered.

Examples

# Basic table
tt(mtcars[1:5, 1:3])
#> #table(
#>   columns: (auto, auto, auto),
#>   stroke: none,
#>   table.header(
#>     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)
#> ) 

# Select specific columns
tt(mtcars, cols = c(mpg, cyl, hp))
#> #table(
#>   columns: (auto, auto, auto),
#>   stroke: none,
#>   table.header(
#>     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"))
#> #table(
#>   columns: (auto, auto, auto),
#>   stroke: none,
#>   table.header(
#>     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: (auto, auto, auto),
#>   align: right,
#>   stroke: none,
#>   table.header(
#>     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)
#> )