TITLE: How various formats can deal with LaTeX math
AUTHOR: Hans Petter Langtangen at Simula Research Laboratory & University of Oslo
DATE: today

__Summary.__
The purpose of this document is to test LaTeX math in DocOnce with
various output formats.  Most LaTeX math constructions are renedered
correctly by MathJax in plain HTML, but some combinations of
constructions may fail.  Unfortunately, only a subset of what works in
html MathJax also works in sphinx MathJax. The same is true for
markdown MathJax expresions (e.g., Jupyter notebooks).  Tests and
examples are provided to illustrate what may go wrong.

The recommendation for writing math that translates to MathJax in
html, sphinx, and markdown is to stick to the environments `\[
... \]`, `equation`, `equation*`, `align`, `align*`, `alignat`, and
`alignat*` only. Test the math with sphinx output; if it works in that
format, it should work elsewhere too.

The current version of the document is translated from DocOnce source
to the format _${FORMAT}_.

======= Test of equation environments =======

===== Test 1: Inline math =====

We can get an inline equation
`$u(t)=e^{-at}$` rendered as $u(t)=e^{-at}$.

===== Test 2: A single equation with label =====

An equation with number,

!bc latexcod
|bt
\begin{equation} u(t)=e^{-at} label{eq1a}\end{equation}
|et
!ec
looks like

!bt
\begin{equation} u(t)=e^{-at} label{_eq1a}\end{equation}
!et
Maybe this multi-line version is what we actually prefer to write:

!bc latexcod
|bt
\begin{equation}
u(t)=e^{-at}
label{eq1b}
\end{equation}
|et
!ec
The result is the same:

!bt
\begin{equation}
u(t)=e^{-at} label{_eq1b}
\end{equation}
!et
We can refer to this equation through its label `eq1b`: (ref{_eq1b}).


===== Test 3: Multiple, aligned equations without label and number =====

MathJax has historically had some problems with rendering many LaTeX
math environments, but the `align*` and `align` environments have
always worked.

!bc latexcod
|bt
\begin{align*}
u(t)&=e^{-at}\\
v(t) - 1 &= \frac{du}{dt}
\end{align*}
|et
!ec
Result:

!bt
\begin{align*}
u(t)&=e^{-at}\\
v(t) - 1 &= \frac{du}{dt}
\end{align*}
!et

===== Test 4: Multiple, aligned equations with label =====

Here, we use `align` with user-prescribed labels:

!bc latexcod
|bt
\begin{align}
u(t)&=e^{-at}
label{eq2b}\\
v(t) - 1 &= \frac{du}{dt}
label{eq3b}
\end{align}
|et
!ec
Result:

!bt
\begin{align}
u(t)&=e^{-at}
label{_eq2b}\\
v(t) - 1 &= \frac{du}{dt}
label{_eq3b}
\end{align}
!et
We can refer to the last equations as the system (ref{_eq2b})-(ref{_eq3b}).




===== Test 5: Multiple, aligned equations without label =====

In LaTeX, equations within an `align` environment is automatically
given numbers.  To ensure that an html document with MathJax gets the
same equation numbers as its latex/pdflatex companion, DocOnce
generates labels in equations where there is no label prescribed. For
example,

!bc latexcod
|bt
\begin{align}
u(t)&=e^{-at}
\\
v(t) - 1 &= \frac{du}{dt}
\end{align}
|et
!ec
is edited to something like

!bc latexcod
|bt
\begin{align}
u(t)&=e^{-at}
label{_auto5}\\
v(t) - 1 &= \frac{du}{dt}
label{_auto6}
\end{align}
|et
!ec
and the output gets the two equation numbered.

!bt
\begin{align}
u(t)&=e^{-at}\\
v(t) - 1 &= \frac{du}{dt}
\end{align}
!et

===== Test 6: Multiple, aligned equations with multiple alignments =====

The `align` environment can be used with two `&` alignment characters, e.g.,

!bc latexcod
|bt
\begin{align}
\frac{\partial u}{\partial t} &= \nabla^2 u, & x\in (0,L),
\ t\in (0,T]\\
u(0,t) &= u_0(x), & x\in [0,L]
\end{align}
|et
!ec
The result in ${FORMAT} becomes

!bt
\begin{align}
\frac{\partial u}{\partial t} &= \nabla^2 u, & x\in (0,L),
\ t\in (0,T]\\
u(0,t) &= u_0(x), & x\in [0,L]
\end{align}
!et

A better solution is usually to use an `alignat` environment:

!bc latexcod
|bt
\begin{alignat}{2}
\frac{\partial u}{\partial t} &= \nabla^2 u, & x\in (0,L),
\ t\in (0,T]\\
u(0,t) &= u_0(x), & x\in [0,L]
\end{alignat}
|et
!ec
with the rendered result

