| 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 |
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 |
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 |
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))
}
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 |
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:
Word spaces binary operators and fractions a little less than TeX: about 0.17 em less around each binary operator (2 points at 12 points, as in
a + b), and no space of the null delimiters around fractions (0.12 em on each side). Equations with such operators or fractions are a few percent wider than in Word;some equations are given a little more space than in Word, which is harmless when the size is used to set the width of a column: the height of matrices in parentheses (up to 20%), the width of matrices in brackets (about 9%), the height of names of functions with an exponent, such as
\sin^2 x(about 20%), the width of\left( ... \right)around a fraction (up to 15%);the size of scripts is an upper bound: Word rounds it down to a half point and displays equations with scripts up to about 4% narrower; 'Microsoft PowerPoint' does not use the script style glyphs of the font and displays them up to about 12% narrower;
Word uses the font 'Cambria Math' unless the document sets another math font: the equations are then about 5% wider;
browsers displaying the HTML output of 'KaTeX' use the fonts of 'KaTeX': the sizes differ by about 7%. The environment
arrayis also 1 em wider in browsers (and in LaTeX), which add half the space between columns before its first column and after its last one; Word does not, and neither does this function;a line break (
\\or\newline) in the row of the equation starts a new line, aligned on the left, as in the HTML output of 'KaTeX'; the baseline of the equation is the baseline of its first line. In a group ({a \\ b}) or a fraction, it is ignored, as in 'KaTeX'. LaTeX ignores these line breaks in math mode. Word centers the lines and, in a sentence, puts the middle of the lines on the baseline of the text;Word ignores the sizing commands (
\tinyto\Huge), the sizes of\bigto\Bigg(their delimiters keep the size of the text),\raiseboxand\pmb: the sizes given for equations using them are those of 'KaTeX', not of Word;in display style, limits asked with
\limitsin a fraction or a script become scripts, as the limits of\sum: 'KaTeX' writes both in the same way;the number given by
\tagis neither measured nor drawn: it belongs to the document, not to the equation. Browsers put it in the right margin, Word after the equation.
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 ( |
strict |
KaTeX option.If |
rpr |
run properties to apply to the 'Office Open XML Math' equations,
used only when |
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")