
Compile a .typ file with whisker-substituted data via Quarto/Typst
Source:R/compile.R
compile_typst.RdLow-level primitive: whisker-renders path against data, writes the
result next to path, then compiles it to output via quarto typst compile --pdf-standard ua-1 --features a11y-extras. Works on any .typ
file (a package-shipped template or a user's own
export_template()-edited copy) since required-token validation is
derived by scanning the file itself, not a separately-maintained
manifest.
Arguments
- path
Character. Path to a .typ file. Its
theme.typ/components.typ/assets must already be alongside it, so Typst's relative#importpaths resolve correctly.- data
Named list of whisker substitution values. A name that is not a token of
pathor of any built-in template is ignored with a warning (naming the closest token, if there is one), which catches misspelled optional tokens.- output
Character. Path to write the compiled PDF to.
- font_dir
Character or
NULL. A directory of font files (.ttf/.otf) to make available to Typst for this compile, passed through as--font-path. Typst does not merge this with the system font list: both system fonts and this directory are searched, so a theme'stext-font/heading-fonttokens can name either a system-installed font or one shipped here (confirmed directly:quarto typst fonts --font-path <dir>lists a font from<dir>alongside system fonts, not instead of them). DefaultNULL(system fonts only).
Details
A template can declare a token optional with a // comment line,
// optional-token: name = default. When data lacks that token (or
it is NULL, empty, or NA), the declared default is used instead of
raising a missing-token error. A default can refer to other tokens, as
in // optional-token: heading = Results (N = {{{n_total}}}); it is
rendered against data first, and those tokens stay required.
Examples
if (FALSE) { # \dontrun{
# Needs Quarto (bundling Typst) on the system: see check_quarto().
typ <- tempfile(fileext = ".typ")
writeLines(
c("#set document(title: [Example])", "#text[{{{greeting}}}]"), typ
)
compile_typst(typ, list(greeting = "Hello!"), tempfile(fileext = ".pdf"))
} # }