
r2typ allows you to generate Typst markup using R and makes you much more efficient in creating PDF reports. Think of it as htmltools, but for Typst! It supports all of the following:
- ✅ almost all Typst functions (+ an option to add yours)
- ✅ conversions from R to Typst (
NULL->none,TRUE->true, etc.) - ✅ Typst colors, alignment, units and direction natively
- ✅
setand (simple)showrules - ✅ works well with Quarto
- ✅ extremely simple syntax
Check out the documentation.
Motivation
Typst is a powerful typesetting system, but writing its markup programmatically from R can be cumbersome when you need to automate reports, generate dynamic documents, or integrate Typst output into data workflows. r2typ fills this gap by offering a light, consistent interface that turns R expressions directly into valid Typst markup.
It streamlines the creation of complex layouts, ensures reliable type conversions, and makes it easy to embed Typst generation into pipelines, scripts, and Quarto projects.
Quick start
The main thing to understand is that r2typ essentially does one thing: generate text. And that’s it!
Basic usage
library(r2typ)
heading(level = 2, numbering = "1.1", "Hello world")
#> #heading(level: 2, numbering: "1.1")[Hello world]
text_(size = pt_(12), baseline = em(1.2), overhang = FALSE, "hey there")
#> #text(size: 12pt, baseline: 1.2em, overhang: false)[hey there]
image_(width = percent(80), height = auto, "link.svg")
#> #image(width: 80%, height: auto, "link.svg")
circle(fill = blue, "hey")
#> #circle(fill: blue)[hey]
circle(radius = pt_(100), "hey", linebreak(), "there")
#> #circle(radius: 100pt)[hey #linebreak() there]
place(top + left, dy = pt_(15), square(size = pt_(35), fill = red))
#> #place(top + left, dy: 15pt)[#square(size: 35pt, fill: red)][!IMPORTANT] Some function names end with
_to avoid confusion between namespaces: some widely used packages have functions with the same name, and using an identical name could cause the code to malfunction.
Types conversion
r2typ converts some R types into Typst types:
-
NULLbecomesnone
image_("image.png", width = percent(80), alt = NULL)
#> #image(width: 80%, alt: none, \"image.png\")-
TRUE/FALSEbecometrue/false
list_(tight = FALSE, "hey", "you")
#> #list(tight: false, [hey], [you])
text_(`stylistic-set` = c(1, 2, 3), "10 years ago")
#> #text(stylistic-set: (1, 2, 3))[10 years ago]
text_(`stylistic-set` = list(1, 2, 3), "10 years ago") # equivalent
#> #text(stylistic-set: (1, 2, 3))[10 years ago]- Named
list()(such aslist(a = "hello", b = "world")) become dictionnaries:
Set and show rules
set_text(red, size = pt_(20))
#> #set text(size: 20pt, red)
show_heading(set_text(fill = red))
#> #show heading: set text(fill: red, size: 20pt)Create Typst variables
You can use the let() function to define Typst variables, and easily reuse them:
Use your own Typst functions
hello <- function(...) typst_function("hello", ...)
hello(fill = red, size = pt_(10), other_arg = "world")
#> #hello(fill: red, size: 10pt, other_arg: "world")Writing, compiling and validating Typst
If you want to compile and validate your Typst code, you can use the tynding package. It offers 3 functions:
-
typst_write(): write a.typfile -
typst_compile(): compile a.typfile -
is_valid_typst(): evaluate whether a character vector is valid Typst (TRUEorFALSE)
Functions in r2typ accept all positional and named arguments! This means that you’re responsible of making sure the arguments you’re using are valid!
library(tynding)
place(
top + left,
dy = pt_(15),
square(size = pt_(35), fill = red)
) |>
is_valid_typst()
#> TRUE
place(
top + left,
dy = pt_(15),
square(size = pt_(35), fill = red)
) |>
typst_write() |>
typst_compile() # generate the PDFAlso note that all examples in the
r2typdocumentation are valid Typst examples.
Complete example
A complete example that generates a PDF using R only:
library(r2typ)
library(tynding) # typst_write() and typst_compile() functions
c(
set_page(height = pt_(400)),
set_text(purple),
set_circle(width = percent(50)),
align(
center + horizon,
circle(
fill = aqua,
stroke = pt_(5) + red,
align(
right,
text_(
font = "Roboto",
size = em(1.2),
"My favorite food is cookies!"
)
)
)
)
) |>
typst_write() |>
typst_compile(output = "example.pdf")
Learn more in the get started vignette.
Markup VS Code mode in Typst
r2typ generates Typst markup, not Typst code. Most people, when writing native Typst, rely primarily on markup mode. Code mode is mainly used to add logic or create functions.
This is an important distinction to keep in mind, but the core difference is that function calls start with a # (e.g., #text("hey") VS text("hey")).
You can learn more about it here.
Missing functions
- ✅ Text
- ✅ Foundations
- ✅ Model, everything except:
cite,link,numbering,ref,terms - ✅ Layout, everything except:
columns,layout,measure,repeat,rotate - ✅ Visualize, everything except:
curve,gradient,path,stroke,polygon,tiling
something’s missing? Please open an issue!