Working with Math environments in texor

This is a small sample article to demonstrate usage of the texor package to convert math environments.

Abhishek Ulayil (Student, Institute of Actuaries of India)
2025-09-01

1 Introduction

Math typesetting has always been a highlight of LaTeX, making it a de facto choice among academics and researchers globally. However, in the move to humble web interfaces, math has been traditionally hard to describe. There have been advances in JavaScript libraries to better typeset and present math in web pages but not all LaTeX commands/math functions are available.

MathJax

The texor package uses Mathjax version 3 to enhance the visual look of the math content in HTML.

The core goal of the MathJax project is the development of its state-of-the-art, open source, JavaScript platform for display of mathematics. The key advantages are (MathJax authors 2021):

As texor calls rmarkdown to render the R Markdown file into HTML, the rjtools::rjournal_web_article template uses MathJax as the math engine by default. We also specify the Mathjax version in the metadata of the generated Rmarkdown file.

2 Inline math

One can define inline math in LaTeX using commands \(..\) or $..$.

Table 1: Inline math syntax and its output side by side.
Command Math
\(\mu = (0,0,0)^\top \)

\(\mu = (0,0,0)^\top\)

$\mu = (0,0,0)^\top $

\(\mu = (0,0,0)^\top\)

3 Display math

Display math refers to equations typeset on separate lines rather than in line with the text. Almost all LaTeX equations can be rendered by MathJax.

\begin{align}
f(x) = \frac{1}{\sigma\sqrt{2\pi}} 
  \exp\left( -\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^{\!2}\,\right)
  \label{eq:1}
\end{align}

The code above will render as Equation (1).

\[\begin{aligned} \label{eq:1} f(x) = \frac{1}{\sigma\sqrt{2\pi}} \exp\left( -\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^{\!2}\,\right) \end{aligned} \tag{1}\]

4 Equation numbering

In LaTeX your equations get numbered automatically unless you are using a displaymath environment (equivalently \[..\]), a starred math environment such as equation*, or \nonumber to suppress numbering. Equation numbering works a bit differently in bookdown (the base of the R Journal web article format) where it is mandatory to have a (\#eq:xx) which is described in more detail in Yihui Xie (2023).

The texor package relieves authors from manually adding (\#eq:xx) to equations in R Markdown by using a pandoc Lua filter to convert existing \label{..} in the equations to (\#eq:xx) during conversion. Equation (2) shows such a use case of an equation being numbered by its label in R Markdown as well as LaTeX.

\[\begin{aligned} S_{T, s}(z_t) = X^{\top} K_{b,t}^* X (Z - z_t)^s, \label{eq:xe} \end{aligned} \tag{2}\]

Equation labels must start with the prefix eq: in bookdown. All labels in bookdown must only contain alphanumeric characters, :, -, and/or / as suggested in (Yihui Xie 2023). To accomadate this, the texor package has a lua filter implementation to correct and modify equation labels and references to the bookdown accepted format.

LaTeX source code
\begin{equation}\label{binomial pdf}
  f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}

\end{equation}
The reference to above equation \eqref{binomial pdf}
Generated R markdown using texor
$$\label{binomial pdf}
  f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}   (\#eq:binomial-pdf)$$

The reference to above equation \@ref(eq:binomial-pdf)
Rendering of the above example

\[\label{binomial pdf} f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k} \tag{3}\]

The reference to above equation (3)

5 Custom math commands

The existence of user-defined LaTeX commands intended for the Math environments will work as long as they do not contain non-math LaTeX commands or commands from other CTAN math packages.

Renders correctly in HTML
\newcommand{\ABS}[1]{\left|#1\right|}

\[|\sigma^2| = \pm 1\]

Does not render correctly in HTML
\newcommand{\rotatethis}[1]{\rotatebox[origin=c]{90}{$#1$}}

\[\rotatebox[origin=c]{90}{\sigma^2} = \pm 1\]

6 Unsupported LaTeX commands

Altough MathJax does a good job of supporting most LaTeX math functions, some functions do not currently work. Common examples are \bm and \boldmath, for which \mathbf can be used instead.

7 Summary

In summary the texor package, with the help of pandoc and MathJax supports:

8 CRAN packages used

texor, rmarkdown, bookdown

9 CRAN Task Views implied by cited packages

ReproducibleResearch

10 Note

This article is converted from a Legacy LaTeX article using the texor package. The pdf version is the official version. To report a problem with the html, refer to CONTRIBUTE on the R Journal homepage.

MathJax authors. Mathjax website. 2021. URL https://www.mathjax.org/.
Yihui Xie. bookdown: Authoring Books and Technical Documents with R Markdown. 2023. URL https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html.

References

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Ulayil, "Working with Math environments in texor", The R Journal, 2025

BibTeX citation

@article{RJ-2025-000,
  author = {Ulayil, Abhishek},
  title = {Working with Math environments in texor},
  journal = {The R Journal},
  year = {2025},
  note = {https://rjournal.github.io/},
  volume = {17},
  issue = {3},
  issn = {2073-4859},
  pages = {1}
}