The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

Package {equatags}


Type: Package
Title: Convert, Measure and Draw 'LaTeX' Equations
Version: 0.4.0
Description: Transform 'LaTeX' math expressions into 'HTML' or 'Office Open XML Math', to include them in 'HTML' pages, 'Microsoft Word' documents or 'Microsoft PowerPoint' presentations with a 'Markdown' document or the R package 'officer'. Compute the size of equations and draw them with 'grid', with the rules of 'TeX' and the parameters of an 'OpenType' math font, independently of the graphic device.
License: MIT + file LICENSE
URL: https://ardata-fr.github.io/equatags/, https://github.com/ardata-fr/equatags
BugReports: https://github.com/ardata-fr/equatags/issues
Depends: R (≥ 4.0.0)
Imports: gdtools (≥ 0.6.0), grid, katex, systemfonts (≥ 1.2.0), xml2, xslt
Suggests: testthat (≥ 3.0.0)
Config/Needs/website: flextable, ggplot2, rmarkdown
Config/roxygen2/version: 8.1.1
Config/testthat/edition: 3
Encoding: UTF-8
NeedsCompilation: no
Packaged: 2026-11-15 18:08:25 UTC; davidgohel
Author: David Gohel [aut, cre], ArData [cph]
Maintainer: David Gohel <david.gohel@ardata.fr>
Repository: CRAN
Date/Publication: 2026-11-15 22:10:02 UTC

Draw an equation with 'grid'

Description

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

equation_size()

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

Size of equations

Description

Compute the size of equations written in LaTeX, as they are displayed with an OpenType math font.

The equations are converted to MathML with 'KaTeX' (the MathML used by transform_mathjax()), then laid out with the rules of TeX and the constants of the 'MATH' table of the font: spaces between operators, fractions, scripts, limits of large operators, radicals, stretchy delimiters, matrices, accents. Words and spaces have the widths stored in the font file.

The result does not depend on a graphic device or an application, only on the font file. It is close to what 'Microsoft Word' displays with the same font (a few percent on usual equations), see the section "Differences with Word and browsers".

Usage

equation_size(x, font = gdtools::stixmath_file(), size = 12, display = FALSE)

Arguments

x

a character vector of LaTeX math expressions.

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.

Value

A data.frame with one row per equation and columns width, height (above the baseline) and depth (below the baseline), in inches. Equations using a construct that is not supported give NA values, with a warning.

Differences with Word and browsers

The layout follows the rules of TeX, as 'KaTeX' and LaTeX do. Compared with 'Microsoft Word' displaying the equation in display style with the same font, the median difference of width is about 1% on a set of usual equations. The known differences are:

See Also

equation_grob() to draw an equation with 'grid'.

Examples

equation_size(c("\\frac{a+b}{c}", "x^2 + \\sqrt{y_i}"))
equation_size("\\sum_{i=1}^{n} x_i", display = TRUE)

'MathJax' equation as 'HTML' or 'MathML'.

Description

Get 'HTML' or 'MathML' XML codes corresponding to the rendering of 'MathJax' equations.

Usage

transform_mathjax(
  x,
  to = "html",
  display = FALSE,
  strict = "ignore",
  rpr = NULL
)

Arguments

x

MathJax equations

to

output format, one of 'html' or 'mml'

display

should the equation be in display (TRUE) or inline mode (FALSE, by default)

strict

KaTeX option.If "ignore" (default), allow features that make writing LaTeX convenient. Other options are "warn" or "error". This happens E;G;, when you use accented characters in your equations.

rpr

run properties to apply to the 'Office Open XML Math' equations, used only when to = "mml": a ⁠<w:rPr>⁠ element for 'Microsoft Word' or an ⁠<a:rPr>⁠ element for 'Microsoft PowerPoint', as a string; the namespace declaration can be omitted. It sets for example the size or the color of the equations, that otherwise get the default values of the document. One value for all equations or one value per equation, NA for none. With NULL (default), no run properties are written.

Value

a character vector that contains 'html' or 'mml' codes corresponding to the equations.

Examples

x <- c("(ax^2 + bx + c = 0)",
       "x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.")
z <- transform_mathjax(x = x, to = "html")
cat(z, sep = "\n\n")
z <- transform_mathjax(x = x, to = "mml")
cat(z, sep = "\n\n")

# equations at 16 points in a Word document
z <- transform_mathjax(
  x = x, to = "mml",
  rpr = "<w:rPr><w:sz w:val=\"32\"/><w:szCs w:val=\"32\"/></w:rPr>"
)
cat(z, sep = "\n\n")

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.