| math_grob | R Documentation |
This function creates a grid grob that renders a math expression.
The grob can be used with standard grid functions like grid::grid.draw(),
combined with other grobs using grid::grobTree(), or used in ggplot2
annotations. Like the standard grid grobs, math_grob() creates a grob
object to pass around while grid.math() both creates the grob and render it
in one pass.
math_grob(
math,
x = 0.5,
y = 0.5,
hjust = "left",
vjust = "baseline",
default.units = "npc",
name = NULL,
gp = NULL,
vp = NULL,
family = "math",
size = 12,
res = 72,
inline = TRUE,
path = NULL,
index = 0,
color = "black"
)
grid.math(
math,
x = 0.5,
y = 0.5,
hjust = "left",
vjust = "baseline",
default.units = "npc",
name = NULL,
gp = NULL,
vp = NULL,
family = "math",
size = 12,
res = 72,
inline = TRUE,
path = NULL,
index = 0,
color = "black",
draw = TRUE
)
math |
A single math expression string. Use raw strings for
cleaner syntax, e.g., |
x, y |
The position of the grob. Can be a unit object or a numeric value (interpreted as npc units). |
hjust |
Horizontal justification. Either a numeric value (0 = left,
0.5 = center, 1 = right) or a character string ( |
vjust |
Vertical justification. Either a numeric value (0 = bottom,
0.5 = center, 1 = top) or a character string ( |
default.units |
A string indicating the default units to use if
|
name |
A character identifier for the grob. |
gp |
A |
vp |
A |
family |
Font family name. For best results, use a font with an OpenType MATH table such as "STIX Two Math", "Latin Modern Math", "Cambria Math", or "Libertinus Math". Fonts without a MATH table will use fallback positioning constants based on TeX defaults, which produces reasonable but less precise results. Default uses the system's default math font. |
size |
The size in points to use for the font |
res |
The resolution to use when doing the shaping. Should optimally match the resolution used when rendering the glyphs. |
inline |
Logical. If |
path, index |
path an index of a font file to circumvent lookup based on family and style |
color |
Base color for the equation (default "black"). Individual
elements can override this using |
draw |
Should the created grob be rendered |
A grob object of class "math_grob".
# Create a math grob (baseline at y=0.5)
g <- math_grob(r"(\frac{a}{b})", x = 0.5, y = 0.5)
# Draw it (requires a device that supports glyph rendering)
try(grid::grid.draw(g))
# Center the expression in the viewport
g <- math_grob(r"(E = mc^2)", x = 0.5, y = 0.5,
hjust = "center", vjust = "center")
try(grid::grid.draw(g))
# Combine with other grobs
tree <- grid::grobTree(
grid::rectGrob(gp = grid::gpar(fill = "lightblue")),
math_grob(r"(E = mc^2)", x = 0.5, y = 0.5,
hjust = "center", vjust = "center")
)
try(grid::grid.draw(tree))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.