Math & LaTeX & MathJax Math Tips

LaTex math is generally not too much trouble, but certain things like changing sizes of text, or fonts, or aligning items, can be difficult or impossible. Some tricks below.

Numbering/Linking Equations

In general, avoid labeling equations. However, in some cases it will be necessary to label an equation so that, in a different location, it can be referred to. If so, this is the syntax:

.. math::
   :label: eq1

   E = mc^2

And elsewhere, in the body of the text, you can refer back
to equation :eq:`eq1` like so. This would give you text that
appears as below.
(1)\[E = mc^2\]

And elsewhere, in the body of the text, you can refer back to equation (1) like so. This would give you text that appears as below. Note labeling equations this way only works within the same document. To link to an equation from another document, use the anchor form .. _equationanchor: before the .. math:: directive. Also do not try use :name: as an anchor on a .. math:: directive for the purpose of cross document linking. This will not work — even though the documentation says the :name: attribute is supported. (See the topic Links for more on linking.)



De-italicize Roman Characters

Use textrm{text}. Compare:

.. math::

   E_m\ =\ 10^{{RMR - 10} \over {40}}
\[E_m\ =\ 10^{{RMR - 10} \over {40}}\]
.. math::

   E_m\ =\ 10^{{\textrm{RMR} - 10} \over {40}}
\[E_m\ =\ 10^{{\textrm{RMR} - 10} \over {40}}\]



Some Do’s and Dont’s with MathJax

Going from the LaTeX that pngmath used to the LaTex that MathJax uses, some common errors popped up that required fixing.

Error        Use Instead
-----        -----------
&=           =

apparently, old TeX and pngmath didn't care if you put a & next to an equals
sign in a one-line equation; mathjax errors on it, which makes sense because
the & is an operator to be used in alignment contexts, which one-line equations
don't have!

\cr         \\

pretty simple, use \\ to get a carriage return into the equation

\eqalign    \begin{align} ... \end{align}

hard to explain this, just look:

.. math::
   \eqalign{p(t) &= p_0\ e^{- \alpha t} \cr
   \noalign{\vskip 9pt} p(t) &= 0 \cr}
   \eqalign{\quad &{\hbox{for}}\ t \ge 0 \cr
   \noalign{\vskip 9pt} &{\hbox {for}}\ t < 0\cr}

above gets you errors, but this works:

.. math::
   \begin{align}
   p(t) & = p_0\ e^{- \alpha t} \ & \hbox{for } t \ge 0 \\
   p(t) & = 0  & \hbox{for } t < 0
   \end{align}

The equation in the last example that works looks like this:

\[\begin{split}\begin{align} p(t) & = p_0\ e^{- \alpha t} \ & \hbox{for } t \ge 0 \\ p(t) & = 0 & \hbox{for } t < 0 \end{align}\end{split}\]

NOTE: going from TeX (old books) to pngmath (Sphinx, first edition), lots of equations used things like eqalign to do alignment, and at the time there was no known way to get that working in pngmath. Consequently, the adopted approach was to split aligned multiline equations into single, centered equations – which caused the alignment to be lost, but at least the equation rendered (pnmath would vomit very verbose error output into the document if rendering failed (yuck)). Using the technique above it should be possible to restore those lost aligned equations to a state of alignment.