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.
install.packages("r2typ", repos = c("https://y-sunflower.r-universe.dev"))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.
The main thing to understand is that r2typ essentially does one thing: generate text. And that's it!
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.
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])c()vectors and unnamedlist()(such aslist("a", "b") become arrays:
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:
text_(costs = list(hyphenation = percent(100), runt = percent(100)))
#> #text(costs: (hyphenation: 100%, runt: 100%))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)You can use the let() function to define Typst variables, and easily reuse them:
let("yellow", rgb_("#FFC300"))
#> #let yellow = rgb_("#FFC300")
let("mycirc", circle(fill = yellow, square(height = cm_(1))))
#> #let mycirc = circle(fill: yellow)[#square(height: 1cm)]hello <- function(...) typst_function("hello", ...)
hello(fill = red, size = pt_(10), other_arg = "world")
#> #hello(fill: red, size: 10pt, other_arg: "world")If you want to compile and validate your Typst code, you can use the tynding package. It offers 3 functions:
typst_write(): write a.typfiletypst_compile(): compile a.typfileis_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.
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.
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.
- ✅ 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!

