This is a small sample article to demonstrate usage of the texor package to convert math environments.
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.
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):
High-quality display of mathematics notation in all browsers.
No special browser setup required.
Support for LaTeX, MathML, and other equation markup directly in the HTML source.
Support for accessibility, copy and paste, and other rich functionality.
Interoperability with other applications and math-aware search.
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.
One can define inline math in LaTeX using commands \(..\) or $..$.
| Command | Math |
|---|---|
|
\(\mu = (0,0,0)^\top\) |
|
\(\mu = (0,0,0)^\top\) |
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}\]
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.
\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}$$\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)\[\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)
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.
\newcommand{\ABS}[1]{\left|#1\right|}\[|\sigma^2| = \pm 1\]
\newcommand{\rotatethis}[1]{\rotatebox[origin=c]{90}{$#1$}}\[\rotatebox[origin=c]{90}{\sigma^2} = \pm 1\]
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.
In summary the texor package, with the help of pandoc and MathJax supports:
Common math environments.
Equation numbering.
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.
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 ...".
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}
}