Package {gridmicrotex}


Title: Native 'LaTeX' Math Rendering for Grid Graphics
Version: 0.0.1
Description: Renders 'LaTeX' math equations as native R grid graphics objects (grobs) using the 'MicroTeX' 'C++' library as the layout engine. Produces resolution-independent vector output that works on any R graphics device, with no external 'LaTeX' installation required.
License: MIT + file LICENSE
Encoding: UTF-8
RoxygenNote: 7.3.3
SystemRequirements: C++17, FreeType (>= 2.9), pkg-config
Depends: R (≥ 4.2.0)
LinkingTo: Rcpp, systemfonts
Imports: grDevices, grid, Rcpp, systemfonts, tools
Suggests: ggplot2 (≥ 4.0.0), knitr, ragg, rmarkdown, S7, testthat (≥ 3.0.0), vdiffr
VignetteBuilder: knitr
Config/testthat/edition: 3
URL: https://github.com/adayim/gridmicrotex, https://adayim.github.io/gridmicrotex/
BugReports: https://github.com/adayim/gridmicrotex/issues
NeedsCompilation: yes
Packaged: 2026-05-05 13:49:14 UTC; alim
Author: Alim Dayim ORCID iD [aut, cre], Nano Michael [cph] (Author of included 'MicroTeX' library), Bundled math font authors [cph] (See inst/COPYRIGHTS for the full list of authors of the bundled math fonts.)
Maintainer: Alim Dayim <ad938@cam.ac.uk>
Repository: CRAN
Date/Publication: 2026-05-08 15:00:02 UTC

gridmicrotex: Native 'LaTeX' Math Rendering for Grid Graphics

Description

Renders 'LaTeX' math equations as native R grid graphics objects (grobs) using the 'MicroTeX' 'C++' library as the layout engine. Produces resolution-independent vector output that works on any R graphics device, with no external 'LaTeX' installation required.

Author(s)

Maintainer: Alim Dayim ad938@cam.ac.uk (ORCID)

Other contributors:

See Also

Useful links:


Build a single batched glyphGrob from collected glyph data

Description

Creates a grid::glyphGrob containing all math glyphs, using grDevices::glyphInfo() to describe glyph IDs, positions, sizes, colors, and fonts. Each unique font file becomes an entry in the glyphFontList.

Usage

.build_glyph_grob(ids, x, y, sizes, cols, font_files, depth = 0)

Arguments

ids

Integer vector of glyph IDs.

x, y

Numeric vectors of glyph positions (already y-flipped, in bigpts).

sizes

Numeric vector of font sizes.

cols

Character vector of colors.

font_files

Character vector of font file paths.

depth

Depth below the baseline in bigpts (default 0).

Value

A grid::glyphGrob or NULL.


Get or create a glyphFont object for a font file

Description

Caches grDevices::glyphFont() objects keyed by file path so that repeated calls for the same font file reuse the same object.

Usage

.get_glyph_font(font_file)

Arguments

font_file

Absolute path to the OTF/TTF font file.

Value

A glyphFont object.


Create a text measurement closure for MicroTeX layout

Description

Returns a function that measures text using R's grid graphics system. The closure is called from C++ during parse_latex_cpp() to get accurate font metrics for \text{} blocks.

Usage

.make_text_measurer(text_gp)

Arguments

text_gp

A gpar object with font settings (fontfamily, fontface) to use for measurement.

Value

A function taking (text, font_style) that returns c(width_ratio, ascent_ratio, height_ratio) where ratios are relative to the font size.


Resolve MicroTeX FontStyle bitmask to R font face

Description

MicroTeX FontStyle: rm=1, bf=2, it=4, etc. (bitmask).

Usage

.resolve_text_face(style)

Arguments

style

Integer font style bitmask.

Value

Character: "plain", "bold", "italic", or "bold.italic".


List available math fonts

Description

Returns the names of all math fonts currently loaded by MicroTeX. These names can be passed to the math_font parameter of latex_grob and grid.latex.

Usage

available_math_fonts()

Value

A character vector of math font names.

Font pairing

The bundled math fonts have different styles. For a consistent look, pair them with a matching fontfamily in gp:

