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
typst_tableobject created withtt().- file
Output file path. The format is determined by the file extension:
.svg,.png,.pdf, or.typ(Typst source).- width
Page width. Use
"auto"(default) for automatic sizing based on table content, or specify a Typst length like"6in","15cm","400pt".- height
Page height. Use
"auto"(default) for automatic sizing, or specify a Typst length.- margin
Page margin around the table. Default is
"0.5em". Use"0pt"for no margin.- 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
NULL(default), searches fortypston the system PATH.- overwrite
Logical. If
TRUE(default), overwrites existing files. IfFALSE, throws an error when the output file already exists.
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.
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")
}