Skip to contents

Create a grob drawing an equation written in LaTeX with an OpenType math font. The equation is laid out as by equation_size(); the glyphs are drawn with grid::glyphGrob(), fraction bars and other rules with rectangles, so the size of the drawing is the size computed.

The graphic device must support the drawing of glyphs (R >= 4.3, see dev.capabilities()$glyphs): 'ragg', 'svglite', pdf() and cairo_pdf() do.

The size of the grob is computed when it is created, from the font file only: widthDetails(), heightDetails(), ascentDetails() and descentDetails() return it in "bigpts" without measuring anything, and no graphic device is needed until the grob is drawn. To get the size without a device, use equation_size() (inches) or the numeric value of these units: grid::convertWidth() opens a device when none is open.

Usage

equation_grob(
  x,
  font = gdtools::stixmath_file(),
  size = 12,
  display = FALSE,
  x0 = 0.5,
  y0 = 0.5,
  hjust = 0.5,
  vjust = 0.5,
  colour = "black",
  default.units = "npc",
  name = NULL,
  vp = NULL
)

Arguments

x

a LaTeX math expression (a single string).

font

an OpenType math font, i.e. a font with a 'MATH' table: the path of a font file, or a family name such as "STIX Two Math", "Latin Modern Math", "Libertinus Math" or "Cambria Math", found with 'systemfonts' (fonts registered with systemfonts::register_font() can be used). The default is the file of 'STIX Two Math' shipped with 'gdtools' (gdtools::stixmath_file()): the results are then the same on every machine. Use font = "STIX Two Math" for the version installed on the system, if any.

size

font size in points.

display

if TRUE, use the display style (large operators with limits above and below, larger fractions), otherwise the inline style.

x0, y0

position of the equation, numeric values (in default.units) or units.

hjust, vjust

justification of the equation box relative to its position (0 is left or bottom, 1 is right or top).

colour

colour of the equation.

default.units

units of x0 and y0 when they are numeric.

name, vp

name and viewport of the grob.

Value

A grob of class equation_grob.

See also

Examples

if (getRversion() >= "4.3.0") {
  library(grid)
  grid.newpage()
  grid.draw(equation_grob("\\frac{a+b}{c} = \\sqrt{x^2 + y^2}", size = 24))
}