Using the LaTeX output
How to render the LaTeX that mathconv writes with KaTeX and MathJax or use it in LaTeX documents, including display mode, equation labels and text.
You are reading the documentation of the development version on the dev branch, which is not released. The latest release is 0.1.0a4: this page in 0.1.0a4.
mathconv writes LaTeX math notation: the body of a formula, without
delimiters unless the delimiters option
asks for them. Every formula comes out in one canonical style, so
formulas with the same structure give the same string whichever tool
produced their MathML. LaTeX output style states
the rules of that style; this page covers what you need to know to use
the output.
Display and inline formulas#
An inline formula sits inside a line of text. A display formula
stands on its own line, usually centred, with larger operators and
limits set above and below. Result.display is True for a display
formula, which the math element marks with display="block".
Render each formula in the mode that its display flag gives. With the
delimiters option, the delimiters already say which mode to use. With
delimiters="none", pass the flag to the renderer yourself: the
displayMode option of KaTeX, or the display option of MathJax's
tex2chtml and tex2svg. Some output needs display mode: in a display
formula, equation labels are written as \tag* and labelled lines as
align* or gather*, which KaTeX accepts in display mode only.
import mathconv
converter = mathconv.Converter()
[(result.output, result.display) for result in converter.result_batch([
"<math><munder><mo>lim</mo><mrow><mi>n</mi><mo>→</mo><mi>∞</mi></mrow></munder><msub><mi>a</mi><mi>n</mi></msub></math>",
'<math display="block"><munder><mo>lim</mo><mrow><mi>n</mi><mo>→</mo><mi>∞</mi></mrow></munder><msub><mi>a</mi><mi>n</mi></msub></math>',
])]
# [('\\lim_{n \\to \\infty}a_n', False), ('\\lim_{n \\to \\infty}a_n', True)]
The LaTeX of both formulas is the same: the renderer places the limit
beside lim in inline mode and below it in display mode.
Rendering with KaTeX and MathJax#
KaTeX and MathJax are the most widely used libraries for
rendering LaTeX in web browsers. Every LaTeX output that mathconv
converts from markup is accepted by both, rendered in the mode that
display gives:
- KaTeX in its default settings. KaTeX in its strict mode
erroraccepts the output too, with one exception: a character that has no LaTeX command is written in\text{}with the fallbackunmapped-character, and strict mode may reject such a character as unknown (unknownSymbol), as in\text{字}. - MathJax 4 in its default settings. MathJax 3 does not load the
textmacrospackage by default; without it, escaped characters inside text show their backslash, as in\text{50\% off}, so addtextmacrosto the packages of its TeX input.
The versions checked are the ones pinned in
tools/render-check/package.json,
and Testing describes
the check. Carried TeX, which
prefer_carried_tex returns, is the
author's own LaTeX and carries no such guarantee.
Using the output in LaTeX documents#
The output uses commands and environments of these packages:
| Package | Provides |
|---|---|
amsmath | \text, \operatorname, \tag*, \binom, \genfrac, \boxed, \overset, \underset, the matrix environments, cases, aligned, gathered, align*, gather* and more |
amssymb | \mathbb, \mathfrak and most symbols beyond those of plain LaTeX, such as \leqslant and \therefore |
mathtools | \xmapsto |
cancel | \cancel, \bcancel and \xcancel |
arydshln | dashed lines in array: : in the column specification and \hdashline |
esint | \oiint |
\usepackage{amsmath,amssymb,mathtools,cancel,arydshln,esint}
With these packages, LuaLaTeX compiles the LaTeX outputs of the
project's test cases and every command of its symbol table, except
\oiiint (∰), which unicode-math defines. Characters that have no
LaTeX command are written as themselves, inside \text{} or, for
symbols that KaTeX and MathJax accept, in math mode; they need LuaLaTeX
or XeLaTeX with a font that has them.
A display formula whose output is an align* or gather* environment
is a complete display of its own. In a LaTeX document it stands alone,
outside \[…\] and $$…$$, which amsmath does not allow around these
environments. KaTeX and MathJax accept it either way. To write LaTeX
documents, convert with delimiters="none" and wrap the other display
formulas in \[…\] yourself.
Equation labels#
A label is the text that MathML sets beside a row of a table, such
as the equation number (1) in the first cell of an mlabeledtr.
mathconv writes each label at the end of its line:
- When the table makes up a whole display formula, each label is a
\tag*{…}on its line, and lines that would be written inalignedorgatheredare written inalign*orgather*, where\tag*is allowed.\tag*shows the label as it stands, brackets included. - Anywhere else, such as in an inline formula or in a matrix, the label
follows its line after a
\qquad, written as text.
import mathconv
lines = (
"<mtable>"
"<mlabeledtr><mtd><mtext>(2)</mtext></mtd><mtd><mi>a</mi></mtd><mtd><mo>=</mo><mi>b</mi></mtd></mlabeledtr>"
"<mlabeledtr><mtd><mtext>(3)</mtext></mtd><mtd><mi>c</mi></mtd><mtd><mo>=</mo><mi>d</mi></mtd></mlabeledtr>"
"</mtable>"
)
mathconv.convert('<math display="block">' + lines + "</math>")
# '\\begin{align*}a & = b \\tag*{(2)} \\\\ c & = d \\tag*{(3)}\\end{align*}'
mathconv.convert("<math>" + lines + "</math>")
# '\\begin{aligned}a & = b \\qquad \\text{(2)} \\\\ c & = d \\qquad \\text{(3)}\\end{aligned}'
mathconv.convert('<math display="block">' + lines + "</math>", drop_labels=True)
# '\\begin{aligned}a & = b \\\\ c & = d\\end{aligned}'
The option drop_labels leaves labels
out. Rule
M4
of the style has the details, such as how labels that hold math are
written.
Text#
Text in a formula, such as the words of mtext, is written in
\text{…}, and bold, italic, sans-serif and monospace text in
\textbf, \textit, \textsf and \texttt. Inside text, #, $,
%, &, _, { and } are escaped with a backslash, and a symbol
that has no text command, such as a Greek letter, is written as math
between $ signs:
import mathconv
mathconv.convert("<math><mtext>50% off for λ > 0</mtext></math>")
# '\\text{50\\% off for $\\lambda$ $>$ 0}'
Text keeps one space at each end, inside the braces, where it separates the text from its neighbours. The text rules of LaTeX output style have the details.
Characters without a command#
A character that LaTeX writes as a command always gets the same
command, the one authors type most, such as \leq for ≤. A character
with no LaTeX command is written as itself when it is a symbol that
KaTeX and MathJax accept in math mode; any other character, such as a
CJK ideograph, is written in \text{} and recorded with the fallback
unmapped-character. Private-use characters and the Unicode line and
paragraph separators are left out and recorded the same way.
import mathconv
result = mathconv.Converter().result("<math><mi>a</mi><mo>≤</mo><mi>字</mi></math>")
result.output, [(fallback.code, fallback.detail) for fallback in result.fallbacks]
# ('a \\leq \\text{字}', [('unmapped-character', '字')])
Rules C1 to C8 of the style describe how each kind of character is written.