Math font Style Suggested text font
Lete Sans Math ("lete", default) Sans-serif "sans"
STIX Two Math ("stix") Serif "serif"

Additional math fonts can be loaded with load_font.

Examples

available_math_fonts()

Convert big points to TeX points

Description

Convert big points to TeX points

Usage

bigpt_to_tex_pt(big_pt)

Arguments

big_pt

Numeric value in PostScript points.

Value

Numeric value in TeX points.


Build grid children from a MicroTeX layout data.frame

Description

Converts each row of the layout data.frame into the appropriate grid grob (pathGrob, segmentsGrob, rectGrob, textGrob, glyphGrob).

Usage

build_latex_children(
  layout_df,
  total_h,
  depth = 0,
  text_gp = NULL,
  render_mode = "typeface"
)

Arguments

layout_df

Data.frame returned by parse_latex_cpp().

total_h

Total height of the formula (height + depth) in bigpts.

depth

Depth below the baseline in bigpts (default 0).

text_gp

Optional gpar for text grobs (from \text{} blocks). Controls fontfamily and fontface.

render_mode

Character string: "path" or "typeface". In typeface mode, glyph records are rendered via glyphGrob.

Value

A grid::gList of child grobs.


Convert path segments to a grid pathGrob

Description

Convert path segments to a grid pathGrob

Usage

build_path_grob(path_data, col, idx, total_h)

Arguments

path_data

List with cmd and coords elements.

col

Fill color.

idx

Index for naming.

total_h

Total height for y-axis flipping.

Value

A grid::pathGrob or NULL.


Check font status

Description

Reports which math fonts are loaded and available for rendering. Shows the MicroTeX version, loaded math fonts, and whether bundled font files are present.

Usage

check_fonts()

Value

Invisibly returns the character vector of available font names.

Examples

check_fonts()

Approximate a cubic bezier curve with line segments

Description

Approximate a cubic bezier curve with line segments

Usage

cubic_bezier(x0, y0, x1, y1, x2, y2, x3, y3, n = 16)

Define a user-level LaTeX macro

Description

Registers a zero-argument shorthand that is expanded by text substitution before the expression reaches the MicroTeX parser. Useful for domain-specific notation (e.g. \RR for \mathbb{R}) you reuse across many plots.

Usage

define_macro(name, definition)

clear_macros(name = NULL)

list_macros()

Arguments

name

Macro name without the leading backslash. For clear_macros, the macro name to drop, or NULL (default) to clear all.

definition

LaTeX source the macro expands to.

Value

See Also

latex_grob, latex_options

Examples


  define_macro("RR", "\\mathbb{R}")
  define_macro("eps", "\\varepsilon")
  grid::grid.newpage()
  grid.latex("\\forall \\eps > 0, \\eps \\in \\RR")
  clear_macros()


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("math", "mixed"),
  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())
}


A ggplot2 geom for LaTeX math labels

Description

Renders LaTeX math expressions as native grid grobs within a ggplot2 plot. Each label is parsed and laid out by MicroTeX, producing resolution-independent vector output.

Usage

