| Title: | Better 'Typst' Tables |
|---|---|
| Description: | Create 'Typst' table markup from data frames. Features a pipe-friendly interface for column, row, and cell styling with support for grouped headers, grouped rows, and data-driven formatting. |
| Authors: | Filip Reierson [aut, cre] (ORCID: <https://orcid.org/0009-0003-2393-5810>) |
| Maintainer: | Filip Reierson <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.0 |
| Built: | 2026-06-02 10:52:01 UTC |
| Source: | https://github.com/freierson/typstable |
Prints the Typst markup for a table to the console.
## S3 method for class 'typst_table' print(x, ...)## S3 method for class 'typst_table' print(x, ...)
x |
A |
... |
Additional arguments (ignored). |
Invisibly returns the Typst code as a character string.
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.
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 )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 )
data |
A data.frame or tibble to convert to a table. |
cols |
< |
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 |
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 |
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 |
repeat_header |
Logical. If TRUE (default), wraps header rows in
|
A typst_table object that can be further styled and rendered.
# Basic table tt(mtcars[1:5, 1:3]) # Select specific columns tt(mtcars, cols = c(mpg, cyl, hp)) # Custom column names tt(mtcars[1:5, 1:3], col_names = c("Miles/Gallon", "Cylinders", "Horsepower")) # Right-align numeric columns tt(mtcars[1:5, 1:3], align = "right")# Basic table tt(mtcars[1:5, 1:3]) # Select specific columns tt(mtcars, cols = c(mpg, cyl, hp)) # Custom column names tt(mtcars[1:5, 1:3], col_names = c("Miles/Gallon", "Cylinders", "Horsepower")) # Right-align numeric columns tt(mtcars[1:5, 1:3], align = "right")
Applies formatting to individual cells. Allows for precise control over cell appearance including spanning multiple rows or columns.
tt_cell( table, row, column, bold = NULL, italic = NULL, color = NULL, fill = NULL, align = NULL, font_size = NULL, rotate = NULL, inset = NULL, stroke = NULL, colspan = 1, rowspan = 1, content = NULL )tt_cell( table, row, column, bold = NULL, italic = NULL, color = NULL, fill = NULL, align = NULL, font_size = NULL, rotate = NULL, inset = NULL, stroke = NULL, colspan = 1, rowspan = 1, content = NULL )
table |
A |
row |
Row number (1-indexed for data rows, 0 for header). Use negative
indices to target |
column |
Column specification: integer index or column name. |
bold |
Logical. Make text bold. |
italic |
Logical. Make text italic. |
color |
Text color. |
fill |
Fill color. |
align |
Cell alignment. |
font_size |
Font size. |
rotate |
Rotation angle (e.g., |
inset |
Cell padding (e.g., |
stroke |
Stroke (border) specification for the cell. Can be |
colspan |
Number of columns to span (default 1). |
rowspan |
Number of rows to span (default 1). |
content |
Optional content to replace cell value. |
The modified typst_table object.
# Highlight a specific cell tt(mtcars[1:5, 1:3]) |> tt_cell(1, 1, fill = "yellow", bold = TRUE) # Cell spanning multiple columns tt(mtcars[1:5, 1:3]) |> tt_cell(1, 1, colspan = 2, content = "Combined")# Highlight a specific cell tt(mtcars[1:5, 1:3]) |> tt_cell(1, 1, fill = "yellow", bold = TRUE) # Cell spanning multiple columns tt(mtcars[1:5, 1:3]) |> tt_cell(1, 1, colspan = 2, content = "Combined")
Applies formatting to one or more columns. Supports both static values and data-driven formatting where styling parameters reference other columns.
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") )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") )
table |
A |
column |
< |
width |
Column width (e.g., |
align |
Column alignment: |
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 |
italic |
Logical, column name, or pattern string. Same behavior as |
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., |
border_right |
Right border specification. |
font_size |
Font size (e.g., |
rotate |
Rotation angle (e.g., |
inset |
Cell padding (e.g., |
stroke |
Stroke (border) specification for the cell(s). Can be |
.missing |
How to handle missing pattern columns: |
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.
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.
The modified typst_table object.
# Right-align and bold a numeric column tt(mtcars[1:5, 1:3]) |> tt_column(mpg, align = "right", bold = TRUE) # Style multiple columns at once tt(mtcars[1:5, 1:3]) |> tt_column(c(mpg, disp), align = "right", color = "blue") # 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)) |> tt_column(c(a, b), color = "color_{col}")# Right-align and bold a numeric column tt(mtcars[1:5, 1:3]) |> tt_column(mpg, align = "right", bold = TRUE) # Style multiple columns at once tt(mtcars[1:5, 1:3]) |> tt_column(c(mpg, disp), align = "right", color = "blue") # 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)) |> tt_column(c(a, b), color = "color_{col}")
Adds a spanning header row above the existing column names to group related columns together.
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" )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" )
table |
A |
header |
Named vector specifying header groups. Names are the group labels,
values are the number of columns each group spans. Use empty string |
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., |
inset |
Cell padding (e.g., |
stroke |
Stroke (border) specification for the header cell(s). Can be |
line |
Logical. Add horizontal line below the header (default TRUE). |
line_sep |
Stroke width of the white separator line between header groups
(e.g., |
The modified typst_table object.
# Group columns under headers tt(mtcars[1:5, 1:6]) |> tt_header_above(c("Performance" = 3, "Design" = 3)) # Leave some columns ungrouped tt(mtcars[1:5, 1:6]) |> tt_header_above(c(" " = 1, "Group A" = 2, "Group B" = 3)) # Disable separator between groups tt(mtcars[1:5, 1:6]) |> tt_header_above(c(" " = 1, "Group A" = 2, "Group B" = 3), line_sep = NULL)# Group columns under headers tt(mtcars[1:5, 1:6]) |> tt_header_above(c("Performance" = 3, "Design" = 3)) # Leave some columns ungrouped tt(mtcars[1:5, 1:6]) |> tt_header_above(c(" " = 1, "Group A" = 2, "Group B" = 3)) # Disable separator between groups tt(mtcars[1:5, 1:6]) |> tt_header_above(c(" " = 1, "Group A" = 2, "Group B" = 3), line_sep = NULL)
Adds a horizontal line at a specific position in the table.
tt_hline(table, y, start = NULL, end = NULL, stroke = TRUE)tt_hline(table, y, start = NULL, end = NULL, stroke = TRUE)
table |
A |
y |
Position of the line (0 = top of table, 1 = below header, 2 = below first data row, etc.). |
start |
Starting column for partial line (0-indexed, inclusive). |
end |
Ending column for partial line (0-indexed, inclusive). |
stroke |
Line style: |
The modified typst_table object.
# Add line below header tt(mtcars[1:5, 1:3]) |> tt_hline(1) # Add colored line tt(mtcars[1:5, 1:3]) |> tt_hline(1, stroke = "blue") # Partial line spanning some columns tt(mtcars[1:5, 1:3]) |> tt_hline(1, start = 0, end = 2) # Remove top booktabs rule tt(mtcars[1:5, 1:3]) |> tt_hline(0, stroke = FALSE)# Add line below header tt(mtcars[1:5, 1:3]) |> tt_hline(1) # Add colored line tt(mtcars[1:5, 1:3]) |> tt_hline(1, stroke = "blue") # Partial line spanning some columns tt(mtcars[1:5, 1:3]) |> tt_hline(1, start = 0, end = 2) # Remove top booktabs rule tt(mtcars[1:5, 1:3]) |> tt_hline(0, stroke = FALSE)
Inserts a group label row and optionally indents the grouped rows. Useful for creating visual sections in the table.
tt_pack_rows( table, group_label = NULL, start_row = NULL, end_row = NULL, index = NULL, indent = TRUE, bold = TRUE, italic = NULL, color = NULL, fill = NULL, align = NULL, font_size = NULL, rotate = NULL, inset = NULL, stroke = NULL, hline_below = NULL )tt_pack_rows( table, group_label = NULL, start_row = NULL, end_row = NULL, index = NULL, indent = TRUE, bold = TRUE, italic = NULL, color = NULL, fill = NULL, align = NULL, font_size = NULL, rotate = NULL, inset = NULL, stroke = NULL, hline_below = NULL )
table |
A |
group_label |
The label text for the group (used with |
start_row |
First row number in the group (1-indexed). |
end_row |
Last row number in the group. |
index |
A named numeric vector for multiple groups (alternative style).
Names are group labels, values are row counts.
Example: |
indent |
Logical. Indent rows in the group (default TRUE). |
bold |
Logical. Make the group label bold (default TRUE). |
italic |
Logical. Make the group label italic. |
color |
Text color for the group label. |
fill |
Fill color for the group label row. |
align |
Alignment for the group label row. |
font_size |
Font size for the group label. |
rotate |
Rotation angle (e.g., |
inset |
Cell padding (e.g., |
stroke |
Stroke (border) specification for the group label cell. Can be |
hline_below |
Add horizontal line below the group label. Can be |
The modified typst_table object.
# Group rows with a label tt(mtcars[1:10, 1:3]) |> tt_pack_rows("4 Cylinders", 1, 5) |> tt_pack_rows("6 Cylinders", 6, 10) # Using index parameter (alternative style) tt(mtcars[1:10, 1:3]) |> tt_pack_rows(index = c("4 Cylinders" = 5, "6 Cylinders" = 5)) # Styled group labels tt(mtcars[1:10, 1:3]) |> tt_pack_rows("4 Cylinders", 1, 5, fill = "#e6f3ff", italic = TRUE) |> tt_pack_rows("6 Cylinders", 6, 10, fill = "#e6f3ff", italic = TRUE)# Group rows with a label tt(mtcars[1:10, 1:3]) |> tt_pack_rows("4 Cylinders", 1, 5) |> tt_pack_rows("6 Cylinders", 6, 10) # Using index parameter (alternative style) tt(mtcars[1:10, 1:3]) |> tt_pack_rows(index = c("4 Cylinders" = 5, "6 Cylinders" = 5)) # Styled group labels tt(mtcars[1:10, 1:3]) |> tt_pack_rows("4 Cylinders", 1, 5, fill = "#e6f3ff", italic = TRUE) |> tt_pack_rows("6 Cylinders", 6, 10, fill = "#e6f3ff", italic = TRUE)
Generates Typst code from a typst_table object. This is called automatically
by print() and knit_print(), but can also be called directly.
tt_render(table)tt_render(table)
table |
A |
A character string containing Typst table markup.
code <- tt(mtcars[1:3, 1:3]) |> tt_render() cat(code)code <- tt(mtcars[1:3, 1:3]) |> tt_render() cat(code)
Applies formatting to one or more rows. Use row = 0 to style the header row.
tt_row( table, row, bold = NULL, italic = NULL, color = NULL, fill = NULL, align = NULL, font_size = NULL, rotate = NULL, inset = NULL, stroke = NULL, hline_above = NULL, hline_below = NULL )tt_row( table, row, bold = NULL, italic = NULL, color = NULL, fill = NULL, align = NULL, font_size = NULL, rotate = NULL, inset = NULL, stroke = NULL, hline_above = NULL, hline_below = NULL )
table |
A |
row |
Integer vector of row numbers to style. Use 0 for the header row,
1 to n for data rows. Use negative indices to target |
bold |
Logical. Make text bold. |
italic |
Logical. Make text italic. |
color |
Text color. |
fill |
Fill color. |
align |
Row alignment override. |
font_size |
Font size. |
rotate |
Rotation angle (e.g., |
inset |
Cell padding (e.g., |
stroke |
Stroke (border) specification for the cell(s). Can be |
hline_above |
Add horizontal line above the row. Can be |
hline_below |
Add horizontal line below the row. Can be |
The modified typst_table object.
# Style header row tt(mtcars[1:5, 1:3]) |> tt_row(0, bold = TRUE, fill = "gray") # Highlight specific rows tt(mtcars[1:5, 1:3]) |> tt_row(c(1, 3, 5), fill = "#ffffcc") # Add horizontal lines tt(mtcars[1:5, 1:3]) |> tt_row(3, hline_above = TRUE, hline_below = TRUE)# Style header row tt(mtcars[1:5, 1:3]) |> tt_row(0, bold = TRUE, fill = "gray") # Highlight specific rows tt(mtcars[1:5, 1:3]) |> tt_row(c(1, 3, 5), fill = "#ffffcc") # Add horizontal lines tt(mtcars[1:5, 1:3]) |> tt_row(3, hline_above = TRUE, hline_below = TRUE)
Exports a typst_table object to various output formats. For SVG, PNG, and PDF
output, the Typst CLI must be installed and available on the system PATH.
tt_save( table, file, width = "auto", height = "auto", margin = "0.5em", ppi = 144, typst_path = NULL, overwrite = TRUE )tt_save( table, file, width = "auto", height = "auto", margin = "0.5em", ppi = 144, typst_path = NULL, overwrite = TRUE )
table |
A |
file |
Output file path. The format is determined by the file extension:
|
width |
Page width. Use |
height |
Page height. Use |
margin |
Page margin around the table. Default is |
ppi |
Pixels per inch for PNG output. Default is 144. Higher values produce larger, higher-resolution images. |
typst_path |
Path to the Typst executable. If |
overwrite |
Logical. If |
For .typ output, only the Typst source code is written and the Typst CLI
is not required.
For .svg, .png, and .pdf output, the function:
Generates a complete Typst document with appropriate page settings
Writes it to a temporary .typ file
Calls typst compile to render the output
Cleans up the temporary file
The width and height parameters control the page size. Using "auto"
for both (the default
for both (the default) creates a page that fits the table content exactly.
Invisibly returns the output file path.
# Save Typst source (no CLI required) tt(mtcars[1:5, 1:3]) |> tt_save(file.path(tempdir(), "table.typ")) if (tt_typst_available()) { # Save as SVG tt(mtcars[1:5, 1:3]) |> tt_save(file.path(tempdir(), "table.svg")) # Save as PNG with higher resolution tt(mtcars[1:5, 1:3]) |> tt_save(file.path(tempdir(), "table.png"), ppi = 300) # Save as PDF with specific page size tt(mtcars[1:5, 1:3]) |> tt_style(stroke = TRUE) |> tt_save(file.path(tempdir(), "table.pdf"), width = "6in", height = "4in") }# Save Typst source (no CLI required) tt(mtcars[1:5, 1:3]) |> tt_save(file.path(tempdir(), "table.typ")) if (tt_typst_available()) { # Save as SVG tt(mtcars[1:5, 1:3]) |> tt_save(file.path(tempdir(), "table.svg")) # Save as PNG with higher resolution tt(mtcars[1:5, 1:3]) |> tt_save(file.path(tempdir(), "table.png"), ppi = 300) # Save as PDF with specific page size tt(mtcars[1:5, 1:3]) |> tt_style(stroke = TRUE) |> tt_save(file.path(tempdir(), "table.pdf"), width = "6in", height = "4in") }
Sets global styling properties for the entire table including stroke (borders), fill, striped rows, and spacing.
tt_style( table, stroke = NULL, fill = NULL, striped = NULL, inset = NULL, row_gutter = NULL, column_gutter = NULL, position = NULL )tt_style( table, stroke = NULL, fill = NULL, striped = NULL, inset = NULL, row_gutter = NULL, column_gutter = NULL, position = NULL )
table |
A |
stroke |
Stroke (border) specification: |
fill |
Fill color for the entire table. |
striped |
Logical. If |
inset |
Cell padding. Can be a single value (e.g., |
row_gutter |
Vertical spacing between rows. |
column_gutter |
Horizontal spacing between columns. |
position |
Table position on page: |
The modified typst_table object.
# Add borders and striped rows tt(mtcars[1:5, 1:3]) |> tt_style(stroke = TRUE, striped = TRUE) # Custom border color and padding tt(mtcars[1:5, 1:3]) |> tt_style(stroke = "gray", inset = "8pt")# Add borders and striped rows tt(mtcars[1:5, 1:3]) |> tt_style(stroke = TRUE, striped = TRUE) # Custom border color and padding tt(mtcars[1:5, 1:3]) |> tt_style(stroke = "gray", inset = "8pt")
Tests whether the Typst command-line interface is installed and accessible.
tt_typst_available(typst_path = NULL)tt_typst_available(typst_path = NULL)
typst_path |
Optional path to the Typst executable. If |
TRUE if Typst is available, FALSE otherwise.
# Check if Typst is installed if (tt_typst_available()) { message("Typst is available") } else { message("Typst not found - install from https://typst.app/") }# Check if Typst is installed if (tt_typst_available()) { message("Typst is available") } else { message("Typst not found - install from https://typst.app/") }
Adds a vertical line at a specific position in the table.
tt_vline(table, x, start = NULL, end = NULL, stroke = TRUE)tt_vline(table, x, start = NULL, end = NULL, stroke = TRUE)
table |
A |
x |
Position of the line (0 = before first column, 1 = after first column, etc.). |
start |
Starting row for partial line (0-indexed, inclusive, 0 = header). |
end |
Ending row for partial line (0-indexed, inclusive). |
stroke |
Line style: |
The modified typst_table object.
# Add line after first column tt(mtcars[1:5, 1:3]) |> tt_vline(1) # Add partial vertical line (data rows only) tt(mtcars[1:5, 1:3]) |> tt_vline(1, start = 1)# Add line after first column tt(mtcars[1:5, 1:3]) |> tt_vline(1) # Add partial vertical line (data rows only) tt(mtcars[1:5, 1:3]) |> tt_vline(1, start = 1)
Sets column widths. Numeric values are converted to Typst units via .unit.
String values are passed through directly to Typst (e.g. "auto", "1fr",
"100pt", "50%"). Validity of strings is not checked in R — Typst will
error on invalid values.
tt_widths(table, ..., .unit = function(x) paste0(x, "fr"), .default = NULL)tt_widths(table, ..., .unit = function(x) paste0(x, "fr"), .default = NULL)
table |
A |
... |
Width values as numbers or strings. Can be unnamed (positional, applied in column order) or named by column name. Positional mode requires exactly one value per column. |
.unit |
Function applied to numeric values to produce a Typst length
string. Default appends |
.default |
When using named columns, the width to apply to columns not
explicitly mentioned. |
Column widths follow last-value-in-pipe-wins logic: calling tt_widths() a
second time with named columns only updates those columns.
Fractional units (fr) distribute available space proportionally. For
example, tt_widths(tbl, 1, 2, 1) creates columns at 25%, 50%, 25% of the
container width.
The modified typst_table object.
# Proportional widths (25%, 50%, 25%) tt(mtcars[1:5, 1:3]) |> tt_widths(1, 2, 1) # String widths passed through directly tt(mtcars[1:5, 1:3]) |> tt_widths("100pt", "auto", "50%") # Update only one column, leave others unchanged tt(mtcars[1:5, 1:3]) |> tt_widths(cyl = 2) # Set all unmentioned columns to auto tt(mtcars[1:5, 1:3]) |> tt_widths(cyl = 2, .default = "auto") # Custom numeric unit tt(mtcars[1:5, 1:3]) |> tt_widths(1, 2, 1, .unit = \(x) paste0(x, "pt"))# Proportional widths (25%, 50%, 25%) tt(mtcars[1:5, 1:3]) |> tt_widths(1, 2, 1) # String widths passed through directly tt(mtcars[1:5, 1:3]) |> tt_widths("100pt", "auto", "50%") # Update only one column, leave others unchanged tt(mtcars[1:5, 1:3]) |> tt_widths(cyl = 2) # Set all unmentioned columns to auto tt(mtcars[1:5, 1:3]) |> tt_widths(cyl = 2, .default = "auto") # Custom numeric unit tt(mtcars[1:5, 1:3]) |> tt_widths(1, 2, 1, .unit = \(x) paste0(x, "pt"))