Documentation version dev
Documentation
On this page

Supported MathML

Which MathML elements and attributes mathconv converts, how it reads broken markup, and how fallbacks report what it cannot convert.

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 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 &alpha; or &InvisibleTimes;, and every numeric character reference, such as &#x3B1;, is replaced by its character. An entity name that HTML does not define is kept as literal text and recorded with the fallback unknown-entity.
  • Comments, processing instructions and declarations are skipped, and the text of a CDATA section 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:

ElementsConverted as
mi, mn, moidentifiers, 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, mstext; an ms keeps its quotes, from lquote and rquote
mspacehorizontal space of its width; a linebreak of newline or indentingnewline keeps the parts apart
mrow, mstyle, mpadded, merrortheir children in a row; mathvariant on mstyle applies to the tokens inside
mfraca fraction; a linethickness of zero gives one without a line
msqrt, mrootroots
msub, msup, msubsup, mmultiscripts, mprescripts, nonescripts, also on the left
munder, mover, munderoverlimits, accents and anything else set below or above, with accent and accentunder
mfencedits children between open and close, separated by separators
menclosethe notations box, roundedbox, updiagonalstrike, downdiagonalstrike, top, bottom and radical
mtable, mtr, mlabeledtr, mtdmatrices and lines of equations, with the alignment of columnalign and of alignment classes, the lines of columnlines and rowlines, and row labels
mphantominvisible content that takes up space
mactionthe child it shows: the one that selection names for a toggle, otherwise the first
semantics, annotation, annotation-xmlthe formula of semantics, as described below
maligngroup, malignmarknothing; 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, mlongdiv and their parts, with the fallback elementary-math.
  • mglyph, a character drawn from an image, which is replaced by its alt text, with the fallback glyph.
  • menclose notations other than those above, such as circle and the default longdiv: the content is kept without the notation, with the fallback unsupported-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:

CodeMeaningThe output contains
no-maththe input has no math elementnothing
extra-mathfurther math elements after the first; the detail is their numbernothing
unknown-elementan element that is not presentation MathML, or one the reader does not convertits text
content-markupcontent markup that the reader does not convert, such as declare or a qualifier outside an applyits text
elementary-mathmstack, mlongdiv or one of their partsits text
glyphan mglyphits alt text
child-counta layout element with too few or too many children, or an operator of content markup with too few or too many argumentsempty groups for missing children; extra children after the element
error-elementan merrorits converted content
unknown-entityan entity name that HTML does not definethe entity as literal text
unmapped-charactera character with no LaTeX commandthe character in text mode, or nothing for a character that no renderer can show
unsupported-notationan menclose notation the target format cannot drawthe content without that notation
depth-limitelements nested deeper than 256 levelsthe text of the deeper part
size-limitan input longer than 256 MiBnothing
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.

Edit this page on GitHub