MathML is an XML vocabulary for marking up mathematics, and it contains two sub-languages: presentation MathML and content MathML. Presentation MathML is primarily concerned with describing the layout of a mathematical expression (and can thus be compared to TeX, or earlier SGML markup languages for mathematics such as ISO 12083). Content MathML is primarily concerned with marking up some aspects of the meaning, or at least the mathematical structure, of expressions. This aspect of MathML was heavily influenced by the OpenMath language (see Resources for a link) for marking up mathematical semantics, and in MathML3 the alignment with OpenMath has been made far more explicit.
A simple example can show the difference between these two styles
of markup. Consider the conventional notation for the product of
sin θ with itself. Perversely, the superscript 2 is placed over
the sin operator, not at the end of the expression as you might have expected: sin2 θ.
Listing 1 shows how Content MathML encodes the mathematical structure.
Listing 1. MathML encoding
<apply><power/>
<apply><sin/>
<ci>θ</ci>
</apply>
<cn>2</cn>
</apply>
|
At the outer level is an application of the squaring function (power, with the second argument the number 2), and the first argument to power, is an
application of the sin function. Using this markup, the meaning of the
expression is highlighted, and a MathML renderer might "know" the convention
for typesetting instances of trigonometric functions and display this
expression as desired by default. Or, you might use an external styling mechanism (for example a transformation to Presentation MathML). Or,
depending on circumstances, a default rendering such as (sinθ)2 can be considered acceptable.
Presentation MathML would directly encode the layout scheme that
has a superscripted number 2 on the identifier sin, being used as a
compound operator applied to the identifier θ (see Listing 2).
Listing 2. Encoding the layout scheme using Presentation MathML
<mrow> <msup><mi>sin</mi><mn>2</mn></msup> <mo>ࠍ</mo> <mi>θ</mi> </mrow> |
Note, however, that Presentation MathML still encodes aspects of the mathematical semantics. Of particular importance in Listing 2 is the Function Application operator U+2061. This is an invisible operator, but its use, along with other invisible operators such as U+2062 (invisible times) is very important for distinguishing the content when adjacent terms have no visible operator, such as distinguishing the function application f(x) from the product 3(x). The proper use of such operators helps not only in mechanically parsing Presentation MathML, but it also improves the accessibility of the document. Audio and other non-visual renderings may be much improved by such semantic hints.
Presentation and Content MathML are not two separate
languages. You can intermingle them. Every Content MathML
expression will have, for a given MathML renderer, a default
presentation and you can freely use Content MathML terms within
presentation MathML. It is common to just use plain text, often a single character, as the content of the Content MathML token element ci (theta in Listing 1). It is, in fact, possible to use an arbitrary Presentation MathML expression, allowing complex identifiers to be built up.
After publishing the second edition of MathML 2 in 2003, the W3C Math Working group deliberately decided to hold off further specification changes to provide a stable period for its implementation experience. However, particular areas where MathML could be strengthened were identified, resulting in the production of the MathML 3.0 draft specification. The basic structure of MathML 3 is the same as that of MathML 2 and many users will probably not notice the new features, except maybe automatic line breaking, which is the main new feature to affect existing users. MathML 3 adds functionality, however, to support communities where MathML 2 was found to be weak. This includes support for right-to-left languages, with a particular emphasis on Arabic scripts, and explicit support for elementary math layouts, which helps both educators to mark up examples and also greatly helps produce accessible non-visual versions of the formulae.
I discuss the main new features of MathML 3 further in the next section.
MathML3 has introduced several new features over MathML2.
MathML 1 and 2 did not have any built in mechanism to specify links. Instead they recommended use of the XLink syntax (see Resources for a link). Partly this showed the age of MathML, since all of the initial design predated the final version of the XML standard. XLink was part
of the original "Grand Plan" for XML, along with XML syntax, XLink for linking, and XSL
for styling. However XLink has not proved generally popular as a core technology, and is
not widely supported. MathML3 now specifies that essentially any MathML element can take
an href attribute that takes an URI specifying a hyperlink in
exactly the same way as the href attribute in the HTML a element.
MathML2 had no explicit support to control the directionality of layout. An earlier note documented some extensions to allow the layout forms used in various Arabic communities to be rendered (see Resources for a link). Depending on the style being used, you have to control both the writing direction of the identifiers used within formulae, and, separately, the layout direction of the formula itself. Principally this is the addition of the dir attribute (analogous to the attribute of the same name in HTML) together with detailed rules about how directionality interacts with the layout rules of presentation MathML. Additional font variants have been proposed to Unicode to be used in a similar way to which bold or blackboard bold (double-struck) are used for the Latin alphabet, as additional alphabets of semantically distinct symbols. Font variants such as italic or sans-serif do not really apply to Arabic letters and so the math font variants initial, tailed, looped, and stretched were proposed in the Arabic Note and standardized in MathML3.
MathML2 had very little support for line breaking of mathematical equations. There was some support to specify allowable breaks in inline mathematics, but the only way to get a multiline displayed mathematical expression was to manually break the equation and enter the parts as separate rows in a table. The mathematical typesetting system TeX has a similar functionality. However, in traditional mathematical typesetting, the formula is being broken to a known page size and so it is not too inconvenient for the author to break the formula by hand. However, MathML is designed for a Web context where the expression is being displayed on a window of unknown and potentially dynamically changing size. This means that having a system of automatic line breaking is far more important. MathML 3 specifies a line breaking model and introduces several new attributes to control the properties of the line breaks and the way in which the lines are aligned.
The glyph element in MathML 1 and 2 was intended to access special
symbols from non-standard fonts that do not correspond to Unicode code
points. It was never easy to use in this manner in a Web context, since you needed to ensure that the fonts were available. In MathML3, the existing
usage is deprecated and mglyph has been extended with a src attribute
to be a general image reference element, similar to HTML's img element.
Perhaps one of the most important additions in MathML3 is a new set of functionality for laying out elementary math. These are the tabular layouts used for long multiplication and long division. A surprisingly large range of notations for these calculations are used in different countries, but they mostly share common features of a basic tabular layout based on the digit width with adornments of horizontal and vertical lines, cross-outs of various forms, and inserted digits in a smaller font size for borrows and carries. It was possible to achieve many of these effects in MathML2, but it required a lot of explicit use of tables and positioning that made the markup hard to use, and it was crucially very hard to generate any reasonable non-visual rendering as the basic form of the calculation had to be obscured by the table markup. Listing 3 shows an example of how long division was handled.
Listing 3. Long division in MathML2
435.3
______
3)1306
12
__
10
9
__
16
15
__
1.0
9
_
1
|
Listing 4 shows the MathML3 markup.
Listing 4. MathML version of long division
<mlongdiv longdivstyle="lefttop">
<mn> 3 </mn>
<mn> 435.3</mn>
<mn> 1306</mn>
<msgroup position="2" shift="-1">
<msgroup>
<mn> 12</mn>
<msline length="2"/>
</msgroup>
<msgroup>
<mn> 10</mn>
<mn> 9</mn>
<msline length="2"/>
</msgroup>
<msgroup>
<mn> 16</mn>
<mn> 15</mn>
<msline length="2"/>
<mn> 1.0</mn>
</msgroup>
<msgroup position='-1'>
<mn> 9</mn>
<msline length="3"/>
<mn> 1</mn>
</msgroup>
</msgroup>
</mlongdiv>
|
Note in particular how the numbers are entered as single tokens, and not explicitly
split up as single digits per table cell. This accessibility is greatly improved since
the audio or braille engine can use the long division (the mlongdiv element) information
and know that the first three rows reliably refer to the operands and result of the
division, with the remaining rows are part of the intermediate result layout.
Chapter 4, defining Content MathML (of the MathML W3C Document), has been completely rewritten.
Content MathML has a range of empty elements such as the examples
<power/> and <sin/> in Listing 1. These elements are
chosen to cover most of the mathematical expressions you might encounter in
a pre-university level mathematical education. However, to refer to
mathematical symbols without a predefined element, you can use the <csymbol> element: <csymbol>myfun</csymbol>.
Optionally, you might use the definitionURL attribute to specify the
URI of a definition of the function; however, there was no predefined
format for such definitions, since they can be simply natural language
text for example.
OpenMath (see Resources) is an older format with origins in computer algebra systems. It defines a model of mathematical objects and various encodings of that model, the most important one now being XML encoding. Major differences between OpenMath and Content MathML are that OpenMath has no analogue of Presentation MathML; it is purely concerned with encoding the mathematical structure. Also, unlike Content MathML, there are no predefined elements for common mathematical operators, since the core OpenMath language just defines the basic structure. Any symbols to be used must be defined externally. The OpenMath standard defines an XML document format, Content Dictionaries, for defining such symbols, although the meaning of the symbol can ultimately be given in natural language, or by reference to a book. For example, the definition of sin can be given by reference to the Content Dictionary for transcendental functions distributed from the OpenMath Web site (see Resources for a link).
MathML3 adds attributes to csymbol to allow explicit reference to
OpenMath Content Dictionaries and further aligns with OpenMath by the
addition of some extra structural elements. OpenMath distinguishes
function application (<OMA>) from binding expression (<OMBIND>).
MathML has traditionally used the element <apply> for both of these
uses, with the meaning determined by the context. However,
binding contexts can now be explicitly marked up with a new <bind>
element. Similarly, OpenMath distinguishes strings as a primitive
data-type in addition to numbers, and MathML3 has added a new <cs>
element for string literals, in addition to <cn> for numbers and
<ci> for identifiers (variables).
A major change to the way Content MathML is defined in the MathML, though it doesn't really change the defined MathML language at all, is that the meanings of all the traditional empty element constructs in Content MathML are now given by means of an explicit set of rewrite rules to a form known as Strict Content MathML. This form only uses csymbols and can be trivially mapped to OpenMath essentially just by changing element names, but preserving the expression structure. Thus, you can rewrite the Content MathML expression in the first section to the form in Listing 5.
Listing 5. Content MathML expression rewritten
<apply><csymbol cd="arith1">power</csymbol> <apply><csymbol cd="transc1">sin</csymbol><ci>θ</ci></apply> <cn type="integer">2</cn> </apply> |
Another change is that the example renderings of Content MathML, which in previous versions of MathML have just been given as images, are given both as Presentation MathML and as images in this version. Hopefully this will help more systems implement Content MathML rendering by mapping internally to existing Presentation MathML rendering support.
The transformations from
- General Content MathML to Strict Content MathML
- Content MathML to Presentation MathML
- Presentation MathML to TeX (for typesetting the sample images)
are all implemented in XSLT 2, and will be made freely available under the W3C Software licence. They are not really ready at the current time, although early, essentially undocumented, versions are available from the OpenMath source repository (see Resources for a link). I expect to update the W3C Math/XSL area with more robust versions of these transformations as part of the Candidate Recommendation "Call for Implementations" activity, hopefully before the end of this year (see Resources).
Clipbboard and Media (Mime) Types
Many existing MathML systems allow you to cut and paste MathML expressions using the operating system clipboard. Unfortunately, different systems vary a lot in the way they handled this. In particular, it can differ in the way that the MathML is just labeled on the clipboard as text or whether it is given a MathML-specific clipboard flavor, and if so which label. MathML 3 for the first time specified in some detail the recommended clipboard behavior, and similarly specifies the Media (MIME) types to use if passing XML fragments through Web services or other systems where MIME types are important. An early version of the clipboard specification is already implemented in MathPlayer MathML renderer for Microsoft® Internet Explorer®, and in Microsoft Word 2007, which allows Math fragments to be reliably cut from Internet Explorer and pasted into Word. Hopefully this clipboard specification in MathML3 will be implemented by the majority of MathML systems, and make it easy to move expressions from one system to another.
A major aim of MathML from its first design phase has been to enable mathematics to be used in Web pages as text, not as images or opaque plugins, to allow searching, the resizing of text, and so on. Currently, this is still harder than it ought to be to serve XHTML as opposed to HTML to a general audience. XHTML is the reformulation of HTML as an XML vocabulary. The original intention was that this, with more rigorous XML parsing rules, would gradually replace HTML as the preferred markup language for the Web. However, browser manufacturers and Web users have shown some reluctance to switch, and in particular the dominant browser, Internet Explorer still has no native support for XHTML.
For a large range of (MathML2) presentation MathML constructs it is possible to mix XHTML and MathML together in the same document. The simplest way to achieve cross-browser support for such a document is to follow these simple rules:
- Serve the document as well formed XML, with HTML elements in the
xhtml namespace (
http://www.w3.org/1999/xhtml) and the MathML elements in the MathML namespace (http://www.w3.org/1998/Math/MathML). - Use the mime type: application/xhtml+xml
- Ensure that the MathML namespace is declared near the top of the
document, most simply by having the namespace declaration
xmlns:m="http://www.w3.org/1998/Math/MathML"on thehtmlelement.
Firefox (all versions) will render such a document natively, although the reader must have previously installed suitable math fonts (see Resources for a link).
Internet Explorer (version 6.0 and later) will render such a document if the reader has installed the free MathPlayer component from Design Science (see Resources for a link). (Note: This use of MathPlayer to get Internet Explorer to render application/xhtml+xml documents should be more widely known, since it's useful even if there is no mathematics in the document.)
Opera, Safari, and other browsers without explicit MathML support can still render a range of such documents by the use of a suitable CSS stylesheet. The use of CSS to render MathML is separated into a separate specification: MathML for CSS (see Resources for a link).
An older and slightly more powerful technique to achieve cross browser support is to use a client side XSLT stylesheet as described in the Math/XSL area, or a slightly updated version detecting more modern browser capabilities as used in the NAG documentation (see Resources for links). Use of XSLT is more difficult to set up, especially as modern releases of browsers increasingly place security restrictions (see Resources for a link) on the use of XSLT. However, it does have the advantage that XSLT can detect the browser used and transform it appropriately, rather than being restricted to the capabilities of the least capable browser.
A third option, rather than using client side XSLT, is to detect the user agent at the server and to provide different content depending on the user agent's capabilities. The details of this depend upon the server environment being used and will not be covered in this article.
Recognizing that XHTML is unlikely to fully replace HTML, work has recently begun to standardize an updated HTML specification, HTML5 (see Resources for a link). The specification of HTML 5 is far from complete but the current drafts specify that MathML can be directly used within an HTML document served with the standard text/html mime type. So, in the near future, it will hopefully be possible to place MathML directly on the Web without needing to worry about mime types and server configurations.
However, without waiting for HTML5 to be standardized, it is still quite easy to serve a document using MathML, which works with very high quality rendering in Firefox and Internet Explorer, and is legible in Opera, Safari, and other browsers with reasonable CSS support. Figure 1 shows the document rendered in the Firefox browser.
Figure 1. MathML document rendered in the Firefox browser
Figure 2 shows the same document rendered in Internet Explorer.
Figure 2. MathML document rendered in Internet Explorer
MathML is becoming the markup language of choice of marking up mathematical expressions. Even if author submissions use other formats (Word, TeX, and so on) they are often translated to MathML for archival purposes of further processing. Not all these processes are public, but you can get a flavor of these "off-Web" uses by looking at MathML support in XSL-FO systems [for example antennahouse or the paper on MathML in Scientific and Technical Publishing published by Design Science (see Resources for links)].
Use of MathML in office software
Modern office editing software, notably Open Office and Microsoft Office, appear to be standardizing on using a zip container for XML as the default file format rather than older, purely binary formats. MathML has an obvious role to play here, either as the native file format within the zip container or as an interoperability layer between different systems.
Currently, Microsoft Office does not use MathML internally (it has its own XML format for mathematics), however it does use MathML on the clipboard for both pasting expressions from elsewhere and for cutting expressions out of Word. (This feature has to be enabled through a check box on the Ribbon menu, but it is a standard feature of Word 2007). OpenOffice (and other systems using the ODF document format) in contrast uses MathML internally in its native file system, although OpenOffice does not currently offer MathML on the clipboard. Hopefully the detailed specification of clipboard behaviour in MathML3 will encourage implementors to implement MathML, cut and pasted in an interoperable way.
While clipboard support is useful for one-off edits and demonstrations, it is less useful if you want to process whole documents. However, the fact that both OOXML and ODF based systems use zipped XML formats means that it is possible to directly interact with the data and extract reusable MathML expressions from either system. See, for example, my blog entries on obtaining XHTML+MathML documents from Word and OpenOffice (see Resources for links).
The MathML support built into Word is only for the newer, Office 2007 versions, but many users of older (and current) systems use the MathType formula editor within Word, which also has MathML support built in.
Conversions to and from MathML
Being an XML vocabulary, MathML is highly suited to transformation using standard XML tools, especially XSLT, the W3C defined general XML transformation language. Transformations to and from OpenMath, and to and from OMML (Word's XML format for Math) and to TeX have already been mentioned in this article. One major area transformation work is the building of transformations to MathML from TeX, the mathematical typesetting system developed by Donald Knuth. Here you need to distinguish systems that try to cope with the extreme variability in TeX documents and convert existing legacy documents to XHTML+MathML, and systems that offer a TeX-like syntax that is specifically aimed at providing a convenient short form authoring syntax for MathML.
Notable systems of the first type are the late Eitan Gurari's tex4ht and Bruce Miller's LateXML (see Resources for links).
Systems offering a TeX like syntax for MathML include Jacques Distler's itex2MML, Peter Jipsen's ASCIIMathML, and Douglas Woodall's LaTeXMathML variant (see Resources for links).
Many specialist math systems will have built-in transformations to MathML, for example the Computer Algebra systems Mathematica and Maple, can both import and export MathML, converting between the XML form and their own internal data structures.
You should now have a complete understanding of MathML, including Presentation MathML and Content MathML. You should also understand all of the advantages that MathML 3.0 will soon bring for marking up mathematics in XML.
Learn
- XML Entity Definitions for Characters (W3C Working Draft 22 October 2009): Review this specification that lists several sets of names which are assigned to Unicode characters.
- HTML 5, A vocabulary and associated APIs for HTML and XHTML (W3C Working Draft 25 August 2009): Learn more about the vocabulary and associated APIs for HTML and XHTML, with a focus on Web applications.
- Arabic mathematical notation (W3C Interest Group Note 31 January 2006): Review the use of MathML for the presentation of mathematics in the notations customarily used with Arabic, and related languages.
- XHTML[tm] 1.0: The Extensible HyperText Markup Language (World Wide Web Consortium Recommendation, 26 January 2000): Read about XHTML 1.0, a reformulation of HTML 4 as an XML 1.0 application, and three DTDs corresponding to the ones defined by HTML 4.
- Mathematical Markup Language (MathML) 1.0 Specification (W3C Recommendation 07-April-1998): Review this XML application that describes mathematical notation and captures both its structure and content.
- Mathematical Markup Language (MathML) Version 2.0 (Second Edition) (W3C Recommendation 21 October 2003): Learn more about how to serve, receive, and process mathematics on the Web.
- Mathematical Markup Language (MathML) Version 3.0 (W3C Working Draft 24 September 2009): Explore recent developments in MathML.
- A MathML for CSS profile (W3C Working Draft 6 October 2009): Read about a profile of MathML 3.0 that admits formatting with Cascading Stylesheets.
- XHTML+MathML documents from
Word and
OpenOffice: See the author's blog entries on obtaining XHTML+MathML documents from these authoring tools.
- David Carlisle: Visit the author's blog on MathML and related issues.
- Just what is TeX?: Visit the TeX User Group and learn about a computer language designed for use in typesetting.
- The OpenMath Standard Version 2.0 (June 2004): Read about OpenMath, a standard for the representation and communication of mathematical objects.
- OpenMath Society: Learn about the symbol that represents the sin function from the OpenMath Content Dictionary: transcendental functions 1.
- antennahouse or the paper on
MathML in Scientific and Technical Publishing published by Design Science: Learn about MathML support in XSL-FO systems.
- XML Linking Language (XLink) Version 1.0 (World Wide Web Consortium Recommendation, 27 June 2001): Learn to insert elements into XML documents to create and describe links between resources.
- The Math/XSL area: Use a client side XSLT stylesheet, an older and slightly more powerful technique to achieve cross browser support, or try a slightly
updated version detecting more modern browser capabilities as used
in the NAG documentation.
- Browser security restrictions: Read how use of XSLT is more difficult to set up, especially as modern releases of browsers
add restrictions.
- XML zone: See the developerWorks XML zone for a wide range of technical articles and tips, tutorials, and standards.
- IBM XML certification: Find out how you can become an IBM-Certified Developer in XML and related technologies.
- XML technical library: See the developerWorks XML Zone for a wide range of technical articles and tips, tutorials, standards, and IBM Redbooks.
- developerWorks technical events and webcasts: Stay current with technology in these sessions.
- developerWorks
podcasts: Listen to interesting interviews and discussions for software developers.
Get products and technologies
- Fonts for MathML-enabled Mozilla: Install
these suitable fonts for Firefox to render a MathML document natively.
- MathPlayer: For Internet Explorer to render a MathML document, install this free component from Design Science. (Note this use of MathPlayer to get Internet Explorer.)
- OpenMath
and Putting mathematics on the Web with MathML (W3C): Find preliminary versions of the
transformations from general Content MathML to Strict Content MathML, from Content MathML to Presentation MathML and presentation MathML to TeX (for typesetting the sample images), all implemented in XSLT 2.
- Eitan Gurari's tex4ht and Bruce Miller's LateXML: Explore these tools that convert mathematical material into content and presentation MathML.
- Jacques Distler's itex2MML,
Peter Jipsen's ASCIIMathML,
and Douglas Woodall's LaTeXMathML variant: Try these systems that offer a TeX like syntax for MathML.
- IBM product evaluation versions: Download or explore the online trials in the IBM SOA Sandbox and get your hands on application development tools and middleware products from DB2®, Lotus®, Rational®, Tivoli®, and WebSphere®.
Discuss
- XML zone discussion forums: Participate in any of several XML-related discussions.
- developerWorks blogs: Check out these blogs and get involved.

David Carlisle is a Senior Technical Consultant at NAG ltd in the UK. He has been involved with OpenMath and MathML since 1998 and has served as co-editor of both the MathML2 and 3 specifications. Prior to working on MathML he was a core member of the LaTeX3 team that designed and implemented the LaTeX2e typesetting system. he also takes an active interset in XSLT and is active on the xsl-list mailing list.