geom_latex(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  position = "identity",
  ...,
  fontsize = 11,
  math_font = "",
  lineheight = 1.2,
  max_width = 0,
  input_mode = c("math", "mixed"),
  render_mode = c("typeface", "path"),
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

Arguments

mapping

Set of aesthetic mappings created by aes(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data. A function can be created from a formula (e.g. ~ head(.x, 10)).

stat

The statistical transformation to use on the data for this layer. When using a ⁠geom_*()⁠ function to construct a layer, the stat argument can be used to override the default coupling between geoms and stats. The stat argument accepts the following:

  • A Stat ggproto subclass, for example StatCount.

  • A string naming the stat. To give the stat as a string, strip the function name of the stat_ prefix. For example, to use stat_count(), give the stat as "count".

  • For more information and other ways to specify the stat, see the layer stat documentation.

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 position argument accepts the following:

  • The result of calling a position function, such as position_jitter(). This method allows for passing extra arguments to the position.

  • A string naming the position adjustment. To give the position as a string, strip the function name of the position_ prefix. For example, to use position_jitter(), give the position as "jitter".

  • For more information and other ways to specify the position, see the layer position documentation.

...

Other arguments passed to layer.

fontsize

Default font size in points. Overridden by the size aesthetic if mapped.

math_font

Name of the math font to use (e.g., "stix").

lineheight

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

max_width

Maximum width in big points for automatic line wrapping (default: 0, 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).

na.rm

If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display. To include legend keys for all levels, even when no data exists, use TRUE. If NA, all levels are shown in legend, but unobserved levels are omitted.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. annotation_borders().

Value

A ggplot2 layer.

Aesthetics

geom_latex() understands the following aesthetics (required aesthetics are in bold):

Examples


if (requireNamespace("ggplot2", quietly = TRUE)) {
  library(ggplot2)
  df <- data.frame(
    x = 1:3, y = 1:3,
    eq = c("x^2", "\\frac{a}{b}", "\\sum_{i=1}^n x_i")
  )
  ggplot(df, aes(x, y, label = eq)) + geom_latex()

  # Use annotate() for single annotations (no legend, no data frame needed)
  ggplot(mtcars, aes(wt, mpg)) + geom_point() +
    annotate("latex", x = 4, y = 30,
             label = "\\hat{y} = \\beta_0 + \\beta_1 x")
}


Set the maximum number of entries kept in the LaTeX layout cache

Description

The cache stores parsed layout information for recently rendered LaTeX expressions, keyed by the expression and relevant rendering parameters (font, size, macros, etc.). This speeds up repeated rendering of the same expressions, especially in loops or interactive sessions. The default limit is 512 entries, which should be sufficient for most use cases. When the limit is exceeded, the least recently used entries are automatically evicted.

Usage

latex_cache_limit(n = 512L)

latex_cache_clear()

latex_cache_info()

Arguments

n

Non-negative integer cache capacity. Default is 512. Set to 0 to disable caching.

Value

See Also

latex_grob, latex_options

Examples


  latex_cache_limit(256)
  grid.latex("e^{i\\pi} + 1 = 0")
  latex_cache_info()
  latex_cache_clear()


Get dimensions of a LaTeX expression

Description

Get dimensions of a LaTeX expression

Usage

latex_dims(
  tex,
  math_font = "",
  max_width = 0,
  tex_style = "",
  input_mode = c("mixed", "math"),
  render_mode = c("typeface", "path"),
  gp = grid::gpar()
)

Arguments

tex

Character string of LaTeX math code.

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.

max_width

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

tex_style

Character: TeX style override. One of "" (default; let the parser decide), "display", "text", "script", or "scriptscript". See latex_grob for the semantics of each value.

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

gp

Graphical parameters (see gpar). Common entries: col (formula foreground), fontfamily / fontface (text font), fontsize / cex (formula size), and lineheight (multi-line spacing). See latex_grob for how each of these flows through MicroTeX.

Value

A list with the following elements:

Examples

latex_dims("\\frac{a}{b}")

Create a grid grob from a LaTeX expression

Description

Parses a LaTeX math expression and returns a grid grob object that renders the formula using native grid graphics primitives. The grob supports standard grid queries such as grobWidth(), grobHeight(), grobX(), and grobY().

A convenience wrapper that creates a latex_grob and immediately draws it on the current device via grid.draw.

Usage

latex_grob(
  tex,
  x = grid::unit(0.5, "npc"),
  y = grid::unit(0.5, "npc"),
  default.units = "npc",
  hjust = 0.5,
  vjust = 0.5,
  rot = 0,
  math_font = "",
  max_width = 0,
  tex_style = "",
  input_mode = c("mixed", "math"),
  render_mode = c("typeface", "path"),
  debug = FALSE,
  name = NULL,
  gp = grid::gpar()
)

grid.latex(tex, ...)

Arguments

tex

Character string of LaTeX math code.

x, y

Position in grid coordinates.

default.units

Units for x, y if given as numeric.

hjust, vjust

Horizontal/vertical justification (0-1).

rot

Rotation angle in degrees, counter-clockwise (default: 0). Matches the rot parameter of textGrob.

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.

max_width

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

tex_style

Character: TeX style override. One of "" (default; let the parser decide), "display", "text", "script", or "scriptscript". See latex_grob for the semantics of each value.

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

debug

Logical; if TRUE, draws diagnostic overlays on the grob — the full bounding box (dashed gray), the baseline (solid red), the depth line (dashed gray), and a small dot at each MicroTeX draw record's origin. Useful for checking positioning and diagnosing vertical alignment.

name

Optional grob name.

gp

Graphical parameters (see gpar). Common entries: col (formula foreground), fontfamily / fontface (text font), fontsize / cex (formula size), and lineheight (multi-line spacing). See latex_grob for how each of these flows through MicroTeX.

...

Additional arguments passed to latex_grob.

Details

Controlling TeX style with tex_style

tex_style selects the size-and-spacing regime MicroTeX applies to the whole expression. It changes the style (display vs. text), not the font size — size is always set via gp$fontsize / gp$cex; style-dependent shrinking (for "script" and "scriptscript") is applied on top of that size.

tex_style applies to the entire expression. To override the style of a sub-expression from within tex, use the inline TeX commands ⁠\displaystyle⁠, ⁠\textstyle⁠, ⁠\scriptstyle⁠, or ⁠\scriptscriptstyle⁠.

Graphical parameters (gp)

Value

A grid grob of class "latexgrob".

Invisibly returns the grob.

See Also

grid.latex, latex_dims, geom_latex, available_math_fonts, latex_wrap, latex_options

Examples


  g <- latex_grob("\\frac{a}{b}", gp = grid::gpar(fontsize = 30))
  grid::grid.draw(g)

  # Red formula
  grid::grid.draw(latex_grob("x^{2}", gp = grid::gpar(col = "red")))

  # Rotated formula
  grid::grid.draw(latex_grob("x^{2} + y^{2}",
                             gp = grid::gpar(fontsize = 24), rot = 45))


  grid.latex("x^{2} + y^{2} = z^{2}")


Set or query package-wide LaTeX rendering defaults

Description

A single entry point for project-wide defaults used by latex_grob, grid.latex, latex_dims, and latex_tree. Options set here are applied only when the corresponding argument is not supplied at the call site, so explicit arguments always win.

Usage

latex_options(
  math_font = NULL,
  render_mode = NULL,
  tex_style = NULL,
  input_mode = NULL
)

reset_latex_options()

Arguments

math_font

Math font name or alias (see available_math_fonts).

render_mode

Either "typeface" or "path".

tex_style

TeX style override. One of "" (let the parser decide), "display", "text", "script", or "scriptscript". "display" forces large operators with limits placed over/under, useful for inline labels that should still look like display equations.

input_mode

How the input string is interpreted before being handed to MicroTeX. "math" (default) treats the whole string as math — the standard MicroTeX behaviour, where letters render as math italics and unwrapped prose looks wrong. "mixed" wraps the string in \text{...} so it reads as ordinary text, with $...$ (and \(...\)) opening math mode — the document-level LaTeX convention. Useful when consuming labels from other packages that mix prose and math without explicit \text{} markers.

Details

Calling latex_options() with no arguments returns the current settings (a list whose NULL entries mean "use the built-in default"). Supply one or more named arguments to update them.

Font size and line spacing are controlled via gp parameters (fontsize, cex, lineheight) at the grob level — see latex_grob.

Value

Invisibly returns the previous settings (a list). With no arguments, returns the current settings visibly.

See Also

available_math_fonts, latex_grob

Examples


  latex_options(math_font = "stix", render_mode = "typeface")
  grid.latex("\\sum_{i=1}^{n} i^{2}", gp = grid::gpar(fontsize = 14))
  reset_latex_options()


Inspect the parsed layout of a LaTeX expression

Description

Returns the raw draw-record table produced by MicroTeX's layout pass together with the bounding-box metadata. Useful for debugging alignment issues, building custom grobs on top of the layout, or counting glyphs/paths/rules in a formula.

Usage

latex_tree(
  tex,
  math_font = "",
  max_width = 0,
  tex_style = "",
  input_mode = c("mixed", "math"),
  render_mode = c("typeface", "path"),
  gp = grid::gpar()
)

Arguments

tex

Character string of LaTeX math code.

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.

max_width

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

tex_style

Character: TeX style override. One of "" (default; let the parser decide), "display", "text", "script", or "scriptscript". See latex_grob for the semantics of each value.

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

gp

Graphical parameters (see gpar). Common entries: col (formula foreground), fontfamily / fontface (text font), fontsize / cex (formula size), and lineheight (multi-line spacing). See latex_grob for how each of these flows through MicroTeX.

Value

A list with class "latex_tree" containing:

records

Data frame of draw records (one row per glyph, path, line, rect, or text block). Columns include type, x, y, glyph, font_size, color, text, codepoint, font_file.

bbox

Named numeric vector with width, height, depth, baseline (all in big points).

tex

The (macro-expanded) input string.

render_mode

Rendering mode used for the layout.

See Also

latex_grob, latex_dims

Examples


  tree <- latex_tree("\\frac{a}{b}")
  print(tree)
  head(tree$records)


Wrap standard text for math-first LaTeX renderers

Description

Parses character strings to safely isolate standard natural language from LaTeX math environments. Standard text is wrapped in ⁠\text{}⁠ blocks, while equations, display math, and specific LaTeX environments are preserved verbatim. This is heavily optimized for passing mixed-content strings (like plot titles or axis labels) to pure-math typesetting engines like MicroTex. The conversion is not perfect, but it should handle most common cases without user intervention.

Usage

latex_wrap(tex, input_mode = c("mixed", "math"))

Arguments

tex

character. The string or vector of strings to be processed.

input_mode

character. A length-one character vector dictating the parsing strategy. If "mixed" (default), the string is tokenized and text is wrapped. If "math", the parser is bypassed and the string is returned unmodified, assuming the user has provided a pure math equation.

Details

latex_wrap() operates as a state-machine tokenizer to ensure that valid LaTeX math is not corrupted by the text-wrapping process. It features:

Value

A character vector of the same length as tex, formatted for math-mode LaTeX rendering.

Examples

# "mixed" mode (default) safely wraps text and preserves inline math
latex_wrap(r"(The equation \(E=mc^2\) is famous)")

# "mixed" mode handles user-escaped characters seamlessly
latex_wrap(r"(Cost: \$100 for $x$ items)")

# "mixed" mode converts R newlines to stacked text blocks
latex_wrap(r"(Line 1\nLine 2)")

# "math" mode returns the string completely unmodified
latex_wrap(r"(\frac{\alpha}{\beta})", input_mode = "math")

Load a math font from an OTF file

Description

Loads an OTF/TTF math font into MicroTeX's internal font registry. The font's OpenType MATH table is parsed directly in C++ and the required metrics are synthesised on the fly. You can download free math fonts like Latin Modern Math (default math fonts in LaTeX) and load it with load_font() to use it for math rendering.

Usage

load_font(otf_path)

Arguments

otf_path

Path to the OTF/TTF font file.

Details

The font is also registered with the systemfonts package so it can be selected for surrounding plot text via gp = gpar(fontfamily = "...") without being installed system-wide.

Value

Invisibly returns NULL.

Text fonts

This function is only for math fonts (fonts with an OpenType MATH table). Plain text fonts used inside \text{} blocks are resolved automatically by systemfonts from the gp$fontfamily argument — no load_font() call required.

See Also

available_math_fonts, latex_options, latex_grob

Examples


  # We will download and load Latin Modern Math
  url <- "https://mirrors.ctan.org/fonts/lm-math/opentype/latinmodern-math.otf"
  math_fnt <- file.path(tempdir(), "latinmodern-math.otf")
  download.file(url = url, destfile = math_fnt, mode = "wb")
  load_font(math_fnt)

  available_math_fonts()




Approximate a quadratic bezier curve with line segments

Description

Approximate a quadratic bezier curve with line segments

Usage

quad_bezier(x0, y0, x1, y1, x2, y2, n = 12)

Resolve a math font name

Description

Translates short aliases (e.g., "stix", "lete") to the full MicroTeX font name. Validates that the font is loaded.

Usage

resolve_math_font(name)

Arguments

name

Font name or alias. Empty string uses the default font.

Value

The resolved font name.


Convert TeX points to big points

Description

TeX points (1/72.27 inch) differ from PostScript/big points (1/72 inch). R's grid "bigpts" unit uses PostScript points.

Usage

tex_pt_to_bigpt(tex_pt)

Arguments

tex_pt

Numeric value in TeX points.

Value

Numeric value in big (PostScript) points.