math_grob: Create a grid grob for a math expression

View source: R/shape_math.R

math_grobR Documentation

Create a grid grob for a math expression

Description

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.

Usage

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
)

Arguments

math

A single math expression string. Use raw strings for cleaner syntax, e.g., r"(\\frac{a}{b})".

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 ("left", "center", "right").

vjust

Vertical justification. Either a numeric value (0 = bottom, 0.5 = center, 1 = top) or a character string ("bottom", "center", "top", "baseline"). The default "baseline" places the math baseline at the y position, which is most useful for aligning with text.

default.units

A string indicating the default units to use if x or y are only given as numeric values.

name

A character identifier for the grob.

gp

A grid::gpar() object containing graphical parameters, or NULL for defaults. Supported parameters are fontsize, col, and fontfamily.

vp

A grid::viewport() object (or NULL).

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 TRUE (default), uses inline style. If FALSE, uses display style (larger operators with limits above/below).

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 ⁠\textcolor{color}{content}⁠.

draw

Should the created grob be rendered

Value

A grob object of class "math_grob".

Examples


# 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))


textshaping documentation built on Nov. 20, 2026, 5:08 p.m.