Concepts
Terms and background used throughout the documentation: MathML, LaTeX, display formulas, carried TeX, fallbacks and canonical output.
The other pages use these terms without redefining them. Readers who know MathML and LaTeX can skip to the sections they need.
Formulas and formats#
A formula is one piece of mathematics, such as x² + 1 or a
matrix. A format is a way of writing formulas, such as MathML or
LaTeX. mathconv takes one formula written in its source format, the
input, and writes the same formula in its target format, the
output. Converting one input is a conversion.
Characters that are hard to tell apart are given by their Unicode code point, such as U+03B1 for the Greek letter α.
MathML#
MathML is a markup language for mathematics. A MathML formula is a tree of elements. Each element has a name, may have attributes, and has content: text, other elements, or both.
<math display="block">
<mfrac>
<mi>a</mi>
<mn>2</mn>
</mfrac>
</math>
Here math, mfrac, mi and mn are elements, display="block" is
an attribute of math, and a and 2 are text. The elements directly
inside an element are its children. The outermost element of a
formula is math. In the written markup, <mi> is the opening tag
of an mi element and </mi> its closing tag.
MathML exists in several versions (MathML 2, 3 and 4, and MathML Core, the part browsers implement); mathconv reads all of them.
XML and HTML5 MathML#
XML MathML follows the strict rules of XML: every element is
closed, and elements belong to the MathML namespace, often written
with a prefix such as mml: in <mml:mi>. Scientific articles and
XHTML pages use it. HTML5 MathML is embedded directly in an HTML
page, without namespaces or prefixes. HTML allows some elements to be
left unclosed, and real pages also contain errors such as tags closed
in the wrong order. mathconv reads both forms and recovers from these
errors.
Markup can also refer to a character instead of containing it. A named
entity reference such as α stands for α, and a numeric
character reference gives the code point: α and α
are both α.
Presentation and content markup#
Presentation markup describes how a formula looks: which symbols
appear and where they sit. The example above is presentation markup.
Content markup describes what a formula means, such as "apply
addition to a and b": <apply><plus/><ci>a</ci><ci>b</ci></apply>.
Its elements name operators, such as plus, and constants, such as
pi; ci holds an identifier, cn a number and csymbol a symbol
from a content dictionary, a published list of symbols and their
meanings. Qualifiers such as bvar (a bound variable) and
lowlimit add the variable and limits of a sum or an integral.
mathconv converts both kinds of markup, and a formula may mix them.
Tokens#
Token elements hold text:
| Element | Holds |
|---|---|
mi | an identifier: a variable or a name, such as x or sin |
mn | a number, such as 3.14 |
mo | an operator: +, =, a bracket, a comma, ∑ |
mtext | ordinary text, such as if |
ms | a quoted string literal |
mspace | blank space of a given width; it holds no text |
mglyph | a character drawn from an image |
One mi, mn or mo element is one token, whatever the length of
its text: <mi>sin</mi> is one token, and so is <mn>3.14</mn>.
Layout elements#
Layout elements arrange other elements:
| Element | Arrangement |
|---|---|
mrow | a horizontal row of children |
mfrac | a fraction: numerator over denominator |
msqrt, mroot | a square root; a root with an index |
msub, msup, msubsup | a base with a subscript, superscript or both |
mmultiscripts | a base with several scripts, also on the left |
munder, mover, munderover | a base with something set below, above or both |
mfenced | children between brackets |
menclose | a child inside a box, circle, strike or line |
mtable, mtr, mlabeledtr, mtd | a table, its rows and its cells |
mstyle, mpadded | style and size settings for the children |
mphantom | children that take up space but are invisible |
merror | children shown as an error message |
maction | alternatives, one of which is shown |
semantics | one formula plus other descriptions of it |
MathML also has elementary math elements (mstack, mlongdiv and
their parts) for school-style arithmetic such as long division.
Several elements, including math, mrow, msqrt and mtd, treat
their children as a row, laid out left to right. Where an element
expects a single child, a row of several children counts as one.
In x² the base is x and the
script is 2; a script is a subscript or a superscript, on
the right of the base or, in mmultiscripts, also on the left. In √x
and ∛x, x is the radicand and 3 is the index. An
accent is a mark set directly above or below a base, such as the
hat in x̂ or the bar in x̄.
Operators#
Operators include relations such as = and < and binary
operators such as +. The MathML operator dictionary lists the
default properties of each operator character:
- the space around it;
- stretchy: it grows to fit its content, as tall brackets grow to the height of a fraction and an arrow grows to the width of a label;
- large operator: it is drawn larger, as
∑and∫are; - movable limits: the items set below and above it, its
limits, move to script positions in an inline formula, as the
limits of
∑do; - fence: it is an opening or closing bracket;
- separator: it separates or ends items, as a comma or a full stop does;
- accent: when set above or below a base, it is an accent.
An operator's properties depend on its form, which follows from its
position in its row: the first item is in prefix form, the last in
postfix form, and any other in infix form. A minus sign before
x is prefix; between a and b it is infix. The form attribute
sets the form directly. mathconv also treats a sign that follows a
relation or another operator, as in a = -b, as prefix, which
Architecture describes. When the dictionary lists an operator only in
another form, that form's properties apply: = at the start of a row
is still spaced as a relation.
Invisible operators carry meaning and display as nothing: function application (U+2061), invisible times (U+2062), invisible separator (U+2063) and invisible plus (U+2064).
Letter styles#
The mathvariant attribute selects a letter style, such as bold,
double-struck (ℝ) or fraktur (𝔤). A one-letter identifier is italic
by default and a longer one, such as sin, is upright. Unicode also
has letters with a style built in, such as 𝐱 (bold x).
Flattened text#
The flattened text of an element is all text inside it, at any
depth, in reading order, with runs of whitespace reduced to one space.
The flattened text of <apply><plus/><ci>a</ci><ci>b</ci></apply> is
ab.
LaTeX#
LaTeX is the most widely used notation for writing mathematics, built on the TeX typesetting system. In these documents "LaTeX" means LaTeX math notation.
\frac{a}{2} + x^2
- A command starts with a backslash. A control word is a
backslash followed by letters, such as
\frac. A control symbol is a backslash followed by one other character, such as\,. A control word ends at the first character that is not a letter, so\alpha xis α times x, and\alphaxis an unknown command. - Braces make a group:
{a+b}is one unit. A command takes its arguments as groups:\frac{a}{2}has the argumentsaand2. _starts a subscript and^a superscript.- An environment is a region between
\begin{name}and\end{name}. Tables use environments such asmatrix; inside them&separates cells and\\ends a row. - Formulas are set in math mode, where letters are italic and
spaces in the source are ignored.
\text{…}switches to text mode, where letters are upright and spaces count. - Delimiters mark where a formula starts and ends inside a
document:
$…$or\(…\)around an inline formula,$$…$$or\[…\]around a display formula. The formula without its delimiters is its body.
KaTeX and MathJax are the most widely used libraries for rendering LaTeX in web browsers. Each supports a different subset of LaTeX.
The symbol table of mathconv maps each character that LaTeX writes
as a command to the single command mathconv uses for it, such as
\leq for ≤.
Function names#
A function name is a name written upright before its argument, such
as sin in sin x. The known function names are those that both
KaTeX and MathJax define as operator commands:
arccos, arcsin, arctan, arg, cos, cosh, cot, coth,
csc, deg, det, dim, exp, gcd, hom, inf, ker, lim,
liminf, limsup, ln, log, max, min, Pr, sec, sin,
sinh, sup, tan, tanh.
Function names that are also units#
Three known function names are also common units: written upright
with no sign of use, they name a unit, as min does in 5 min. These
are the names that need that sign to count as function names:
deg, min, sec.
Function names that take limits#
LaTeX sets the items below and above ten of the known function names
as limits, the way it does for \sum: below and above the name in
display style, and as scripts beside it otherwise:
det, gcd, inf, lim, liminf, limsup, max, min, Pr,
sup.
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 symbols. In MathML
a formula is a display formula when its math element has
display="block", or mode="display" in documents written for
MathML 1; otherwise it is inline. The display flag of a conversion
is true for a display formula.
Carried TeX#
Many MathML formulas are generated from LaTeX and keep the LaTeX source
alongside the markup, in an annotation element inside semantics or
in the alttext attribute of math.
<math>
<semantics>
<msup><mi>x</mi><mn>2</mn></msup>
<annotation encoding="application/x-tex">x^2</annotation>
</semantics>
</math>
LaTeX found in either place is carried TeX. On request, mathconv
returns the carried TeX verbatim instead of converting the markup, as
the option prefer_carried_tex
describes.
Fallback and raw tags#
When mathconv encounters an element it cannot convert, such as
mstack, it outputs the element's text instead, along with
any structure inside it that it can convert. This is a fallback,
and each one is recorded with a code that describes it, as listed
under Fallbacks.
A raw tag is anything that looks like the start of a markup tag:
< followed by a letter, /, ! or ?, as in <mi> or </mrow>.
The output of mathconv never contains a raw tag, whatever the input.
Canonical output#
Many LaTeX strings render the same formula: x^2, x^{2} and
x^{ 2 } all render as x². mathconv always writes exactly one of them,
chosen by fixed style rules modelled on hand-written LaTeX. The output
is canonical: formulas with the same structure produce the same
output, regardless of which tool or author produced the MathML.
The result#
A conversion returns a result: the output, the display flag, where the output was taken from (the converted markup or carried TeX), and the list of fallbacks. Python API reference describes its attributes.