!bt
\begin{alignat}{2}
\frac{\partial u}{\partial t} &= \nabla^2 u, & x\in (0,L),
\ t\in (0,T]\\
u(0,t) &= u_0(x), & x\in [0,L]
\end{alignat}
!et



If DocOnce had not rewritten the above equations, they would be
rendered in ${FORMAT} as

$$
\begin{alignat}{2}
\frac{\partial u}{\partial t} &= \nabla^2 u, & x\in (0,L),
\ t\in (0,T]\\
u(0,t) &= u_0(x), & x\in [0,L]
\end{alignat}
$$




===== Test 7: Multiple, aligned eqnarray equations without label =====

Let us try the old `eqnarray*` environment.

!bc latexcod
|bt
\begin{eqnarray*}
u(t)&=& e^{-at}\\
v(t) - 1 &=& \frac{du}{dt}
\end{eqnarray*}
|et
!ec
which results in

!bt
\begin{eqnarray*}
u(t)&=& e^{-at}\\
v(t) - 1 &=& \frac{du}{dt}
\end{eqnarray*}
!et

===== Test 8: Multiple, eqnarrayed equations with label =====

Here we use `eqnarray` with labels:

!bc latexcod
|bt
\begin{eqnarray}
u(t)&=& e^{-at}
label{eq2c}\\
v(t) - 1 &=& \frac{du}{dt}
label{eq3c}
\end{eqnarray}
|et
!ec
which results in

!bt
\begin{eqnarray}
u(t)&=& e^{-at} label{_eq2c}\\
v(t) - 1 &=& \frac{du}{dt} label{_eq3c}
\end{eqnarray}
!et
Can we refer to the last equations as the system (ref{_eq2c})-(ref{_eq3c})
in the ${FORMAT} format?

===== Test 9: The `multiline` environment with label and number =====

The LaTeX code

!bc latexcod
|bt
\begin{multline}
\int_a^b f(x)dx = \sum_{j=0}^{n} \frac{1}{2} h(f(a+jh) +
f(a+(j+1)h)) \\
=\frac{h}{2}f(a) + \frac{h}{2}f(b) + \sum_{j=1}^n f(a+jh)
label{multiline:eq1}
\end{multline}
|et
!ec
gets rendered as

!bt
\begin{multline}
\int_a^b f(x)dx = \sum_{j=0}^{n} \frac{1}{2} h(f(a+jh) +
f(a+(j+1)h)) \\
=\frac{h}{2}f(a) + \frac{h}{2}f(b) + \sum_{j=1}^n f(a+jh)
label{_multiline:eq1}
\end{multline}
!et
and we can hopefully refer to the Trapezoidal rule
as the formula (ref{_multiline:eq1}).


===== Test 10: Splitting equations using a split environment =====

Although `align` can be used to split too long equations, a more obvious
command is `split`:

!bc latexcod
|bt
\begin{equation}
\begin{split}
\int_a^b f(x)dx = \sum_{j=0}^{n} \frac{1}{2} h(f(a+jh) +
f(a+(j+1)h)) \\
=\frac{h}{2}f(a) + \frac{h}{2}f(b) + \sum_{j=1}^n f(a+jh)
\end{split}
\end{equation}
|et
!ec

The result becomes

!bt
\begin{equation}
\begin{split}
\int_a^b f(x)dx = \sum_{j=0}^{n} \frac{1}{2} h(f(a+jh) +
f(a+(j+1)h)) \\
=\frac{h}{2}f(a) + \frac{h}{2}f(b) + \sum_{j=1}^n f(a+jh)
\end{split}
\end{equation}
!et

===== Test 11: Newcommands and boldface bm vs pmb =====

First we use the plain old pmb package for bold math. The formula

!bc latexcod
|bt
\[ \frac{\partial\u}{\partial t} +
\u\cdot\nabla\u = \nu\nabla^2\u -
\frac{1}{\varrho}\nabla p,\]
|et
!ec
and the inline expression `$\nabla\u (\x)\cdot\normalvec$`
(with suitable newcommands using pmb)
get rendered as

!bt
\[ \frac{\partial\u}{\partial t} +
\u\cdot\nabla\u = \nu\nabla^2\u -
\frac{1}{\varrho}\nabla p,\]
!et
and $\nabla\u (\x)\cdot\normalvec$.

Somewhat nicer fonts may appear with the more modern `\bm` command:

!bc latexcod
|bt
\[ \frac{\partial\ubm}{\partial t} +
\ubm\cdot\nabla\ubm = \nu\nabla^2\ubm -
\frac{1}{\varrho}\nabla p,\]
|et
!ec
(backslash `ubm` is a newcommand for bold math $u$), for which we get

!bt
\[ \frac{\partial\ubm}{\partial t} +
\ubm\cdot\nabla\ubm = \nu\nabla^2\ubm -
\frac{1}{\varrho}\nabla p.\]
!et
Moreover,

