| Title: | Better 'Typst' Tables |
| Version: | 0.1.0 |
| URL: | https://freierson.github.io/typstable/, https://github.com/freierson/typstable |
| BugReports: | https://github.com/freierson/typstable/issues |
| 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. |
| Depends: | R (≥ 4.1.0) |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.3 |
| Imports: | rlang (≥ 1.0.0), tidyselect |
| Suggests: | testthat (≥ 3.0.0), knitr, rmarkdown, tibble, dplyr |
| VignetteBuilder: | knitr |
| Config/testthat/edition: | 3 |
| License: | GPL-3 |
| Date: | 2026-01-26 |
| NeedsCompilation: | no |
| Packaged: | 2026-02-11 23:27:54 UTC; filip |
| Author: | Filip Reierson |
| Maintainer: | Filip Reierson <filip.reierson@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-02-16 17:30:02 UTC |
Knit print method for typst_table
Description
Renders a typst_table for use in Quarto/knitr documents with Typst output.
Usage
knit_print.typst_table(x, ...)
Arguments
x |
A |
... |
Additional arguments (ignored). |
Value
A knitr::asis_output object containing the Typst markup.
Print a typst_table object
Description
Prints the Typst markup for a table to the console.
Usage
## S3 method for class 'typst_table'
print(x, ...)
Arguments
x |
A |
... |
Additional arguments (ignored). |
Value
Invisibly returns the Typst code as a character string.
Create a Typst table
Description
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 |
< |
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 |
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 |
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])
# Select specific columns (excludes row names)
tt(mtcars, cols = c(mpg, cyl, hp), rownames = FALSE)
# Custom column names
tt(mtcars[1:5, 1:3], col_names = c("Miles/Gallon", "Cylinders", "Horsepower"), rownames = FALSE)
# Right-align numeric columns
tt(mtcars[1:5, 1:3], align = "right")
Style specific cells
Description
Applies formatting to individual cells. Allows for precise control over cell appearance including spanning multiple rows or columns.
Usage
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
)
Arguments
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. |
Value
The modified typst_table object.
Examples
# Highlight a specific cell
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_cell(1, 1, fill = "yellow", bold = TRUE)
# Cell spanning multiple columns
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_cell(1, 1, colspan = 2, content = "Combined")
Style specific columns
Description
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 |
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: |
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.
Value
The modified typst_table object.
Examples
# Right-align and bold a numeric column
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_column(mpg, align = "right", bold = TRUE)
# Style multiple columns at once
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
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), rownames = FALSE) |>
tt_column(c(a, b), color = "color_{col}")
Add grouped header row above column names
Description
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,
gap = "10pt"
)
Arguments
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). |
gap |
Width of visual gap between header groups (e.g., "10pt", "0.5em"). When specified, empty columns are inserted between groups. Default "10pt". Use NULL to disable gaps. |
Value
The modified typst_table object.
Examples
# Group columns under headers
tt(mtcars[1:5, 1:6], rownames = FALSE) |>
tt_header_above(c("Performance" = 3, "Design" = 3))
# Leave some columns ungrouped
tt(mtcars[1:5, 1:6], rownames = FALSE) |>
tt_header_above(c(" " = 1, "Group A" = 2, "Group B" = 3))
# Disable gaps between groups
tt(mtcars[1:5, 1:6], rownames = FALSE) |>
tt_header_above(c(" " = 1, "Group A" = 2, "Group B" = 3), gap = NULL)
Add horizontal line to table
Description
Adds a horizontal line at a specific position in the table.
Usage
tt_hline(table, y, start = NULL, end = NULL, stroke = TRUE)
Arguments
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: |
Value
The modified typst_table object.
Examples
# Add line below header
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_hline(1)
# Add colored line
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_hline(1, stroke = "blue")
# Partial line spanning some columns
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_hline(1, start = 0, end = 2)
Group rows under a label
Description
Inserts a group label row and optionally indents the grouped rows. Useful for creating visual sections in the table.
Usage
tt_pack_rows(
table,
group_label = NULL,
start_row = NULL,
end_row = NULL,
index = NULL,
indent = TRUE,
bold_label = TRUE
)
Arguments
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_label |
Logical. Make the group label bold (default TRUE). |
Value
The modified typst_table object.
Examples
# Group rows with a label
tt(mtcars[1:10, 1:3], rownames = FALSE) |>
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], rownames = FALSE) |>
tt_pack_rows(index = c("4 Cylinders" = 5, "6 Cylinders" = 5))
Render a typst_table to Typst markup
Description
Generates Typst code from a typst_table object. This is called automatically
by print() and knit_print(), but can also be called directly.
Usage
tt_render(table)
Arguments
table |
A |
Value
A character string containing Typst table markup.
Examples
code <- tt(mtcars[1:3, 1:3], rownames = FALSE) |> tt_render()
cat(code)
Style specific rows
Description
Applies formatting to one or more rows. Use row = 0 to style the header row.
Usage
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
)
Arguments
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. |
Value
The modified typst_table object.
Examples
# Style header row
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_row(0, bold = TRUE, fill = "gray")
# Highlight specific rows
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_row(c(1, 3, 5), fill = "#ffffcc")
# Add horizontal lines
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_row(3, hline_above = TRUE, hline_below = TRUE)
Save a typst_table to SVG, PNG, PDF, or Typst source
Description
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.
Usage
tt_save(
table,
file,
width = "auto",
height = "auto",
margin = "0.5em",
ppi = 144,
typst_path = NULL,
overwrite = TRUE
)
Arguments
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 |
Details
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:
1
Generates a complete Typst document with appropriate page settings
Writes it to a temporary
.typfileCalls
typst compileto render the outputCleans 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.
Value
Invisibly returns the output file path.
Examples
# Save Typst source (no CLI required)
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_save(file.path(tempdir(), "table.typ"))
if (tt_typst_available()) {
# Save as SVG
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_save(file.path(tempdir(), "table.svg"))
# Save as PNG with higher resolution
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_save(file.path(tempdir(), "table.png"), ppi = 300)
# Save as PDF with specific page size
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_style(stroke = TRUE) |>
tt_save(file.path(tempdir(), "table.pdf"), width = "6in", height = "4in")
}
Style overall table appearance
Description
Sets global styling properties for the entire table including stroke (borders), fill, striped rows, and spacing.
Usage
tt_style(
table,
stroke = NULL,
fill = NULL,
striped = NULL,
inset = NULL,
row_gutter = NULL,
column_gutter = NULL,
position = NULL,
full_width = FALSE
)
Arguments
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: |
full_width |
Logical. If |
Value
The modified typst_table object.
Examples
# Add borders and striped rows
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_style(stroke = TRUE, striped = TRUE)
# Custom border color and padding
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_style(stroke = "gray", inset = "8pt")
Check if Typst CLI is available
Description
Tests whether the Typst command-line interface is installed and accessible.
Usage
tt_typst_available(typst_path = NULL)
Arguments
typst_path |
Optional path to the Typst executable. If |
Value
TRUE if Typst is available, FALSE otherwise.
Examples
# Check if Typst is installed
if (tt_typst_available()) {
message("Typst is available")
} else {
message("Typst not found - install from https://typst.app/")
}
Add vertical line to table
Description
Adds a vertical line at a specific position in the table.
Usage
tt_vline(table, x, start = NULL, end = NULL, stroke = TRUE)
Arguments
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: |
Value
The modified typst_table object.
Examples
# Add line after first column
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_vline(1)
# Add partial vertical line (data rows only)
tt(mtcars[1:5, 1:3], rownames = FALSE) |>
tt_vline(1, start = 1)
Set proportional column widths
Description
Sets column widths as proportions that fill the page or container width.
Widths are converted to Typst fr (fractional) units.
Usage
tt_widths(table, ...)
Arguments
table |
A |
... |
Width values as numbers. Can be unnamed (applied in order) or named by column. Values represent relative proportions. |
Details
Widths are relative proportions, not absolute values. For example,
tt_widths(tbl, 1, 2, 1) creates columns at 25%, 50%, 25% of the container width.
Value
The modified typst_table object.
Examples
# Equal widths
tt(mtcars[1:5, 1:3], rownames = FALSE) |> tt_widths(1, 1, 1)
# Proportional widths (25%, 50%, 25%)
tt(mtcars[1:5, 1:3], rownames = FALSE) |> tt_widths(1, 2, 1)
# Named columns
tt(mtcars[1:5, 1:3], rownames = FALSE) |> tt_widths(mpg = 1, cyl = 2, disp = 1)