Supported MathML
Which MathML elements and attributes mathconv converts, how it reads broken markup, and how fallbacks report what it cannot convert.
mathconv reads MathML 2, 3 and 4 and MathML Core, the part of MathML that browsers implement, in presentation markup, content markup or a mix of both. Concepts introduces the elements named on this page.
Markup and characters#
mathconv reads XML MathML, as in scientific articles and XHTML pages, and HTML5 MathML, as embedded in HTML pages:
- Namespace prefixes, such as
mml:in<mml:mi>, are removed, and namespace declarations are ignored. Element and attribute names are compared case-insensitively. - Attribute values may be in double, single or no quotes.
- Every HTML entity name, such as
αor⁢, and every numeric character reference, such asα, is replaced by its character. An entity name that HTML does not define is kept as literal text and recorded with the fallbackunknown-entity. - Comments, processing instructions and declarations are skipped, and
the text of a
CDATAsection is text.
Real pages contain broken markup, such as unclosed elements or tags closed in the wrong order. mathconv repairs it: a closing tag closes the nearest open element of its name and everything opened inside it, a closing tag with nothing to close is ignored, and the end of the input closes every open element. The parser describes the rules in full.
import mathconv
mathconv.convert("<math><mfrac><mi>a</mi><mi>b</mfrac><mo>+</mo><mn>1</math>")
# '\\frac{a}{b} + 1'
The first math element of the input is converted; anything before and
after it is ignored, and further math elements are counted with the
fallback extra-math.
Presentation markup#
Presentation markup describes how a formula looks. mathconv converts these elements:
| Elements | Converted as |
|---|---|
mi, mn, mo | identifiers, numbers and operators, with letter styles from mathvariant and operator properties from the MathML operator dictionary and the attributes form, stretchy, largeop, movablelimits, fence, separator and accent |
mtext, ms | text; an ms keeps its quotes, from lquote and rquote |
mspace | horizontal space of its width; a linebreak of newline or indentingnewline keeps the parts apart |
mrow, mstyle, mpadded, merror | their children in a row; mathvariant on mstyle applies to the tokens inside |
mfrac | a fraction; a linethickness of zero gives one without a line |
msqrt, mroot | roots |
msub, msup, msubsup, mmultiscripts, mprescripts, none | scripts, also on the left |
munder, mover, munderover | limits, accents and anything else set below or above, with accent and accentunder |
mfenced | its children between open and close, separated by separators |
menclose | the notations box, roundedbox, updiagonalstrike, downdiagonalstrike, top, bottom and radical |
mtable, mtr, mlabeledtr, mtd | matrices and lines of equations, with the alignment of columnalign and of alignment classes, the lines of columnlines and rowlines, and row labels |
mphantom | invisible content that takes up space |
maction | the child it shows: the one that selection names for a toggle, otherwise the first |
semantics, annotation, annotation-xml | the formula of semantics, as described below |
maligngroup, malignmark | nothing; they only mark alignment points |
Settings that do not change which formula is shown are dropped: colours,
sizes, lspace and rspace, mpadded dimensions, scriptlevel, and
displaystyle of anything but a table. Of a table, rowalign, align,
frame, groupalign, side, spacing, widths and spans are dropped as
well. The lowering rules give
the exact reading of each element and attribute.
These are not converted, and their text is kept in the output with a fallback:
- Elementary math, the school arithmetic layout of
mstack,mlongdivand their parts, with the fallbackelementary-math. mglyph, a character drawn from an image, which is replaced by itsalttext, with the fallbackglyph.menclosenotations other than those above, such ascircleand the defaultlongdiv: the content is kept without the notation, with the fallbackunsupported-notation.- Any element that is not MathML, such as HTML inside a formula, with
the fallback
unknown-element.
Content markup#
Content markup describes what a formula means, such as "apply addition to a and b". mathconv writes it in the notation of hand-written mathematics, the default renderings of the MathML 3 specification, with parentheses only where the meaning requires them:
import mathconv
mathconv.convert("<math><apply><times/><apply><plus/><ci>a</ci><ci>b</ci></apply><ci>c</ci></apply></math>")
# '(a + b)c'
It converts apply with the operators and constants of the MathML 3
content dictionaries, csymbol with the names of those dictionaries,
the tokens ci, cn, cs and csymbol, the qualifiers such as
bvar, lowlimit and condition, and the constructors interval,
set, list, vector, matrix, piecewise and lambda. A content
element that cannot be written as notation, such as declare or a
qualifier outside an apply, keeps its text with the fallback
content-markup, and an operator with the wrong number of arguments
writes what it has, with the fallback child-count. The
content markup rules of the style
show the notation of each construct.
Semantics and annotations#
A semantics element holds one formula followed by annotations:
annotation elements with text and annotation-xml elements with
markup, each naming its notation in its encoding attribute. The
formula is the first child that is not an annotation.
When semantics holds both presentation and content markup, mathconv
converts the presentation markup, because it shows the notation the
author chose: the formula when it is presentation markup, or else the
first annotation-xml with the encoding MathML-Presentation or
application/mathml-presentation+xml. Content markup is converted only
when no presentation markup is there. When every child is an
annotation, the formula is the markup of the first presentation
annotation-xml, or else of the first annotation-xml.
Annotations in other notations, such as LaTeX or the formats of
equation editors, never reach the output, with one exception: the
option prefer_carried_tex returns
a LaTeX annotation or the alttext attribute in place of the
conversion.
Limits#
An input longer than 256 MiB is not converted and gives empty output
with the fallback size-limit. Elements nested deeper than 256 levels
are replaced by their text, with the fallback depth-limit.
Fallbacks#
A fallback records a part of the input that was not converted. Each has a code and a detail, which names the element, entity, character or notation concerned. The conversion goes on, and the output keeps what it can of that part, as the last column says:
| Code | Meaning | The output contains |
|---|---|---|
no-math | the input has no math element | nothing |
extra-math | further math elements after the first; the detail is their number | nothing |
unknown-element | an element that is not presentation MathML, or one the reader does not convert | its text |
content-markup | content markup that the reader does not convert, such as declare or a qualifier outside an apply | its text |
elementary-math | mstack, mlongdiv or one of their parts | its text |
glyph | an mglyph | its alt text |
child-count | a layout element with too few or too many children, or an operator of content markup with too few or too many arguments | empty groups for missing children; extra children after the element |
error-element | an merror | its converted content |
unknown-entity | an entity name that HTML does not define | the entity as literal text |
unmapped-character | a character with no LaTeX command | the character in text mode, or nothing for a character that no renderer can show |
unsupported-notation | an menclose notation the target format cannot draw | the content without that notation |
depth-limit | elements nested deeper than 256 levels | the text of the deeper part |
size-limit | an input longer than 256 MiB | nothing |
import mathconv
result = mathconv.Converter().result("<math><mfrac><mi>a</mi></mfrac><mglyph alt='star'/></math>")
result.output, [(fallback.code, fallback.detail) for fallback in result.fallbacks]
# ('\\frac{a}{}star', [('child-count', 'mfrac'), ('glyph', 'mglyph')])
The output never contains anything that looks like the start of a
markup tag (< followed by a letter, /, ! or ?), whatever the
input.
Coverage#
The coverage table in the README gives, for formulas held out for testing from the public mathconv corpus, the share converted without any fallback, the share that KaTeX and MathJax accept, and the share that looks the same as the LaTeX its author wrote. Corpus evaluation describes how these figures are measured.