element_latex: A ggplot2 theme element for LaTeX text

View source: R/ggplot2-integration.R

element_latexR Documentation

A ggplot2 theme element for LaTeX text

Description

Use this as a theme element for axis titles, axis labels, plot titles, or any other text element in a ggplot2 theme. The text string is parsed as LaTeX math and rendered via MicroTeX.

Usage

element_latex(
  math_font = "",
  fontsize = NULL,
  lineheight = 1.2,
  max_width = 0,
  input_mode = c("mixed", "math"),
  render_mode = c("typeface", "path"),
  ...
)

Arguments

math_font

Name of the math font to use (e.g., "stix"). Use "" (default) for Lete Sans Math, which pairs with R's default sans-serif text font. See available_math_fonts for loaded fonts.

fontsize

Convenience alias for size; when supplied, it is forwarded to ggplot2::element_text() as the text size in points. If NULL (default), the theme's inherited size is used.

lineheight

Multi-line height multiplier (default 1.2), matching grid::gpar() semantics.

max_width

Numeric maximum width in big points for automatic line wrapping. Use 0 (default) for no wrapping.

input_mode

How tex is interpreted before being parsed. "mixed" wraps the input in \text{...} so the string reads as ordinary text and $...$ (or \(...\)) opens math mode, matching document-level LaTeX semantics. Useful for labels that arrive from external sources mixing prose and math without explicit \text{} markers. "math" (default) is the standard MicroTeX behaviour — the whole string is treated as math, so unwrapped prose renders as spaced math italics. The default can be changed globally via latex_options(input_mode = "mixed"). See latex_wrap for details on the wrapping process.

render_mode

Character string: "typeface" (default) renders glyphs as native text using the math font, producing selectable/accessible text in PDF and SVG output. Bundled math fonts and any registered via load_font are read directly from their OTF files — no system-wide font install is required. Falls back to path mode automatically on devices that lack the R \geq 4.3 glyph engine (e.g., the base pdf() device). For selectable PDF output, prefer cairo_pdf. "path" renders math symbols as filled vector paths (works on all devices but text is not selectable in PDF/SVG).

...

Additional arguments passed to ggplot2::element_text() (e.g., size, colour, hjust).

Details

Dollar signs ($...$) in the label text are stripped automatically so that both "\frac{a}{b}" and "$\frac{a}{b}$" work.

This element is an S7 subclass of ggplot2::element_text, so it inherits all standard text properties (size, colour, hjust, etc.) from the theme and supports merge_element() correctly.

Value

An S7 object of class element_latex, inheriting from ggplot2::element_text.

Examples


if (requireNamespace("ggplot2", quietly = TRUE)) {
  library(ggplot2)
  ggplot(mtcars, aes(wt, mpg)) + geom_point() +
    labs(x = "$\\beta_1 \\cdot x + \\beta_0$") +
    theme(axis.title.x = element_latex())
}


gridmicrotex documentation built on May 16, 2026, 5:06 p.m.