!bc
$\nabla\bm{u}(\bm{x})\cdot\bm{n}$
!ec
becomes $\nabla\bm{u}(\bm{x})\cdot\bm{n}$.

!bwarning
Note: for the ${FORMAT} format, `\bm` was substituted by DocOnce
to `\boldsymbol`.
!ewarning


======= Problematic equations =======

Finally, we collect some problematic formulas in MathJax. They all work
fine in LaTeX. Most of them look fine in html too, but some fail in
sphinx, ipynb, or markdown.

===== Colored terms in equations =====

The LaTeX code

!bc latexcod
|bt
\[ {\color{blue}\frac{\partial\u}{\partial t}} +
\nabla\cdot\nabla\u = \nu\nabla^2\u -
\frac{1}{\varrho}\nabla p,\]
|et
!ec

results in

!bt
\[ {\color{blue}\frac{\partial\u}{\partial t}} +
\nabla\cdot\nabla\u = \nu\nabla^2\u -
\frac{1}{\varrho}\nabla p,\]
!et

===== Bar over symbols =====

Sometimes one must be extra careful with the LaTeX syntax to get sphinx MathJax
to render a formula correctly. Consider the combination of a bar over a
bold math symbol:

!bc latexcod
|bt
\[ \bar\f = f_c^{-1}\f,\]
|et
!ec

which for ${FORMAT} output results in

!bt
\[ \bar\f = f_c^{-1}\f.\]
!et

With sphinx, this formula is not rendered. However, using curly braces for the bar,

!bc latexcod
|bt
\[ \bar{\f} = f_c^{-1}\f,\]
|et
!ec

makes the output correct also for sphinx:

!bt
\[ \bar{\f} = f_c^{-1}\f,\]
!et


===== Matrix formulas =====

Here is an `align` environment with a label and the `pmatrix`
environment for matrices and vectors in LaTeX.

!bc latexcod
|bt
\begin{align}
\begin{pmatrix}
G_2 + G_3 & -G_3 & -G_2 & 0 \\
-G_3 & G_3 + G_4 & 0 & -G_4 \\
-G_2 & 0 & G_1 + G_2 & 0 \\
0 & -G_4 & 0 & G_4
\end{pmatrix}
&=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix}
+ \cdots
label{mymatrixeq}\\
\begin{pmatrix}
C_5 + C_6 & -C_6 & 0 & 0 \\
-C_6 & C_6 & 0 & 0 \\
0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0
\end{pmatrix}
\frac{d}{dt} &=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix} =
\begin{pmatrix}
0 \\
0 \\
0 \\
-i_0
\end{pmatrix}
\end{align}
|et
!ec

which becomes

!bt
\begin{align}
\begin{pmatrix}
G_2 + G_3 & -G_3 & -G_2 & 0 \\
-G_3 & G_3 + G_4 & 0 & -G_4 \\
-G_2 & 0 & G_1 + G_2 & 0 \\
0 & -G_4 & 0 & G_4
\end{pmatrix}
&=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix}
+ \cdots
label{_mymatrixeq}\\
\begin{pmatrix}
C_5 + C_6 & -C_6 & 0 & 0 \\
-C_6 & C_6 & 0 & 0 \\
0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0
\end{pmatrix}
\frac{d}{dt} &=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix} =
\begin{pmatrix}
0 \\
0 \\
0 \\
-i_0
\end{pmatrix}
\end{align}
!et

The same matrices without labels in an `align*` environment:

!bc latexcod
|bt
\begin{align*}
\begin{pmatrix}
G_2 + G_3 & -G_3 & -G_2 & 0 \\
-G_3 & G_3 + G_4 & 0 & -G_4 \\
-G_2 & 0 & G_1 + G_2 & 0 \\
0 & -G_4 & 0 & G_4
\end{pmatrix}
&=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix}
+ \cdots \\
\begin{pmatrix}
C_5 + C_6 & -C_6 & 0 & 0 \\
-C_6 & C_6 & 0 & 0 \\
0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0
\end{pmatrix}
\frac{d}{dt} &=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix} =
\begin{pmatrix}
0 \\
0 \\
0 \\
-i_0
\end{pmatrix}
\end{align*}
|et
!ec

The rendered result becomes

!bt
\begin{align*}
\begin{pmatrix}
G_2 + G_3 & -G_3 & -G_2 & 0 \\
-G_3 & G_3 + G_4 & 0 & -G_4 \\
-G_2 & 0 & G_1 + G_2 & 0 \\
0 & -G_4 & 0 & G_4
\end{pmatrix}
&=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix}
+ \cdots \\
\begin{pmatrix}
C_5 + C_6 & -C_6 & 0 & 0 \\
-C_6 & C_6 & 0 & 0 \\
0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0
\end{pmatrix}
\frac{d}{dt} &=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix} =
\begin{pmatrix}
0 \\
0 \\
0 \\
-i_0
\end{pmatrix}
\end{align*}
!et
