| Title: | Render High-Quality Text and Formulas in 'ggplot2' by Typst |
|---|---|
| Description: | Provides a seamless integration of the 'Typst' typesetting engine into 'ggplot2'. It allows users to render complex mathematical formulas, equations, and rich text directly in plot annotations, axis labels, and titles without requiring a local Typst or LaTeX installation. Under the hood, it leverages Rust FFI to compile Typst code into SVG. |
| Authors: | Hao Cheng [aut, cre, cph] |
| Maintainer: | Hao Cheng <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-03-16 09:57:21 UTC |
| Source: | https://github.com/Yousa-Mirage/ggtypst |
annotate_math_mitex() is the LaTeX-input companion to annotate_math_typst().
It accepts LaTeX-style math, converts it to Typst math through MiTeX,
and renders the converted result as a single annotation.
It will normalize the outer $...$ or $$...$$ delimiters automatically.
This allows you to use LaTeX math syntax in your plot, which you may be more familiar with :).
annotate_math_mitex( latex_math_code, x, y, hjust = 0.5, vjust = 0.5, scale = 1, size = NULL, size.unit = "pt", color = NULL, colour = NULL, alpha = NULL, face = NULL, fontface = NULL, angle = NULL, lineheight = NULL, math_family = NULL, inline = FALSE )annotate_math_mitex( latex_math_code, x, y, hjust = 0.5, vjust = 0.5, scale = 1, size = NULL, size.unit = "pt", color = NULL, colour = NULL, alpha = NULL, face = NULL, fontface = NULL, angle = NULL, lineheight = NULL, math_family = NULL, inline = FALSE )
latex_math_code |
A single LaTeX math string. Outer |
x, y
|
The annotation position in data coordinates. |
hjust, vjust
|
Horizontal and vertical justification for the rendered
grob ( |
scale |
A positive scaling factor applied to the rendered Typst size. |
size |
Optional text size. |
size.unit |
The unit of |
color, colour
|
Optional text color. RGB or color name are supported. |
alpha |
Optional color alpha multiplier in |
face, fontface
|
Optional math face. Only |
angle |
Optional text rotation angle in degrees. |
lineheight |
Optional line height value. May be negative. |
math_family |
Optional font family for math content. The default math font is |
inline |
Whether to render inline math. Default |
MiTeX is a LaTeX-to-Typst converter. It should be stable and reliable for typical LaTeX math expressions. If you find any LaTeX math that MiTeX fails to convert properly, you can report an issue to ggtypst first.
A ggplot2 layer.
annotate_typst(), annotate_math_typst()
ggplot(mtcars, aes(wt, mpg)) + geom_point() + annotate_math_mitex( latex_math_code = r"(\eta = \frac{mpg}{wt}, \text{written in LaTeX math})", x = 3.5, y = 30, size = 18, face = "bold" ) + theme_minimal()ggplot(mtcars, aes(wt, mpg)) + geom_point() + annotate_math_mitex( latex_math_code = r"(\eta = \frac{mpg}{wt}, \text{written in LaTeX math})", x = 3.5, y = 30, size = 18, face = "bold" ) + theme_minimal()
annotate_math_typst() is the math-specialized variant of annotate_typst().
It treats the annotation text as Typst math expression and then renders it to a
math visual representation.
It will normalize the outer $...$ or $ ... $ delimiters automatically.
annotate_math_typst( typst_math_code, x, y, hjust = 0.5, vjust = 0.5, scale = 1, size = NULL, size.unit = "pt", color = NULL, colour = NULL, alpha = NULL, face = NULL, fontface = NULL, angle = NULL, lineheight = NULL, math_family = NULL, inline = FALSE )annotate_math_typst( typst_math_code, x, y, hjust = 0.5, vjust = 0.5, scale = 1, size = NULL, size.unit = "pt", color = NULL, colour = NULL, alpha = NULL, face = NULL, fontface = NULL, angle = NULL, lineheight = NULL, math_family = NULL, inline = FALSE )
typst_math_code |
A single Typst math string. Outer |
x, y
|
The annotation position in data coordinates. |
hjust, vjust
|
Horizontal and vertical justification for the rendered
grob ( |
scale |
A positive scaling factor applied to the rendered Typst size. |
size |
Optional text size. |
size.unit |
The unit of |
color, colour
|
Optional text color. RGB or color name are supported. |
alpha |
Optional color alpha multiplier in |
face, fontface
|
Optional math face. Only |
angle |
Optional text rotation angle in degrees. |
lineheight |
Optional line height value. May be negative. |
math_family |
Optional font family for math content. The default math font is |
inline |
Whether to render inline math. Default |
A ggplot2 layer.
annotate_typst(), annotate_math_mitex()
ggplot(mtcars, aes(wt, mpg)) + geom_point() + annotate_math_typst( typst_math_code = r"(sum_(i=1)^n x_i)", x = 3.5, y = 30, size = 18, face = "bold" ) + theme_minimal()ggplot(mtcars, aes(wt, mpg)) + geom_point() + annotate_math_typst( typst_math_code = r"(sum_(i=1)^n x_i)", x = 3.5, y = 30, size = 18, face = "bold" ) + theme_minimal()
annotate_typst() is a more powerful replacement for ggplot2::annotate() that
renders a single Typst string and inserts that grob into a ggplot2 plot.
Use it when you want one manually positioned note, callout, or mixed text-and-math label.
annotate_typst( typst_code, x, y, hjust = 0.5, vjust = 0.5, scale = 1, size = NULL, size.unit = "pt", color = NULL, colour = NULL, alpha = NULL, face = NULL, fontface = NULL, angle = NULL, lineheight = NULL, family = NULL, math_family = NULL )annotate_typst( typst_code, x, y, hjust = 0.5, vjust = 0.5, scale = 1, size = NULL, size.unit = "pt", color = NULL, colour = NULL, alpha = NULL, face = NULL, fontface = NULL, angle = NULL, lineheight = NULL, family = NULL, math_family = NULL )
typst_code |
A single Typst source string to render. |
x, y
|
The annotation position in data coordinates. |
hjust, vjust
|
Horizontal and vertical justification for the rendered
grob ( |
scale |
A positive scaling factor applied to the rendered Typst size. |
size |
Optional text size. |
size.unit |
The unit of |
color, colour
|
Optional text color. RGB or color name are supported. |
alpha |
Optional color alpha multiplier in |
face, fontface
|
Optional text face: |
angle |
Optional text rotation angle in degrees. |
lineheight |
Optional line height value. May be negative. |
family |
Optional text font family. The family must be available to Typst. If |
math_family |
Optional font family for math content. The default math font is |
A ggplot2 layer.
annotate_math_typst(), annotate_math_mitex()
ggplot(mtcars, aes(wt, mpg)) + geom_point() + annotate_typst( typst_code = r"(#rect(radius: 5pt)[*Fuel economy* #linebreak() $sum_(i=1)^n x_i$])", x = 3.5, y = 30, size = 18, color = "red" ) + theme_minimal()ggplot(mtcars, aes(wt, mpg)) + geom_point() + annotate_typst( typst_code = r"(#rect(radius: 5pt)[*Fuel economy* #linebreak() $sum_(i=1)^n x_i$])", x = 3.5, y = 30, size = 18, color = "red" ) + theme_minimal()
element_math_mitex() is the LaTeX-input companion to element_math_typst().
It accepts LaTeX-style math, converts it to Typst math through MiTeX,
and renders the converted result as the element's visual representation.
It will normalize the outer $...$ or $$...$$ delimiters automatically.
This allows you to use LaTeX math syntax in your plot, which you may be more familiar with :).
element_math_mitex( hjust = NULL, vjust = NULL, scale = NULL, size = NULL, size.unit = "pt", color = NULL, colour = NULL, alpha = NULL, face = NULL, fontface = NULL, angle = NULL, lineheight = NULL, math_family = NULL, inline = FALSE, margin = NULL, debug = NULL, inherit.blank = FALSE )element_math_mitex( hjust = NULL, vjust = NULL, scale = NULL, size = NULL, size.unit = "pt", color = NULL, colour = NULL, alpha = NULL, face = NULL, fontface = NULL, angle = NULL, lineheight = NULL, math_family = NULL, inline = FALSE, margin = NULL, debug = NULL, inherit.blank = FALSE )
hjust, vjust
|
Horizontal and vertical justification for the rendered
grob ( |
scale |
A positive scaling factor applied to the rendered Typst size. |
size |
Optional font size. |
size.unit |
The unit of |
color, colour
|
Optional text color. RGB or color name are supported. |
alpha |
Optional color alpha multiplier in |
face, fontface
|
Optional math face. Only |
angle |
Optional text rotation angle in degrees. |
lineheight |
Optional line height value. May be negative. |
math_family |
Optional font family for math content. The default math font is |
inline |
Whether to render inline math. Default |
margin |
Optional text margins created with |
debug |
If |
inherit.blank |
If |
MiTeX is a LaTeX-to-Typst converter. It should be stable and reliable for typical LaTeX math expressions. If you find any LaTeX math that MiTeX fails to convert properly, you can report an issue to ggtypst first.
A ggplot2 theme element.
element_typst(), element_math_typst()
ggplot(mtcars, aes(wt, mpg)) + geom_point() + labs(x = r"(\eta = \frac{mpg}{wt}, \text{written in LaTeX math})") + theme_minimal() + theme(axis.title.x = element_math_mitex(size = 14))ggplot(mtcars, aes(wt, mpg)) + geom_point() + labs(x = r"(\eta = \frac{mpg}{wt}, \text{written in LaTeX math})") + theme_minimal() + theme(axis.title.x = element_math_mitex(size = 14))
element_math_typst() is the math-specialized variant of element_typst(). It
treats the theme element's text as Typst math expression and then renders it as
the element's visual representation.
It will normalize the outer $...$ or $ ... $ delimiters automatically.
element_math_typst( hjust = NULL, vjust = NULL, scale = NULL, size = NULL, size.unit = "pt", color = NULL, colour = NULL, alpha = NULL, face = NULL, fontface = NULL, angle = NULL, lineheight = NULL, math_family = NULL, inline = FALSE, margin = NULL, debug = NULL, inherit.blank = FALSE )element_math_typst( hjust = NULL, vjust = NULL, scale = NULL, size = NULL, size.unit = "pt", color = NULL, colour = NULL, alpha = NULL, face = NULL, fontface = NULL, angle = NULL, lineheight = NULL, math_family = NULL, inline = FALSE, margin = NULL, debug = NULL, inherit.blank = FALSE )
hjust, vjust
|
Horizontal and vertical justification for the rendered
grob ( |
scale |
A positive scaling factor applied to the rendered Typst size. |
size |
Optional font size. |
size.unit |
The unit of |
color, colour
|
Optional text color. RGB or color name are supported. |
alpha |
Optional color alpha multiplier in |
face, fontface
|
Optional math face. Only |
angle |
Optional text rotation angle in degrees. |
lineheight |
Optional line height value. May be negative. |
math_family |
Optional font family for math content. The default math font is |
inline |
Whether to render inline math. Default |
margin |
Optional text margins created with |
debug |
If |
inherit.blank |
If |
A ggplot2 theme element.
element_typst(), element_math_mitex()
ggplot(mtcars, aes(wt, mpg)) + geom_point() + labs( title = r"("Matrix Title:" mat(0, 1; 1, 0))", y = r"($sum_(i=1)^n c_i$)" ) + theme_minimal() + theme( plot.title = element_math_typst(size = 18, face = "bold"), axis.title.y = element_math_typst(size = 14) )ggplot(mtcars, aes(wt, mpg)) + geom_point() + labs( title = r"("Matrix Title:" mat(0, 1; 1, 0))", y = r"($sum_(i=1)^n c_i$)" ) + theme_minimal() + theme( plot.title = element_math_typst(size = 18, face = "bold"), axis.title.y = element_math_typst(size = 14) )
element_typst() is a more powerful replacement for ggplot2::element_text() and ggtext::element_markdown() that
renders each theme element through Typst. Use it for plot titles, subtitles,
axis titles, axis text, facet strips, legend text, and other theme slots that
accept text elements.
element_typst( hjust = NULL, vjust = NULL, scale = NULL, size = NULL, size.unit = "pt", color = NULL, colour = NULL, alpha = NULL, face = NULL, fontface = NULL, angle = NULL, lineheight = NULL, family = NULL, math_family = NULL, margin = NULL, debug = NULL, inherit.blank = FALSE )element_typst( hjust = NULL, vjust = NULL, scale = NULL, size = NULL, size.unit = "pt", color = NULL, colour = NULL, alpha = NULL, face = NULL, fontface = NULL, angle = NULL, lineheight = NULL, family = NULL, math_family = NULL, margin = NULL, debug = NULL, inherit.blank = FALSE )
hjust, vjust
|
Horizontal and vertical justification for the rendered
grob ( |
scale |
A positive scaling factor applied to the rendered Typst size. |
size |
Optional font size. |
size.unit |
The unit of |
color, colour
|
Optional text color. RGB or color name are supported. |
alpha |
Optional color alpha multiplier in |
face, fontface
|
Optional text face: |
angle |
Optional text rotation angle in degrees. |
lineheight |
Optional line height value. May be negative. |
family |
Optional text font family. The family must be available to Typst. If |
math_family |
Optional font family for math content. The default math font is |
margin |
Optional text margins created with |
debug |
If |
inherit.blank |
If |
A ggplot2 theme element.
element_math_typst(), element_math_mitex()
ggplot(mtcars, aes(wt, mpg)) + geom_point() + labs( title = r"(*Typst Title* #linebreak() with inline math $E = m c^2$)", x = r"(X-axis powered by _Typst_: $hat(y) = beta_0 + beta_1 x$)" ) + theme_minimal() + theme( plot.title = element_typst(size = 18, color = "red"), axis.title.x = element_typst(size = 14, color = "#40a2b4") )ggplot(mtcars, aes(wt, mpg)) + geom_point() + labs( title = r"(*Typst Title* #linebreak() with inline math $E = m c^2$)", x = r"(X-axis powered by _Typst_: $hat(y) = beta_0 + beta_1 x$)" ) + theme_minimal() + theme( plot.title = element_typst(size = 18, color = "red"), axis.title.x = element_typst(size = 14, color = "#40a2b4") )
geom_math_mitex() is the LaTeX-input companion to geom_math_typst() that
renders data-driven LaTeX-style math labels by converting each label to Typst
through MiTeX before rendering. It will
normalize the outer $...$ or $$...$$ delimiters automatically.
This allows you to use LaTeX math syntax in your plot, which you may be more familiar with :).
geom_math_mitex( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., nudge_x = 0, nudge_y = 0, inline = FALSE, size.unit = "pt", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )geom_math_mitex( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., nudge_x = 0, nudge_y = 0, inline = FALSE, size.unit = "pt", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
... |
Other arguments passed on to
|
nudge_x, nudge_y
|
Horizontal and vertical nudge offsets. Supply these
instead of |
inline |
Whether to render inline math. Default |
size.unit |
The unit of |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
MiTeX is a LaTeX-to-Typst converter. It should be stable and reliable for typical LaTeX math expressions. If you find any LaTeX math that MiTeX fails to convert properly, you can report an issue to ggtypst first.
A ggplot2 layer that can be added to a plot.
geom_typst(), geom_math_typst(), ggplot2::geom_label()
math_labels <- data.frame( wt = c(2.1, 3.2, 4.8), mpg = c(32, 24, 16), label = c( r"(\sum_{i=1}^n x_i)", r"(x^2 + y^2)", r"(\int_0^1 x \, dx)" ) ) ggplot(math_labels, aes(wt, mpg, label = label)) + geom_point() + geom_math_mitex(nudge_y = 1, size = 14, show.legend = FALSE) + theme_minimal()math_labels <- data.frame( wt = c(2.1, 3.2, 4.8), mpg = c(32, 24, 16), label = c( r"(\sum_{i=1}^n x_i)", r"(x^2 + y^2)", r"(\int_0^1 x \, dx)" ) ) ggplot(math_labels, aes(wt, mpg, label = label)) + geom_point() + geom_math_mitex(nudge_y = 1, size = 14, show.legend = FALSE) + theme_minimal()
geom_math_typst() is the math-specialized variant of geom_typst() that
renders data-driven Typst math labels. Each label is treated as Typst math
expression. It will normalize the outer $...$ or $ ... $ delimiters
automatically.
geom_math_typst( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., nudge_x = 0, nudge_y = 0, inline = FALSE, size.unit = "pt", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )geom_math_typst( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., nudge_x = 0, nudge_y = 0, inline = FALSE, size.unit = "pt", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
... |
Other arguments passed on to
|
nudge_x, nudge_y
|
Horizontal and vertical nudge offsets. Supply these
instead of |
inline |
Whether to render inline math. Default |
size.unit |
The unit of |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
A ggplot2 layer that can be added to a plot.
geom_typst(), geom_math_mitex(), ggplot2::geom_label()
math_labels <- data.frame( wt = c(2.1, 3.2, 4.8), mpg = c(32, 24, 16), label = c(r"(sum_(i=1)^n x_i)", r"(x^2 + y^2)", r"(integral_0^1 x dif x)") ) ggplot(math_labels, aes(wt, mpg, label = label)) + geom_point() + geom_math_typst(nudge_y = 1, size = 14, show.legend = FALSE) + theme_minimal()math_labels <- data.frame( wt = c(2.1, 3.2, 4.8), mpg = c(32, 24, 16), label = c(r"(sum_(i=1)^n x_i)", r"(x^2 + y^2)", r"(integral_0^1 x dif x)") ) ggplot(math_labels, aes(wt, mpg, label = label)) + geom_point() + geom_math_typst(nudge_y = 1, size = 14, show.legend = FALSE) + theme_minimal()
geom_typst() is a more powerful replacement for ggplot2::annotate() and
ggtext::geom_richtext() that renders a vector of raw Typst labels at data
positions. Each label is compiled independently with Typst. Parameter and
aesthetic names follow the conventions of ggplot2::geom_label().
geom_typst( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., nudge_x = 0, nudge_y = 0, size.unit = "pt", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )geom_typst( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., nudge_x = 0, nudge_y = 0, size.unit = "pt", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
... |
Other arguments passed on to
|
nudge_x, nudge_y
|
Horizontal and vertical nudge offsets. Supply these
instead of |
size.unit |
The unit of |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
A ggplot2 layer that can be added to a plot.
geom_typst() understands the following aesthetics (required aesthetics are
in bold):
x
y
label Raw Typst source code. label can be mapped in ggplot2::aes() or supplied as a constant value, for example geom_typst(label = "Hello"), to render the same Typst label for every row in the layer.
hjust
vjust
scale
size
colour (alias color)
alpha
face (alias fontface)
angle
lineheight
family
math_family
To learn more about these theme arguments, see annotate_typst(). If mapped
colour values are already literal colours such as "#1E66F5", add
ggplot2::scale_colour_identity() so ggplot2 treats them as colours instead
of discrete categories.
geom_math_typst(), geom_math_mitex(), ggplot2::geom_label()
labels <- data.frame( wt = c(2, 3.5, 4.5), mpg = c(15, 30, 10), label = c( r"(*Toyota Corolla* 😂)", r"(*Fiat 128*, $y = x$)", r"(*_Maserati Bora_*)" ), colour = c("blue", NA, "red") ) ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_typst( data = labels, aes(wt, mpg, label = label, colour = colour), scale = 1.1, size = 14, show.legend = FALSE ) + scale_colour_identity() + theme_minimal()labels <- data.frame( wt = c(2, 3.5, 4.5), mpg = c(15, 30, 10), label = c( r"(*Toyota Corolla* 😂)", r"(*Fiat 128*, $y = x$)", r"(*_Maserati Bora_*)" ), colour = c("blue", NA, "red") ) ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_typst( data = labels, aes(wt, mpg, label = label, colour = colour), scale = 1.1, size = 14, show.legend = FALSE ) + scale_colour_identity() + theme_minimal()