library(r2typ){r2typ} is an R package designed to generate Typst markup. You can use it as is, but it also works well with Quarto.
You can install it with:
# install.packages("pak")
pak::pkg_install("y-sunflower/r2typ")library(r2typ)All functions accept any kind of positional or named arguments:
heading(level = 2, "A title")
#> #heading(level: 2)[A title]
text_(weight = 500, "Some text")
#> #text(weight: 500)[Some text]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 uses a unique approach for units, as they are not quoted.
{r2typ} provides several utility functions to make working with Typst units easy:
text_(size = pt_(20), "Ice cream")
#> #text(size: 20pt)[Ice cream]This works with all Typst units:
image_("image.png", width = percent(80))
#> #image(width: 80%, "image.png")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:
text_(fill = rgb_("#ffc300"), "Yellow-ish text")
#> #text(fill: rgb("#ffc300"))[Yellow-ish text]Similarly to colors, Typst includes specific objects for alignment. They work well in {r2typ}:
place(
center,
dy = pt_(15),
"hello"
)
#> #place(center, dy: 15pt)[hello]You can combine them to mimic Typst syntax:
place(
top + left,
dy = pt_(15),
"hello"
)
#> #place(top + left, dy: 15pt)[hello]You can even combine them with colors, which is often useful for strokes:
line_(stroke = pt_(2) + blue)
#> #line(stroke: 2pt + blue){r2typ} converts some R types into Typst types:
NULL becomes noneimage_("image.png", width = percent(80), alt = NULL)
#> #image(width: 80%, alt: none, "image.png")TRUE/FALSE become true/falselist_(tight = FALSE, "hey", "you")
#> #list(tight: false, [hey], [you])c() vectors and unnamed list() (such as list("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]list() (such as list(a = "hello", b = "world")) become dictionnaries:text_(costs = list(hyphenation = percent(100), runt = percent(100)))
#> #text(costs: (hyphenation: 100%, runt: 100%))Note that the goal here is only to translate R data types that have an equivalent in Typst, not to be exhaustive.
{r2typ} has all a function for all set and (simple) show rules. If you’re unfamiliar with them, check out the official documentation.
set_text(red)
#> #set text(red)
set_enum(tight = FALSE)
#> #set enum(tight: false)
set_par(
`first-line-indent` = em(1),
spacing = em(0.65),
justify = TRUE
)
#> #set par(first-line-indent: 1em, spacing: 0.65em, justify: true)show_heading(set_text(navy))
#> #show heading: set text(navy)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]]])You create tables manually:
table_(
columns = 2,
"hey",
"folks!"
)
#> #table(columns: 2, [hey], [folks!])Or from a data frame:
df <- data.frame(x = c(1, 2, 3), y = c(4, 5, 6))
table_(
`columns-gutter` = cm_(1),
align = right,
df
)
#> #table(columns: 2, columns-gutter: 1cm, align: right, [1], [4], [2], [5], [3], [6])It automatically detects the number of columns, but you can specify it yourself!