r2typ is an R package designed to generate Typst markup. You can use it as is, but it also works well with Quarto.
At the moment, it supports most Typst functions, types, colors, alignment, and units with a very simple syntax, but it is not yet fully functional.
Installation
You can install it with:
# install.packages("pak")
pak::pkg_install("y-sunflower/r2typ")Quick start
All functions accept any kind of positional or named arguments:
However, in Typst, not all functions behave the same way regarding positional and named arguments. For this reason, r2typ automatically adjusts the behavior when necessary.
For example, the list_() function will put everything in a list format:
list_(tight = FALSE, "hey", "you", "!")
#> #list(tight: false, [hey], [you], [!])Note that the function is
list_()and notlist()becauselist()is a base R function.
The same applies to the table_() function:
table_("hey", "you", "!")
#> #table([hey], [you], [!])Typst units
Typst uses a unique approach for units, as they are not quoted.
r2typ provides several utility functions to make working with Typst units easy:
This works with all Typst units:
Typst colors
Typst offers a large set of predefined colors such as red or blue. r2typ provides the same:
text(fill = green, "Green text")
#> #text(fill: green)[Green text]All built-in Typst colors are available in r2typ. You can find them here.
You can also use the rgb() function:
Typst alignment
Similarly to colors, Typst includes specific objects for alignment. They work well in r2typ:
You can combine them to mimic Typst syntax:
You can even combine them with colors, which is often useful for strokes:
Convert R types to Typst types
r2typ converts some R types into Typst types:
-
NULLbecomesnone
-
TRUE/FALSEbecometrue/false
list_(tight = FALSE, "hey", "you")
#> #list(tight: false, [hey], [you])- Named
list()(such aslist(a = "hello", b = "world")) become dictionnaries:
Note that the goal here is only to translate R data types that have an equivalent in Typst, not to be exhaustive.
Set and show rules
r2typ has all a function for all set and (simple) show rules. If you’re unfamiliar with them, check out the official documentation.
set rules
show rules
show_heading(set_text(navy))
#> #show heading: set text(navy)Nested function calls
Calling a function within another function works as well:
place(
center + horizon,
dy = pt(15),
square(size = pt(35), fill = red)
)
#> #place(center + horizon, dy: 15pt)[#square(size: 35pt, fill: red)]
page(
flipped = TRUE,
columns = 2,
fill = red,
place(
top + left,
dx = pt(-5),
rect(
fill = blue,
radius = pt(2),
"yooooo"
)
)
)
#> #page(flipped: true, columns: 2, fill: red, [#place(top + left, dx: -5pt)[#rect(fill: blue, radius: 2pt)[yooooo]]])