MathML

MathML
Mathematical Markup Language (MathML) is a mathematical markup language, an application of XML for describing mathematical notations and capturing both its structure and content, and is one of a number of mathematical markup languages. Its aim is to natively integrate mathematical formulae into World Wide Web pages and other documents. It is part of HTML5 and standardised by ISO/IEC since 2015.
History
Following some experiments in the Arena browser based on proposals for mathematical markup in HTML, MathML 1 was released as a W3C recommendation in April 1998 as the first XML language to be recommended by the W3C. Version 1.01 of the format was released in July 1999 and version 2.0 appeared in February 2001. Implementations of the specification appeared in Amaya 1.1, Mozilla 1.0 and Opera 9.5. In October 2003, the second edition of MathML Version 2.0 was published as the final release by the W3C Math Working Group.
MathML was originally designed before the finalization of XML namespaces. However, it was assigned a namespace immediately after the Namespace Recommendation was completed, and for XML use, the elements should be in the namespace with namespace URL http://www.w3.org/1998/Math/MathML. When MathML is used in HTML (as opposed to XML) this namespace is automatically inferred by the HTML parser and need not be specified in the document.
MathML version 3
Version 3 of the MathML specification was released as a W3C recommendation on 20 October 2010. A recommendation of A MathML for CSS Profile was later released on 7 June 2011; this is a subset of MathML suitable for CSS formatting. Another subset, Strict Content MathML, provides a subset of content MathML with a uniform structure and is designed to be compatible with OpenMath. Other content elements are defined in terms of a transformation to the strict subset. New content elements include which associates bound variables to expressions, for example a summation index. The new element allows structure sharing.
The development of MathML 3.0 went through a number of stages. In June 2006, the W3C rechartered the MathML Working Group to produce a MathML 3 Recommendation until February 2008, and in November 2008 extended the charter to April 2010. A sixth Working Draft of the MathML 3 revision was published in June 2009. On 10 August 2010 version 3 graduated to become a "Proposed Recommendation" rather than a draft. An implementation of MathML 2 landed in WebKit around this same time, with a Chromium implementation following a couple of years later, although that implementation was removed from Chromium after less than a year.
The Second Edition of MathML 3.0 was published as a W3C Recommendation on 10 April 2014. The specification was approved as an ISO/IEC international standard 40314:2015 on 23 June 2015. Also in 2015, the MathML Association was founded to support the adoption of the MathML standard. At that time, according to a member of the MathJax team, none of the major browser makers paid any of their developers for any MathML-rendering work; whatever support existed was overwhelmingly the result of unpaid volunteer time/work.
MathML Core
In August 2021, a new specification called MathML Core was published, described as the “core subset of Mathematical Markup Language, or MathML, that is suitable for browser implementation.” MathML Core set itself apart from MathML 3.0 by including detailed rendering rules and integration with CSS, automated browser support testing resources, and focusing on a fundamental subset of MathML. An implementation was added to Chromium at the beginning of 2023.
Presentation and semantics
MathML deals not only with the presentation but also the meaning of formula components (the latter part of MathML is known as "Content MathML"). Because the meaning of the equation is preserved separate from the presentation, how the content is communicated can be left up to the user. For example, web pages with MathML embedded in them can be viewed as normal web pages with many browsers, but visually impaired users can also have the same MathML read to them through the use of screen readers (e.g. using the VoiceOver in Safari). JAWS from version 16 onward supports MathML voicing as well as braille output.
The quality of rendering of MathML in a browser depends on the installed fonts. The STIX Fonts project have released a comprehensive set of mathematical fonts under an open license. The Cambria Math font supplied with Microsoft Windows had slightly more limited support.
A valid MathML document typically consists of the XML declaration, DOCTYPE declaration, and document element. The document body then contains MathML expressions which appear in elements as needed in the document. Often, MathML will be embedded in more general documents, such as HTML, DocBook, or other XML-based formats.
Presentation MathML
Presentation MathML focuses on the display of an equation, and has about 30 elements. The elements' names all begin with . A Presentation MathML expression is built up out of tokens that are combined using higher-level elements, which control their layout. Finer details of presentation are affected by close to 50 attributes.
Token elements generally only contain characters (not other elements). They include:
– identifiers;
– operators;
– numbers;
– text.
Note, however, that these token elements may be used as extension points, allowing markup in host languages.
MathML in HTML5 allows most inline HTML markup in mtext, and is conforming, with the HTML markup being used within the MathML to mark up the embedded text (making the first word bold in this example).
These are combined using layout elements, that generally contain only elements. They include:
– a horizontal row of items;
and others – superscripts, limits over and under operators like sums, etc.;
– fractions;
and – roots;
– surrounding content with fences, such as parentheses.
As usual in HTML and XML, many entities are available for specifying special symbols by name, such as and . An interesting feature of MathML is that entities also exist to express normally-invisible operators, such as (or the shorthand ) for implicit multiplication. They are:
;
;
;
.
The full specification of MathML entities is closely coordinated with the corresponding specifications for use with HTML and XML in general.
Thus, the expression requires two layout elements: one to create the overall horizontal row and one for the superscripted exponent. However, the individual tokens also have to be identified as identifiers, operators, or numbers. Adding the token markup, the full form ends up as
<mrow>
<mi>a</mi> <mo>&InvisibleTimes;</mo> <msup><mi>x</mi><mn>2</mn></msup>
<mo>+</mo><mi>b</mi><mo>&InvisibleTimes;</mo><mi>x</mi>
<mo>+</mo><mi>c</mi>
</mrow>
A complete document that consists of just the MathML example above, is shown here:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<mi>a</mi> <mo>&InvisibleTimes;</mo> <msup><mi>x</mi><mn>2</mn></msup>
<mo>+</mo><mi>b</mi><mo>&InvisibleTimes;</mo><mi>x</mi>
<mo>+</mo><mi>c</mi>
</mrow>
</math>
Content MathML
Content MathML focuses on the semantics, or meaning, of the expression rather than its layout. Central to Content MathML is the element that represents function application. The function being applied is the first child element under , and its operands or parameters are the remaining child elements. Content MathML uses only a few attributes.
Tokens such as identifiers and numbers are individually marked up, much as for Presentation MathML, but with elements such as and . Rather than being merely another type of token, operators are represented by specific elements, whose mathematical semantics are known to MathML: etc. There are over a hundred different elements for different functions and operators.
For example, represents and represents . The elements representing operators and functions are empty elements, because their operands are the other elements under the containing .
The expression could be represented as
<math>
<apply>
<plus/>
<apply>
<times/>
<ci>a</ci>
<apply>
<power/>
<ci>x</ci>
<cn>2</cn>
</apply>
</apply>
<apply>
<times/>
<ci>b</ci>
<ci>x</ci>
</apply>
<ci>c</ci>
</apply>
</math>
Content MathML is nearly isomorphic to expressions in a functional language such as Scheme and other dialects of Lisp. amounts to Scheme's , and the many operator and function elements amount to Scheme functions. With this trivial literal transformation, plus un-tagging the individual tokens, the example above becomes:
(plus
(times a (power x 2))
(times b x)
c)
This reflects the long-known close relationship between XML element structures, and LISP or Scheme S-expressions.
Wikidata annotation in Content MathML
According to the OM Society, OpenMath Content Dictionaries can be employed as collections of symbols and identifiers with declarations of their semanticsnames, descriptions and rules. A 2018 paper presented at the SIGIR conference proposed that the semantic knowledge base Wikidata could be used as an OpenMath Content Dictionary to link semantic elements of a mathematical formula to unique and language-independent Wikidata items.
Example
The well-known quadratic formula could be represented in Presentation MathML as an expression tree made up from layout elements like or :
<math mode="display" xmlns="http://www.w3.org/1998/Math/MathML">
<semantics>
<mrow>
<mi>x</mi>
<mo>=</mo>
<mfrac>
<mrow>
<mo form="prefix">−</mo>
<mi>b</mi>
<mo>&pm;</mo>
<msqrt>
<msup><mi>b</mi><mn>2</mn></msup>
<mo>−</mo>
<mn>4</mn><mo>&it;</mo><mi>a</mi><mo>&it;</mo><mi>c</mi>
</msqrt>
</mrow>
<mrow>
<mn>2</mn>
<mo>&it;</mo>
<mi>a</mi>
</mrow>
</mfrac>
</mrow>
<annotation encoding="application/x-tex"><!-- TeX -->
x = \frac{-b\pm\sqrt{b^2-4ac}}{2a}
</annotation>
<annotation encoding="StarMath 5.0">
x = {-b plusminus sqrt {b^2 - 4 ac}} over {2 a}
</annotation>
<!-- More annotations can be written: application/x-troff-eqn for eqn, application/x-asciimath for AsciiMath... -->
<!-- Semantic MathML go under <annotation-xml encoding="MathML-Content">. -->
</semantics>
</math>
This example uses the element, which can be used to embed a semantic annotation in non-XML format, for example to store the formula in the format used by an equation editor such as StarMath or the markup using LaTeX syntax. The field is usually a MIME type, although most of the equation encodings don't have such a registration; freeform text may be used in such cases.
Although less compact than other formats, the XML structuring of MathML makes its content widely usable and accessible, allows near-instant display in applications such as web browsers, and facilitates an interpretation of its meaning in mathematical software products. MathML is not intended to be written or edited directly by humans.
Embedding MathML in HTML/XHTML files
MathML, being XML, can be embedded inside other XML files such as XHTML files using XML namespaces.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN"
"http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Example of MathML embedded in an XHTML file</title>
<meta name="description" content="Example of MathML embedded in an XHTML file"/>
</head>
<body>
<h1>Example of MathML embedded in an XHTML file</h1>
<p>
The area of a circle is
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mi>π<!-- π --></mi>
<mo>⁢<!-- &InvisibleTimes; --></mo>
<msup>
<mi>r</mi>
<mn>2</mn>
</msup>
</math>.
</p>
</body>
</html>
Inline MathML is also supported in HTML5 files. There is no need to specify namespaces as there was in XHTML.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example of MathML embedded in an HTML5 file</title>
</head>
<body>
<h1>Example of MathML embedded in an HTML5 file</h1>
<p>
The area of a circle is
<math>
<mi>π</mi>
<mo>&InvisibleTimes;</mo>
<msup>
<mi>r</mi>
<mn>2</mn>
</msup>
</math>.
</p>
</body>
</html>
Other standards
Another standard called OpenMath that has been more specifically designed (largely by the same people who devised Content MathML) for storing formulae semantically can be used to complement MathML. OpenMath data can be embedded in MathML using the element. OpenMath content dictionaries can be used to define the meaning of elements. The following would define P1(x) to be the first Legendre polynomial:
<apply>
<csymbol encoding="OpenMath" definitionURL="http://www.openmath.org/cd/contrib/cd/orthpoly1.xhtml#legendreP">
<msub><mi>P</mi><mn>1</mn></msub>
</csymbol>
<ci>x</ci>
</apply>
The OMDoc format has been created for markup of larger mathematical structures than formulae, from statements like definitions, theorems, proofs, and examples, to complete theories and even entire text books. Formulae in OMDoc documents can either be written in Content MathML or in OpenMath; for presentation, they are converted to Presentation MathML.
The ISO/IEC standard Office Open XML (OOXML) defines a different XML math syntax, derived from Microsoft Office products. However, it is partially compatible through XSL Transformations.
See also
CSS
List of document markup languages
Comparison of document markup languages
Formula editors
LaTeX2HTML
LaTeXML
KaTeXJavaScript library that converts LaTeX to MathML
MathJaxJavaScript library that converts LaTeX to MathML
References
Further reading
Specifications
W3C Recommendation: Mathematical Markup Language (MathML) 1.01 Specification
W3C Recommendation: Mathematical Markup Language (MathML) Version 2.0 (Second Edition)
W3C Recommendation: Mathematical Markup Language (MathML) Version 3.0 (Third Edition)
External links
W3C Math Home – Contains the specifications, a FAQ, and a list of supporting software.
– A collection of XSLT programs for handling MathML (e.g. converting Content MathML to Presentation MathML, converting Presentation MathML to TeX)
Articles with example Scheme (programming language) code
Mathematical markup languages
World Wide Web Consortium standards
XML markup languages
XML-based standards