MathML Core

W3C Editor's Draft

More details about this document
This version:
https://w3c.github.io/mathml-core/
Latest published version:
https://www.w3.org/TR/mathml-core/
Latest editor's draft:
https://w3c.github.io/mathml-core/
History:
https://www.w3.org/standards/history/mathml-core/
Commit history
Test suite:
https://github.com/web-platform-tests/wpt/tree/master/mathml/
Implementation report:
https://wpt.fyi/results/?label=master&label=experimental&aligned&q=math%20%20not%28path%3A%2Fjs%29
Editors:
David Carlisle (NAG)
Frédéric Wang (Igalia)
Former editors:
Patrick Ion (Mathematical Reviews, American Mathematical Society)
Robert Miner (deceased) (Design Science, Inc.)
Feedback:
GitHub w3c/mathml-core (pull requests, new issue, open issues)

Abstract

This specification defines a core subset of Mathematical Markup Language, or MathML, that is suitable for browser implementation. MathML is a markup language for describing mathematical notation and capturing both its structure and content. The goal of MathML is to enable mathematics to be served, received, and processed on the World Wide Web, just as HTML has enabled this functionality for text.

Status of This Document

This section describes the status of this document at the time of its publication. A list of current W3C publications and the latest revision of this technical report can be found in the W3C standards and drafts index.

This document was published by the Math Working Group as an Editor's Draft.

Publication as an Editor's Draft does not imply endorsement by W3C and its Members.

This is a draft document and may be updated, replaced, or obsoleted by other documents at any time. It is inappropriate to cite this document as other than a work in progress.

This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent that the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This document is governed by the 18 August 2025 W3C Process Document.

1. Introduction

This section is non-normative.

The [MATHML3] specification has several shortcomings that make it hard to implement consistently across web rendering engines or to extend with user-defined constructions, e.g.:

This MathML Core specification intends to address these issues by being as accurate as possible on the visual rendering of mathematical formulas using additional rules from the TeXBook’s Appendix G [TEXBOOK] and from the Open Font Format [OPEN-FONT-FORMAT], [OPEN-TYPE-MATH-ILLUMINATED]. It also relies on modern browser implementations and web technologies [HTML] [SVG] [CSS2] [DOM], clarifying interactions with them when needed or introducing new low-level primitives to improve the web platform layering.

Parts of MathML3 that do not fit well in this framework or are less fundamental have been omitted. Instead, they are described in a separate and larger [MATHML4] specification. The details of which math feature will be included in future versions of MathML Core or implemented as polyfills is still open. This question and other potential improvements are tracked on GitHub.

By increasing the level of implementation details, focusing on a workable subset, following a browser-driven design and relying on automated web platform tests, this specification is expected to greatly improve MathML interoperability. Moreover, effort on MathML layering will enable users to implement the rest of the MathML 4 specification, or more generally to extend MathML Core, using modern web technologies such as shadow trees, custom elements or APIs from [HOUDINI].

2. MathML Fundamentals

2.1 Elements and attributes

The term MathML element refers to any element in the MathML namespace. The MathML elements defined in this specification are called the MathML Core elements and are listed below. Any MathML element that is not listed below is called an Unknown MathML element.

  1. a
  2. annotation
  3. annotation-xml
  4. maction
  5. math
  6. merror
  7. mfrac
  8. mi
  9. mmultiscripts
  10. mn
  11. mo
  12. mover
  13. mpadded
  14. mphantom
  15. mprescripts
  16. mroot
  17. mrow
  18. ms
  19. mspace
  20. msqrt
  21. mstyle
  22. msub
  23. msubsup
  24. msup
  25. mtable
  26. mtd
  27. mtext
  28. mtr
  29. munder
  30. munderover
  31. semantics

The grouping elements are a, maction, math, merror, mphantom, mprescripts, mrow, mstyle, semantics and unknown MathML elements.

The scripted elements are mmultiscripts, mover, msub, msubsup, msup, munder and munderover.

The radical elements are mroot and msqrt.

The attributes defined in this specification have no namespace and are called MathML attributes:

2.1.1 The Top-Level <math> Element

MathML specifies a single top-level or root math element, which encapsulates each instance of MathML markup within a document. All other MathML content must be contained in a <math> element.

The <math> element accepts the attributes described in 2.1.3 Global Attributes as well as the following attributes:

The display attribute, if present, must be an ASCII case-insensitive match to block or inline. The user agent stylesheet described in A. User Agent Stylesheet contains rules for this attribute that affect the default values for the display (block math or inline math) and math-style (normal or compact) properties. If the display attribute is absent or has an invalid value, the User Agent stylesheet treats it the same as inline.

This specification does not define any observable behavior that is specific to the alttext attribute.

Note
The alttext attribute may be used as alternative text by some legacy systems that do not implement math layout.

If the <math> element does not have its computed display property equal to block math or inline math then it is laid out according to the CSS specification where the corresponding value is described. Otherwise the layout algorithm of the mrow element is used to produce a math content box. That math content box is used as the content for the layout of the element, as described by CSS for display: block (if the computed value is block math) or display: inline (if the computed value is inline math). Additionally, if the computed display property is equal to block math then that math content box is rendered horizontally centered within the content box.

Note
TEX's display mode $$...$$ and inline mode $...$ correspond to display="block" and display="inline" respectively.

In the following example, a math formula is rendered in display mode on a new line and taking full width, with the math content centered within the container:

<div style="width: 15em;">
  This mathematical formula with a big summation and the number pi
  <math display="block" style="border: 1px dotted black;">
    <mrow>
      <munderover>
        <mo></mo>
        <mrow><mi>n</mi><mo>=</mo><mn>1</mn></mrow>
        <mrow><mo>+</mo><mn></mn></mrow>
      </munderover>
      <mfrac>
        <mn>1</mn>
        <msup><mi>n</mi><mn>2</mn></msup>
      </mfrac>
    </mrow>
    <mo>=</mo>
    <mfrac>
      <msup><mi>π</mi><mn>2</mn></msup>
      <mn>6</mn>
    </mfrac>
  </math>
  is easy to prove.
</div>
math example (display)

As a comparison, the same formula would look as follows in inline mode. The formula is embedded in the paragraph of text without forced line breaking. The baselines specified by the layout algorithm of the mrow are used for vertical alignment. Note that the middle of sum and equal symbols or fractions are all aligned, but not with the alphabetical baseline of the surrounding text.

math example (inline)

Because good mathematical rendering requires use of mathematical fonts, the user agent stylesheet should set the font-family to the math value on the <math> element instead of inheriting it. Additionally, several CSS properties that can be set on a parent container such as font-style, font-weight, direction or text-indent etc are not expected to apply to the math formula and so the user agent stylesheet has rules to reset them by default.

math {
  direction: ltr;
  text-indent: 0;
  letter-spacing: normal;
  line-height: normal;
  word-spacing: normal;
  font-family: math;
  font-size: inherit;
  font-style: normal;
  font-weight: normal;
  display: inline math;
  math-shift: normal;
  math-style: compact;
  math-depth: 0;
}
math[display="block" i] {
  display: block math;
  math-style: normal;
}
math[display="inline" i] {
  display: inline math;
  math-style: compact;
}

2.1.2 Types for MathML Attribute Values

In addition to CSS data types, some MathML attributes rely on the following MathML-specific types:

unsigned-integer
An <integer> value as defined in [CSS-VALUES-4], whose first character is neither U+002D HYPHEN-MINUS character (-) nor U+002B PLUS SIGN (+).
boolean
A string that is an ASCII case-insensitive match to true or false.

2.1.3 Global Attributes

The following attributes are common to and may be specified on all MathML elements:

2.1.4 Attributes common to HTML and MathML elements

The id, class, style, data-*, autofocus and nonce and tabindex attributes have the same syntax and semantics as defined for id, class, style, data-*, autofocus, nonce and tabindex attributes on HTML elements.

The dir attribute, if present, must be an ASCII case-insensitive match to ltr or rtl. In that case, the user agent is expected to treat the attribute as a presentational hint setting the element's direction property to the corresponding value. More precisely, an ASCII case-insensitive match to rtl is mapped to rtl while an ASCII case-insensitive match to ltr is mapped to ltr.

Note
The dir attribute is used to set the directionality of math formulas, which is often rtl in Arabic speaking world. However, languages written from right to left often embed math written from left to right and so the user agent stylesheet resets the direction property accordingly on the math elements.

In the following example, the dir attribute is used to render "𞸎 plus 𞸑 raised to the power of (٢ over, 𞸟 plus ١)" from right-to-left.

<math dir="rtl">
  <mrow>
    <mi>𞸎</mi>
    <mo>+</mo>
    <msup>
      <mi>𞸑</mi>
      <mfrac>
        <mn>٢</mn>
        <mrow>
          <mi>𞸟</mi>
          <mo>+</mo>
          <mn>١</mn>
        </mrow>
      </mfrac>
    </msup>
  </mrow>
</math>
dir example

All MathML elements support event handler content attributes, as described in event handler content attributes in HTML.

All event handler content attributes noted by HTML as being supported by all HTMLElements are supported by all MathML elements as well, as defined in the MathMLElement IDL.

2.1.5 Legacy MathML Style Attributes

The mathcolor and mathbackground attributes, if present, must have a value that is a <color>. In that case, the user agent is expected to treat these attributes as a presentational hint setting the element's color and background-color properties to the corresponding values. The mathcolor attribute describes the foreground fill color of MathML text, bars etc while the mathbackground attribute describes the background color of an element.

The mathsize attribute, if present, must have a value that is a valid <length-percentage>. In that case, the user agent is expected to treat the attribute as a presentational hint setting the element's font-size property to the corresponding value. The mathsize property indicates the desired height of glyphs in math formulas but also scales other parts (spacing, shifts, line thickness of bars etc) accordingly.

Note
The above attributes are implemented for compatibility with full MathML. Authors whose only target is MathML Core are encouraged to use CSS for styling.

2.1.6 The displaystyle and scriptlevel attributes

The displaystyle attribute, if present, must have a value that is a boolean. In that case, the user agent is expected to treat the attribute as a presentational hint setting the element's math-style property to the corresponding value. More precisely, an ASCII case-insensitive match to true is mapped to normal while an ASCII case-insensitive match to false is mapped to compact. This attribute indicates whether formulas should try to minimize the logical height (value is false) or not (value is true) e.g. by changing the size of content or the layout of scripts.

The scriptlevel attribute, if present, must have value +<U>, -<U> or <U> where <U> is an unsigned-integer. In that case the user agent is expected to treat the scriptlevel attribute as a presentational hint setting the element's math-depth property to the corresponding value. More precisely, +<U>, -<U> and <U> are respectively mapped to add(<U>) add(<-U>) and <U>.

displaystyle and scriptlevel values are automatically adjusted within MathML elements. To fully implement these attributes, additional CSS properties must be specified in the user agent stylesheet as described in A. User Agent Stylesheet. In particular, for all MathML elements a default font-size: math is specified to ensure that scriptlevel changes are taken into account.

In this example, an munder element is used to attach a script "A" to a base "∑". By default, the summation symbol is rendered with the font-size inherited from its parent and the A as a scaled down subscript. If displaystyle is true, the summation symbol is drawn bigger and the "A" becomes an underscript. If scriptlevel is reset to 0 on the "A", then it will use the same font-size as the top-level math root.

<math>
  <munder>
    <mo></mo>
    <mi>A</mi>
  </munder>
  <munder displaystyle="true">
    <mo></mo>
    <mi>A</mi>
  </munder>
  <munder>
    <mo></mo>
    <mi scriptlevel="0">A</mi>
  </munder>
</math>
displaystyle-scriptlevel example
Note
TEX's \displaystyle, \textstyle, \scriptstyle, and \scriptscriptstyle correspond to displaystyle and scriptlevel as true and 0, false and 0, false and 1, and false and 2, respectively.

2.1.7 Attributes Reserved as Valid

The attributes intent and arg are reserved as valid attributes.

This specification does not define any observable behavior that is specific to the intent and arg attributes.

Note
These attributes are described in [MATHML4] and future versions of this specification may or may not define them. Authors should be aware that they are currently in development and subject to change.

2.2 Integration in the Web Platform

2.2.1 HTML and SVG

MathML can be mixed with HTML and SVG as described in the relevant specifications [HTML] [SVG].

When evaluating the SVG requiredExtensions attribute, user agents must claim support for the language extension identified by the MathML namespace.

In this example, inline MathML and SVG elements are used inside an HTML document. SVG elements <switch> and <foreignObject> (with proper <requiredExtensions>) are used to embed a MathML formula with a text fallback, inside a diagram. HTML input element is used within the mtext to include an interactive input field inside a mathematical formula. See also 3.8 Semantics and Presentation for an example of SVG and HTML inside an annotation-xml element.

<svg style="font-size: 20px" width="400px" height="220px" viewBox="0 0 200 110">
  <g transform="translate(10,80)">
    <path d="M 0 0 L 150 0 A 75 75 0 0 0 0 0
             M 30 0 L 30 -60 M 30 -10 L 40 -10 L 40 0"
          fill="none" stroke="black"></path>
    <text transform="translate(10,20)">1</text>
    <switch transform="translate(35,-40)">
      <foreignObject width="200" height="50"
                     requiredExtensions="http://www.w3.org/1998/Math/MathML">
        <math>
          <msqrt>
            <mn>2</mn>
            <mi>r</mi>
            <mo></mo>
            <mn>1</mn>
          </msqrt>
        </math>
      </foreignObject>
      <text>\sqrt{2r - 1}</text>
    </switch>
  </g>
</svg>

<p>
  Fill the blank:
  <math>
    <msqrt>
      <mn>2</mn>
      <mtext><input onchange="..." size="2" type="text"></mtext>
      <mo></mo>
      <mn>1</mn>
    </msqrt>
    <mo>=</mo>
    <mn>3</mn>
  </math>
</p>
html-svg example

2.2.2 CSS styling

User agents must support various CSS features mentioned in this specification, including new ones described in 4. CSS Extensions for Math Layout. They must follow the computation rule for display: contents.

In this example, the MathML formula inherits the CSS color of its parent and uses the font-family specified via the style attribute.

<div style="width: 15em; color: blue">
  This mathematical formula with a big summation and the number pi
  <math display="block" style="font-family: STIX Two Math">
    <mrow>
      <munderover>
        <mo></mo>
        <mrow><mi>n</mi><mo>=</mo><mn>1</mn></mrow>
        <mrow><mo>+</mo><mn></mn></mrow>
      </munderover>
      <mfrac>
        <mn>1</mn>
        <msup><mi>n</mi><mn>2</mn></msup>
      </mfrac>
    </mrow>
    <mo>=</mo>
    <mfrac>
      <msup><mi>π</mi><mn>2</mn></msup>
      <mn>6</mn>
    </mfrac>
  </math>
  is easy to prove.
</div>
style example

All documents containing MathML Core elements must include CSS rules described in A. User Agent Stylesheet as part of user-agent level style sheet defaults. In particular, this adds !important rules to force writing mode to horizontal-lr on all MathML elements.

The float property does not create floating of elements whose parent's computed display value is block math or inline math, and does not take them out-of-flow.

The ::first-line and ::first-letter pseudo-elements do not apply to elements whose computed display value is block math or inline math, and such elements do not contribute a first formatted line or first letter to their ancestors.

The following CSS features are not supported and must be ignored:

  • Line breaking inside math formulas: white-space is treated as nowrap on all MathML elements.
  • Alignment properties: align-content, justify-content, align-self, justify-self have no effects on MathML elements.
Note
These features might be handled in future versions of this document. For now, authors are discouraged from setting a different value for these properties as that might lead to backward incompatibility issues.

2.2.3 DOM and JavaScript

User agents supporting Web application APIs must ensure that they keep the visual rendering of MathML synchronized with the [DOM] tree, in particular perform necessary updates when MathML attributes are modified dynamically.

All the nodes representing MathML elements in the DOM must implement, and expose to scripts, the following MathMLElement interface, unless otherwise specified.

WebIDL[Exposed=Window]
interface MathMLElement : Element { };
MathMLElement includes GlobalEventHandlers;
MathMLElement includes HTMLOrSVGOrMathMLElement;

The GlobalEventHandlers and HTMLOrSVGOrMathMLElement interfaces are defined in [HTML].

The style IDL attribute is defined in [CSSOM].

In the following example, a MathML formula is used to render the fraction "α over 2". When clicking the red α, it is changed into a blue β.

<script>
  function ModifyMath(mi) {
      mi.style.color = 'blue';
      mi.textContent = 'β';
  }
</script>
<math>
  <mrow>
    <mfrac>
      <mi style="color: red" onclick="ModifyMath(this)">α</mi>
      <mn>2</mn>
    </mfrac>
  </mrow>
</math>
dom-idl example

2.2.4 Text layout

Because math fonts generally contain very tall glyphs such as big integrals, using typographic metrics is important to avoid excessive line spacing of text. As a consequence, user agents must take into account the USE_TYPO_METRICS flag from the OS/2 table [OPEN-FONT-FORMAT] when performing text layout.

2.2.5 Focus

MathML provides the ability for authors to allow for interactivity in supporting interactive user agents using the same concepts, approach and guidance to Focus as described in HTML, with modifications or clarifications regarding application for MathML as described in this section.

When an element is focused, all applicable CSS focus-related pseudo-classes as defined in Selectors Level 3 apply, as defined in that specification.

The contents of embedded math elements (including HTML elements inside token elements) contribute to the sequential focus order of the containing owner HTML document (combined sequential focus order).

3. Presentation Markup

3.1 Visual formatting model

3.1.1 Box Model

The default display property is described in A. User Agent Stylesheet:

  • For the <math> root, it is equal to inline math or block math according to the value of the display attribute.
  • For Tabular MathML elements mtable, mtr, mtd it is respectively equal to inline-table, table-row and table-cell.
  • For all but the first children of the maction and semantics elements, it is equal to none.
  • For all the other MathML elements it is equal to block math.

In order to specify math layout in different writing modes, this specification uses concepts from [CSS-WRITING-MODES-4]:

Note
Unless specified otherwise, the figures in this specification use a writing mode of horizontal-lr and ltr. See Figure 4, Figure 5 and Figure 6 for examples of other writing modes that are sometimes used for math layout.

Boxes used for MathML elements rely on several parameters in order to perform layout in a way that is compatible with CSS but also to take into account very accurate positions and spacing within math formulas:

  1. Inline metrics. min-content inline size, max-content inline size and inline size from CSS. See Figure 1.
    Figure 1 Generic Box Model for MathML elements
  2. Block metrics. The block size, first baseline set and last baseline set. The following baselines are defined for MathML boxes:

    1. The alphabetic baseline which typically aligns with the bottom of uppercase Latin glyphs. The algebraic distance from the alphabetic baseline to the line-over edge of the box is called the line-ascent. The algebraic distance from the line-under edge to the alphabetic baseline of the box is called the line-descent.
    2. The mathematical baseline, also called math axis, which typically aligns with the fraction bar, middle of fences and binary operators. It is shifted away from the alphabetic baseline by AxisHeight towards the line-over.
    3. The ink-over baseline, indicating the line-over theorical limit of the math content drawn, excluding any extra space. If not specified, it is aligned with the line-over edge. The algebraic distance from the alphabetic baseline to the ink-over baseline is called the ink line-ascent.
    4. The ink-under baseline, indicating the line-under theorical limit of the math content drawn, excluding any extra space. If not specified, it is aligned with the line-under edge. The algebraic distance from the ink-under baseline to the alphabetic baseline is called the ink line-descent.
    Note
    For math layout, it is very important to rely on the ink extent when positioning text. This is not the case for more complex notations (e.g. square root). Although ink-ascent and ink-descent are defined for all MathML elements they are really only used for the token elements. In other cases, they just match normal ascent and descent.
    Unless specified otherwise, the last baseline set is equal to the first baseline set for MathML boxes.
  3. An optional italic correction which provides a measure of how much the text of a box is slanted along the inline axis. See Figure 2.
    Figure 2 Examples of italic correction for italic f and large integral
    If it is requested during calculation of min-content inline size and max-content inline size or during layout then 0 is used as a fallback value.
  4. An optional top accent attachment which provides a reference offset on the inline axis of a box that should be used when positioning that box as an accent. See Figure 3.
    Figure 3 Example of top accent attachment for a circumflex accent
    If it is requested during calculation of min-content inline size (respectively max-content inline size) then half the min-content inline size (respectively max-content inline size) is used as a fallback value. If it is requested during layout then half the inline size of the box is used as a fallback value.

Given a MathML box, the following offsets are defined:

  • The inline offset of a child box is the offset between the inline-start edge of the parent box and the inline-start edge of the child box.
  • The block offset of a child box is the offset between the block-start edge of the parent box and the block-start edge of the child box.
  • The line-left offset of a child box is the offset between the line-left edge of the parent box and the line-left edge of the child box.
Figure 4 Box model for writing mode horizontal-tb and rtl that may be used in e.g. Arabic math.
Figure 5 Box model for writing mode vertical-lr and ltr that may be used in e.g. Mongolian math.
Figure 6 Box model for writing mode vertical-rl and ltr that may be used in e.g. Japanese math.
Note
The position of child boxes and graphical items inside a MathML box are expressed using the inline offset and block offset. For convenience, the layout algorithms may describe offsets using flow-relative directions, line-relative directions or the alphabetic baseline. It is always possible to pass from one description to the other because position of child boxes is always performed after the metrics of the box and of its child boxes are calculated.

Here are examples of offsets obtained from line-relative metrics:

Issue 78: Ink ascent/descent opentype/texneeds-tests
Improve definition of ink ascent/descent?

3.1.2 Layout Algorithms

Each MathML element has an associated math content box, which is calculated as described in this chapter's layout algorithms using the following structure:

  1. Calculation of min-content inline size and max-content inline size of the math content.
  2. Box layout:
    1. Layout of in-flow child boxes.
    2. Calculation of inline size, ink line-ascent, ink line-descent, line-ascent and line-ascent of the math content.
    3. Calculation of offsets of child boxes within the math content box as well as sizes and offsets of extra graphical items (bars, radical symbol, etc).
    4. Layout and positioning of absolutely-positioned and fixed-positioned boxes, as described in [CSS-POSITION-3].

The following extra steps must be performed:

Note
Per the description above, margin-collapsing does not apply to MathML elements.

During box layout, optional inline stretch size constraint and block stretch size constraint parameters may be used on embellished operators. The former indicates a target size that a core operator stretched along the inline axis should cover. The latter indicates an ink line-ascent and ink line-descent that a core operator stretched along the block axis should cover. Unless specified otherwise, these parameters are ignored during box layout and child boxes are laid out without any stretch size constraint.

Issue 76: Define what inline percentages resolve against. css/html5need specification updateneeds-tests
Define what inline percentages resolve against
Issue 77: Define what block percentages resolve against. css/html5need specification updateneeds-tests
Define what block percentages resolve against

3.1.3 Anonymous <mrow> boxes

An anonymous box is a box without any associated element in the DOM tree and which is generated for layout purpose only. The properties of anonymous boxes are inherited from the enclosing non-anonymous box while non-inherited properties have their initial value. An anonymous <mrow> box is an anonymous box with display equal to block math and which is laid out as described in section 3.3.1.2 Layout of <mrow>.

If a MathML element generates an anonymous <mrow> box then it wraps its children in an anonymous <mrow> box. I.e., its subtree in the visual formatting model is made of an anonymous <mrow> box which itself contains the boxes associated to the children of this MathML element.

In the following example, the math and mrow elements are laid out as described in section 3.3.1.2 Layout of <mrow>. In particular, the <math> element adds proper spacing around its <mo>≠</mo> child and the <mrow> element stretches its <mo>|</mo> children vertically.

The mtd element has display: table-cell and the msqrt element displays a radical symbol around its children. However, they also place their children in a way that is similar to what is described in section 3.3.1.2 Layout of <mrow>: the <msqrt> element adds proper spacing around its <mo>+</mo> child while the <mtd> element stretches its <mo> children vertically. In order to make this possible, each of these two elements generates an anonymous <mrow> box.

<math>
  <mrow>
    <mo>|</mo>
    <mtable>
      <mtr>
        <mtd>
          <mi>x</mi>
        </mtd>
        <mtd>
          <mo>(</mo>
          <mfrac linethickness="0">
            <mn>5</mn>
            <mn>3</mn>
          </mfrac>
          <mo>)</mo>
        </mtd>
      </mtr>
      <mtr>
        <mtd>
          <msqrt>
            <mn>7</mn>
            <mo>+</mo>
            <mn>2</mn>
          </msqrt>
        </mtd>
        <mtd>
          <mi>y</mi>
        </mtd>
      </mtr>
    </mtable>
    <mo>|</mo>
  </mrow>
  <mo></mo>
  <mn>0</mn>
</math>
math example (display)

3.1.4 Stacking contexts

MathML elements can overlap due to various spacing rules. They can as well contain extra graphical items (bars, radical symbol, etc). A MathML element with computed style display: block math or display: inline math generates a new stacking context. The painting order of in-flow children of such a MathML element is exactly the same as block elements. The extra graphical items are painted after text and background (right after step 7.2.4 for display: inline math and right after step 7.2 for display: block math).

3.2 Token Elements

Token elements in presentation markup are broadly intended to represent the smallest units of mathematical notation which carry meaning. Tokens are roughly analogous to words in text. However, because of the precise, symbolic nature of mathematical notation, the various categories and properties of token elements figure prominently in MathML markup. By contrast, in textual data, individual words rarely need to be marked up or styled specially.

Note
In practice, most MathML token elements just contain simple text for variables, numbers, operators etc and don't need sophisticated layout. However, it can contain text with line breaks or arbitrary HTML5 phrasing elements.

3.2.1 Text <mtext>

The mtext element is used to represent arbitrary text that should be rendered as itself. In general, the <mtext> element is intended to denote commentary text.

The <mtext> element accepts the attributes described in 2.1.3 Global Attributes.

In the following example, mtext is used to put conditional words in a definition:

<math>
  <mi>y</mi>
  <mo>=</mo>
  <mrow>
    <msup>
      <mi>x</mi>
      <mn>2</mn>
    </msup>
    <mtext>&nbsp;if&nbsp;</mtext>
    <mrow>
      <mi>x</mi>
      <mo></mo>
      <mn>1</mn>
    </mrow>
    <mtext>&nbsp;and&nbsp;</mtext>
    <mn>2</mn>
    <mtext>&nbsp;otherwise.</mtext>
  </mrow>
</math>
mtext example
3.2.1.1 Layout of <mtext>

If the element does not have its computed display property equal to block math or inline math then it is laid out according to the CSS specification where the corresponding value is described. Otherwise, the layout below is performed.

If the <mtext> element contains only text content without forced line break or soft wrap opportunity then, the anonymous child node generated for that text is laid out as defined in the relevant CSS specification and:

Otherwise, the mtext element is laid out as a block box and corresponding min-content inline size, max-content inline size, inline size, block size, first baseline set and last baseline set are used for the math content box.

3.2.2 Identifier <mi>

The mi element represents a symbolic name or arbitrary text that should be rendered as an identifier. Identifiers can include variables, function names, and symbolic constants.

The <mi> element accepts the attributes described in 2.1.3 Global Attributes as well as the following attribute:

The layout algorithm is the same as the mtext element. The user agent stylesheet must contain the following property in order to implement automatic italic via the text-transform value introduced in 4.2 The math-auto transform:

mi {
  text-transform: math-auto;
}

The mathvariant attribute, if present, must be an ASCII case-insensitive match of normal. In that case, the user agent is expected to treat the attribute as a presentational hint setting the element's text-transform property to none. Otherwise it has no effects.

Note

In [MathML3], the mathvariant attribute was used to define logical classes of token elements, each class providing a collection of typographically-related symbolic tokens with specific meaning within a given mathematical expression.

In MathML Core, this attribute is only used to cancel automatic italic of the mi element. For other use cases, the proper Mathematical Alphanumeric Symbols [UNICODE] should be used instead. See also section C. Mathematical Alphanumeric Symbols.

In the following example, mi is used to render variables and function names. Note that per 4.2 The math-auto transform the default style text-transform: math-auto has no effect on the first <mi> ("cos" is made of three characters), makes the second <mi> render as math italic ("c" is made of a single character U+0063 Latin Small Letter C which is mapped to U+1D450 Mathematical Italic Small C per the italic table), has no effect on the third <mi> (overridden by mathvariant="normal", setting text-transform to none) or on the fourth <mi> (no mapping defined for U+221E Infinity in the italic table).

<math>
  <mi>cos</mi>
  <mo>,</mo>
  <mi>c</mi>
  <mo>,</mo>
  <mi mathvariant="normal">c</mi>
  <mo>,</mo>
  <mi></mi>
</math>
mi example

3.2.3 Number <mn>

The mn element represents a "numeric literal" or other data that should be rendered as a numeric literal. Generally speaking, a numeric literal is a sequence of digits, perhaps including a decimal point, representing an unsigned integer or real number.

The <mn> element accepts the attributes described in 2.1.3 Global Attributes. Its layout algorithm is the same as the mtext element.

In the following example, mn is used to write a decimal number.

<math>
  <mn>3.141592653589793</mn>
</math>
mn example

3.2.4 Operator, Fence, Separator or Accent <mo>

The mo element represents an operator or anything that should be rendered as an operator. In general, the notational conventions for mathematical operators are quite complicated, and therefore MathML provides a relatively sophisticated mechanism for specifying the rendering behavior of an <mo> element.

As a consequence, in MathML the list of things that should "render as an operator" includes a number of notations that are not mathematical operators in the ordinary sense. Besides ordinary operators with infix, prefix, or postfix forms, these include fence characters such as braces, parentheses, and "absolute value" bars; separators such as comma and semicolon; and mathematical accents such as a bar or tilde over a symbol. This chapter uses the term "operator" to refer to operators in this broad sense.

The <mo> element accepts the attributes described in 2.1.3 Global Attributes as well as the following attributes:

This specification does not define any observable behavior that is specific to the fence and separator attributes.

Note
Authors may use the fence and separator to describe specific semantics of operators. The default values may be determined from the Operators_fence and Operators_separator tables, or equivalently the human-readable version of the operator dictionary.

In the following example, the mo element is used for the binary operator +. Default spacing is symmetric around that operator. A tighter spacing is used if you rely on the form attribute to force it to be treated as a prefix operator. Spacing can also be specified explicitly using the lspace and rspace attributes.

<math>
  <mn>1</mn>
  <mo>+</mo>
  <mn>2</mn>
  <mo form="prefix">+</mo>
  <mn>3</mn>
  <mo lspace="2em">+</mo>
  <mn>4</mn>
  <mo rspace="3em">+</mo>
  <mn>5</mn>
</math>
mo example 1

Another use case is for big operators such as summation. When displaystyle is true, such an operator is drawn larger but one can change that with the largeop attribute. When displaystyle is false, underscripts are actually rendered as subscripts but one can change that with the movablelimits attribute.

<math>
  <mrow displaystyle="true">
  <munder>
    <mo></mo>
    <mn>5</mn>
  </munder>
  <munder>
    <mo largeop="false"></mo>
    <mn>6</mn>
  </munder>
  </mrow>
  <mrow>
    <munder>
      <mo></mo>
      <mn>5</mn>
    </munder>
    <munder>
      <mo movablelimits="false"></mo>
      <mn>7</mn>
    </munder>
  </mrow>
</math>
mo example 2

Operators are also used for stretchy symbols such as fences, accents, arrows etc. In the following example, the vertical arrow stretches to the height of the mspace element. One can override default stretch behavior with the stretchy attribute e.g. to force an unstretched arrow. The symmetric attribute allows to indicate whether the operator should stretch symmetrically above and below the math axis (fraction bar). Finally the minsize and maxsize attributes add additional constraints over the stretch size.

<math>
  <mfrac>
    <mspace height="50px" depth="50px" width="10px" style="background: blue"/>
    <mspace height="25px" depth="25px" width="10px" style="background: green"/>
  </mfrac>
  <mo></mo>
  <mo stretchy="false"></mo>
  <mo symmetric="true"></mo>
  <mo minsize="250px"></mo>
  <mo maxsize="50px"></mo>
</math>
mo example 3

Note that the default properties of operators are dictionary-based, as explained in 3.2.4.2 Dictionary-based attributes. For example a binary operator typically has default symmetric spacing around it while a fence is generally stretchy by default.

3.2.4.1 Embellished operators

A MathML Core element is an embellished operator if it is:

  1. An mo element;
  2. a scripted element or an mfrac, whose first in-flow child exists and is an embellished operator;
  3. a grouping element or mpadded, whose in-flow children consist (in any order) of one embellished operator and zero or more space-like elements.

The core operator of an embellished operator is the <mo> element defined recursively as follows:

  1. The core operator of an mo element; is the element itself.
  2. The core operator of an embellished scripted element or mfrac element is the core operator of its first in-flow child.
  3. The core operator of an embellished grouping element or mpadded is the core operator of its unique embellished operator in-flow child.

The stretch axis of an embellished operator is inline if its core operator contains only text content made of a single character c, and that character has inline intrinsic stretch axis. Otherwise, the stretch axis of the embellished operator is block.

The same definitions apply for boxes in the visual formatting model where an anonymous <mrow> box is treated as a grouping element.

3.2.4.2 Dictionary-based attributes

The form property of an embellished operator is either infix, prefix or postfix. The corresponding form attribute on the mo element, if present, must be an ASCII case-insensitive match to one of these values.

The algorithm for determining the form of an embellished operator is as follows:

  1. If the form attribute is present and valid on the core operator, then its ASCII lowercased value is used.
  2. If the embellished operator is the first in-flow child of a grouping element, mpadded or msqrt with more than one in-flow child (ignoring all space-like children) then it has form prefix.
  3. Or, if the embellished operator is the last in-flow child of a grouping element, mpadded or msqrt with more than one in-flow child (ignoring all space-like children) then it has form postfix.
  4. Or, if the embellished operator is an in-flow child of a scripted element, other than the first in-flow child, then it has form postfix.
  5. Otherwise, the embellished operator has form infix.

The stretchy, symmetric, largeop, movablelimits properties of an embellished operator are either false or true. In the latter case, it is said that the embellished operator has the property. The corresponding stretchy, symmetric, largeop, movablelimits attributes on the mo element, if present, must be a boolean.

The lspace, rspace, minsize properties of an embellished operator are <length-percentage>. The maxsize property of an embellished operator is either a <length-percentage> or ∞. The lspace, rspace, minsize and maxsize attributes on the mo element, if present, must be a <length-percentage>.

The algorithm for determining the properties of an embellished operator is as follows:

  1. If the corresponding stretchy, symmetric, largeop, movablelimits, lspace, rspace, maxsize or minsize attribute is present and valid on the core operator, then the ASCII lowercased value of this property is used.
  2. Otherwise, run the algorithm for determining the form of an embellished operator.
  3. If the core operator contains only text content Content, then set Category to the result of the algorithm to determine the category of an operator (Content, Form) where Form is the form calculated at the previous step.
  4. If Category is Default and the form of embellished operator was not explicitly specified as an attribute on its core operator:
    1. Set Category to the result of the algorithm to determine the category of an operator (Content, Form) where Form is infix.
    2. If Category is Default, then run the algorithm again with Form set to postfix.
    3. If Category is Default, then run the algorithm again with Form set to prefix.
  5. Run the algorithm to set the properties of an operator from its category Category.

When used during layout, the values of stretchy, symmetric, largeop, movablelimits, lspace, rspace, minsize are obtained by the algorithm for determining the properties of an embellished operator with the following extra resolutions:

  • Percentage values for lspace, rspace are interpreted relative to the value read from the dictionary or to the fallback value above.
  • Interpretation of percentage values for minsize and maxsize are described in 3.2.4.3 Layout of operators.
  • Font-relative lengths for lspace, rspace, minsize and maxsize rely on the font style of the core operator, not the one of the embellished operator.
3.2.4.3 Layout of operators

If the <mo> element does not have its computed display property equal to block math or inline math then it is laid out according to the CSS specification where the corresponding value is described. Otherwise, the layout below is performed.

The text of the operator must only be painted if the visibility of the <mo> element is visible. In that case, it must be painted with the color of the <mo> element.

Let dir be the element's computed direction.

Operators are laid out as follows:

  1. If the content of the <mo> element is not made of a single character c then fall back to the layout algorithm of 3.2.1.1 Layout of <mtext>. If it is not possible to get a glyph corresponding to c given directionality dir, then fall back to the layout algorithm of 3.2.1.1 Layout of <mtext>. Otherwise, let g be the result of running get a glyph corresponding to c given directionality dir.
  2. If the operator has the stretchy property:
  3. If the operator has the largeop property and if math-style on the <mo> element is normal, then:
    1. If it is not possible to shape a stretchy glyph g in the block direction with the first available font then fall back to the layout algorithm of 3.2.1.1 Layout of <mtext>.
      Note
      Here we treat a non-stretchy largeop glyph as stretchy with target dimension DisplayOperatorMinHeight.
    2. The min-content inline size and max-content inline size of the math content are set to the preferred inline size of a glyph stretched along the block axis.
    3. The inline size, ink line-ascent, ink line-descent, line-ascent and line-descent of the math content are obtained by the algorithm to shape a stretchy glyph g to block dimension DisplayOperatorMinHeight. The inline size of the math content is the width of the stretchy glyph. The stretchy glyph is shifted towards the line-under by a value Δ so that its center aligns with the center of the target when symmetric: the ink ascent of the math content is the ascent of the stretchy glyph − Δ and the ink descent of the math content is the descent of the stretchy glyph + Δ.
      • If the operator has the symmetric property, then Δ = [(ascent of stretchy glyph − descent of stretchy glyph) − 2 * AxisHeight] / 2.
      • Otherwise, Δ = 0.
      Note
      The point of Δ here is simply to vertically align the operator when symmetric.
    4. The painting of the operator is performed by the algorithm to shape a stretchy glyph g stretched to block dimension DisplayOperatorMinHeight and at position determined by the previous box metrics shifted by Δ towards the line-over.
    Figure 8 Base and displaystyle sizes of the summation symbol
  4. Otherwise fall back to the layout algorithm of 3.2.1.1 Layout of <mtext>.

If the algorithm to shape a stretchy glyph has been used for one of the step above, then the italic correction of the math content is set to the value returned by that algorithm.

3.2.5 Space <mspace>

The mspace empty element represents a blank space of any desired size, as set by its attributes.

The <mspace> element accepts the attributes described in 2.1.3 Global Attributes as well as the following attributes:

The width, height, depth, if present, must have a value that is a valid <length-percentage>.

  • If the width attribute is present, valid and not a percentage then that attribute is used as a presentational hint setting the element's width property to the corresponding value.
  • If the height attribute is absent, invalid or a percentage then the requested line-ascent is 0. Otherwise the requested line-ascent is the resolved value of the height attribute, clamping negative values to 0.
  • If both the height and depth attributes are present, valid and not a percentage then they are used as a presentational hint setting the element's height property to the concatenation of the strings "calc(", the height attribute value, " + ", the depth attribute value, and ")". If only one of these attributes is present, valid and not a percentage then it is treated as a presentational hint setting the element's height property to the corresponding value.

In the following example, mspace is used to force spacing within the formula (a 1px blue border is added to easily visualize the space):

<math>
  <mn>1</mn>
  <mspace width="1em"
          style="border-top: 1px solid blue"/> 
  <mfrac>
    <mrow>
      <mn>2</mn>
      <mspace depth="1em"
              style="border-left: 1px solid blue"/>
    </mrow>
    <mrow>
      <mn>3</mn>
      <mspace height="2em"
              style="border-left: 1px solid blue"/>
    </mrow>
  </mfrac>
</math>
mspace example

If the <mspace> element does not have its computed display property equal to block math or inline math then it is laid out according to the CSS specification where the corresponding value is described. Otherwise, the <mspace> element is laid out as shown on Figure 9. The min-content inline size, max-content inline size and inline size of the math content are equal to the resolved value of the width property. The block size of the math content is equal to the resolved value of the height property. The line-ascent of the math content is equal to the requested line-ascent determined above.

Figure 9 Box model for the <mspace> element
Note
The terminology height/depth comes from [MATHML3], itself inspired from [TEXBOOK].
3.2.5.1 Definition of space-like elements

A number of MathML presentation elements are "space-like" in the sense that they typically render as whitespace, and do not affect the mathematical meaning of the expressions in which they appear. As a consequence, these elements often function in somewhat exceptional ways in other MathML expressions.

A MathML Core element is a space-like element if it is:

  1. an mtext or mspace;
  2. or a grouping element or mpadded all of whose in-flow children are space-like.

The same definitions apply for boxes in the visual formatting model where an anonymous <mrow> box is treated as a grouping element.

Note
Note that an mphantom is not automatically defined to be space-like, unless its content is space-like. This is because operator spacing is affected by whether adjacent elements are space-like. Since the <mphantom> element is primarily intended as an aid in aligning expressions, operators adjacent to an <mphantom> should behave as if they were adjacent to the contents of the <mphantom>, rather than to an equivalently sized area of whitespace.

3.2.6 String Literal <ms>

ms element is used to represent "string literals" in expressions meant to be interpreted by computer algebra systems or other systems containing "programming languages".

The <ms> element accepts the attributes described in 2.1.3 Global Attributes. Its layout algorithm is the same as the mtext element.

In the following example, ms is used to write a literal string of characters:

<math>
  <mi>s</mi>
  <mo>=</mo>
  <ms>"hello world"</ms>
</math>
ms example
Note
In MathML3, it was possible to use the lquote and rquote attributes to respectively specify the strings to use as opening and closing quotes. These are no longer supported and the quotes must instead be specified as part of the text of the <ms> element. One can add CSS rules to legacy documents in order to preserve visual rendering. For example, in left-to-right direction:
ms:before, ms:after {
  content: "\0022";
}
ms[lquote]:before {
  content: attr(lquote);
}
ms[rquote]:after {
  content: attr(rquote);
}

3.3 General Layout Schemata

Besides tokens there are several families of MathML presentation elements. One family of elements deals with various "scripting" notations, such as subscript and superscript. Another family is concerned with matrices and tables. The remainder of the elements, discussed in this section, describe other basic notations such as fractions and radicals, or deal with general functions such as setting style properties and error handling.

3.3.1 Group Sub-Expressions <mrow>

The mrow element is used to group together any number of sub-expressions, usually consisting of one or more <mo> elements acting as "operators" on one or more other expressions that are their "operands".

In the following example, mrow is used to group a sum "1 + 2/3" as a fraction numerator (first child of mfrac) and to construct a fenced expression (first child of msup) that is raised to the power of 5. Note that mrow alone does not add visual fences around its grouped content, one has to explicitly specify them using the mo element.

Within the mrow elements, one can see that vertical alignment of children (according to the alphabetic baseline or the mathematical baseline) is properly performed, fences are vertically stretched and spacing around the binary + operator automatically calculated.

<math>
  <msup>
    <mrow>
      <mo>(</mo>
      <mfrac>
        <mrow>
          <mn>1</mn>
          <mo>+</mo>
          <mfrac>
            <mn>2</mn>
            <mn>3</mn>
          </mfrac>
        </mrow>
        <mn>4</mn>
      </mfrac>
      <mo>)</mo>
    </mrow>
    <mn>5</mn>
  </msup>
</math>
mrow example

The <mrow> element accepts the attributes described in 2.1.3 Global Attributes. An <mrow> element with in-flow children child1, child2, …, childN is laid out as shown on Figure 10. The child boxes are put in a row one after the other with all their alphabetic baselines aligned.

Figure 10 Box model for the <mrow> element
Note
Because the box model ensures alignment of alphabetic baselines, fraction bars or symmetric stretchy operators will also be aligned along the math axis in the typical case when AxisHeight is the same for all in-flow children.
3.3.1.1 Algorithm for stretching operators along the block axis
Figure 11 Symmetric and non-symmetric stretching of operators along the block axis

The algorithm for stretching operators along the block axis consists in the following steps:

  1. If there is a block stretch size constraint or an inline stretch size constraint then the element being laid out is an embellished operator. Lay out the one in-flow child that is an embellished operator with the same stretch size constraint and all the other in-flow children without any stretch size constraint and stop.
  2. Otherwise, split the list of in-flow children into a first list LToStretch containing embellished operators with a stretchy property and block stretch axis; and a second list LNotToStretch.
  3. Perform layout without any stretch size constraint on all the items of LNotToStretch. If LToStretch is empty then stop. If LNotToStretch is empty, perform layout with block stretch size constraint (0, 0) for all the items of LToStretch.
  4. Calculate the unconstrained target sizes Uascent and Udescent as respectively the maximum ink ascent and maximum ink descent of the margin boxes of in-flow children that have been laid out in the previous step.
  5. Lay out or relayout all the elements of LToStretch with block stretch size constraint (Uascent, Udescent).
3.3.1.2 Layout of <mrow>

If the box is not an anonymous <mrow> box and the associated element does not have its computed display property equal to block math or inline math then it is laid out according to the CSS specification where the corresponding value is described. Otherwise, the layout below is performed.

A child box is slanted if it is not an embellished operator and has nonzero italic correction.

Note
Large operators may have nonzero italic correction but that one is used when attaching scripts. More generally, all embellished operators are treated as non-slanted since the spacing around them is calculated as specified by lspace and rspace.

The min-content inline size (respectively max-content inline size) are calculated using the following algorithm:

  1. Set add-space to true if the box corresponds to a math element or is not an embellished operator; and to false otherwise.
  2. Set inline-offset to 0.
  3. Set previous-italic-correction to 0.
  4. For each in-flow child:
    1. If the child is not slanted, then increment inline-offset by previous-italic-correction.
    2. If the child is an embellished operator and add-space is true then increment inline-offset by its lspace property.
    3. Increment inline-offset by the min-content inline size (respectively max-content inline size) of the child's margin box.
    4. If the child is slanted then set previous-italic-correction to its italic correction. Otherwise set it to 0.
    5. If the child is an embellished operator and add-space is true then increment inline-offset by its rspace property.
  5. Increment inline-offset by previous-italic-correction.
  6. Return inline-offset.

The in-flow children are laid out using the algorithm for stretching operators along the block axis.

The inline size of the math content is calculated like the min-content inline size and max-content inline size of the math content, using the inline size of the in-flow children's margin boxes instead.

The ink line-ascent (respectively line-ascent) of the math content is the maximum of the ink line-ascents (respectively line-ascents) of all the in-flow children's margin boxes. Similarly, the ink line-descent (respectively line-descent) of the math content is the maximum of the ink line-descents (respectively ink line-ascents) of all the in-flow children's margin boxes.

The in-flow children are positioned using the following algorithm:

  1. Set add-space to true if the box corresponds to a math element or is not an embellished operator; and to false otherwise.
  2. Set inline-offset to 0.
  3. Set previous-italic-correction to 0.
  4. For each in-flow child:
    1. If the child is not slanted, then increment inline-offset by previous-italic-correction.
    2. If the child is an embellished operator and add-space is true then increment inline-offset by its lspace property.
    3. Set the inline offset of the child to inline-offset and its block offset such that the alphabetic baseline of the child is aligned with the alphabetic baseline.
    4. Increment inline-offset by the inline size of the child's margin box.
    5. If the child is slanted then set previous-italic-correction to its italic correction. Otherwise set it to 0.
    6. If the child is an embellished operator and add-space is true then increment inline-offset by its rspace property.

The italic correction of the math content is set to the italic correction of the last in-flow child, which is the final value of previous-italic-correction.

3.3.2 Fractions <mfrac>

The mfrac element is used for fractions. It can also be used to mark up fraction-like objects such as binomial coefficients and Legendre symbols.

If the <mfrac> element does not have its computed display property equal to block math or inline math then it is laid out according to the CSS specification where the corresponding value is described. Otherwise, the layout below is performed.

The <mfrac> element accepts the attributes described in 2.1.3 Global Attributes as well as the following attribute:

The linethickness attribute indicates the fraction line thickness to use for the fraction bar. If present, it must have a value that is a valid <length-percentage>. If the attribute is absent or has an invalid value, FractionRuleThickness is used as the default value. A percentage is interpreted relative to that default value. A negative value is interpreted as 0.

The following example contains four fractions with different linethickness values. The bars are always aligned with the middle of plus and minus signs. The numerator and denominator are horizontally centered. The fractions that are not in displaystyle use smaller gaps and font-size.

<math>
  <mn>0</mn>
  <mo>+</mo>
  <mfrac displaystyle="true">
    <mn>1</mn>
    <mn>2</mn>
  </mfrac>
  <mo></mo>
  <mfrac>
    <mn>1</mn>
    <mn>2</mn>
  </mfrac>
  <mo>+</mo>
  <mfrac linethickness="200%">
    <mn>1</mn>
    <mn>234</mn>
  </mfrac>
  <mo></mo>
  <mrow>
    <mo>(</mo>
    <mfrac linethickness="0">
      <mn>123</mn>
      <mn>4</mn>
    </mfrac>
    <mo>)</mo>
  </mrow>
</math>
mfrac example

The <mfrac> element sets displaystyle to false, or if it was already false increments scriptlevel by 1, within its children. It sets math-shift to compact within its second child. To avoid visual confusion between the fraction bar and another adjacent items (e.g. minus sign or another fraction's bar), a default 1-pixel space is added around the element. The user agent stylesheet must contain the following rules:

mfrac {
  padding-inline: 1px;
}
mfrac > * {
  math-depth: auto-add;
  math-style: compact;
}
mfrac > :nth-child(2) {
  math-shift: compact;
}

If the <mfrac> element has less or more than two in-flow children, its layout algorithm is the same as the mrow element. Otherwise, the first in-flow child is called numerator, the second in-flow child is called denominator and the layout algorithm is explained below.

Note
In practice, an <mfrac> element has two children that are in-flow. Hence the CSS rules basically perform scriptlevel, displaystyle and math-shift changes for the numerator and denominator.
3.3.2.1 Fraction with nonzero line thickness

If the fraction line thickness is nonzero, the <mfrac> element is laid out as shown on Figure 12. The fraction bar must only be painted if the visibility of the <mfrac> element is visible. In that case, the fraction bar must be painted with the color of the <mfrac> element.

Figure 12 Box model for the <mfrac> element

The min-content inline size (respectively max-content inline size) of content is the maximum between the min-content inline size (respectively max-content inline size) of the numerator's margin box and the min-content inline size (respectively max-content inline size) of the denominator's margin box.

If there is an inline stretch size constraint or a block stretch size constraint then the numerator is also laid out with the same stretch size constraint, otherwise it is laid out without any stretch size constraint. The denominator is always laid out without any stretch size constraint.

The inline size of the math content is the maximum between the inline size of the numerator's margin box and the inline size of the denominator's margin box.

NumeratorShift is the maximum between:

DenominatorShift is the maximum between:

The line-ascent of the math content is the maximum between:

The line-descent of the math content is the maximum between:

The inline offset of the numerator (respectively denominator) is half the inline size of the math content − half the inline size of the numerator's margin box (respectively denominator's margin box).

The alphabetic baseline of the numerator (respectively denominator) is shifted away from the alphabetic baseline by a distance of NumeratorShift (respectively DenominatorShift) towards the line-over (respectively line-under).

The math content box is placed within the content box so that their block-start edges are aligned and the middles of these edges are at the same position.

The inline size of the fraction bar is the inline size of the content box and its inline-start edge is the aligned with the one the content box. The center of the fraction bar is shifted away from the alphabetic baseline of the math content box by a distance of AxisHeight towards the line-over. Its block size is the fraction line thickness.

3.3.2.2 Fraction with zero line thickness

If the fraction line thickness is zero, the <mfrac> element is instead laid out as shown on Figure 13.

Figure 13 Box model for the <mfrac> element without bar

The min-content inline size, max-content inline size and inline size of the math content are calculated the same as in 3.3.2.1 Fraction with nonzero line thickness.

If there is an inline stretch size constraint or a block stretch size constraint then the numerator is also laid out with the same stretch size constraint and otherwise it is laid out without any stretch size constraint. The denominator is always laid out without any stretch size constraint.

If the math-style is compact then TopShift and BottomShift are respectively set to StackTopShiftUp and StackBottomShiftDown. Otherwise math-style is normal and they are respectively set to StackTopDisplayStyleShiftUp and StackBottomDisplayStyleShiftDown.

The Gap is defined to be (BottomShift − the ink line-ascent of the denominator's margin box) + (TopShift − the ink line-descent of the numerator's margin box). If math-style is compact then GapMin is StackGapMin, otherwise math-style is normal and it is StackDisplayStyleGapMin. If Δ = GapMinGap is positive then TopShift and BottomShift are respectively increased by Δ/2 and Δ − Δ/2.

The line-ascent of the math content is the maximum between:

The line-descent of the math content is the maximum between:

The inline offsets of the numerator and denominator are calculated the same as in 3.3.2.1 Fraction with nonzero line thickness.

The alphabetic baseline of the numerator (respectively denominator) is shifted away from the alphabetic baseline by a distance of TopShift (respectively − BottomShift) towards the line-over (respectively line-under).

The math content box is placed within the content box so that their block-start edges are aligned and the middles of these edges are at the same position.

3.3.3 Radicals <msqrt>, <mroot>

The radical elements construct an expression with a root symbol √ with a line over the content. The msqrt element is used for square roots, while the mroot element is used to draw radicals with indices, e.g. a cube root.

The <msqrt> and <mroot> elements accept the attributes described in 2.1.3 Global Attributes.

The following example contains a square root written with msqrt and a cube root written with mroot. Note that msqrt has several children and the square root applies to all of them. mroot has exactly two children: it is a root of index the second child (the number 3), applied to the first child (the square root). Also note these elements only change the font-size within the mroot index, but it is scaled down more than within the numerator and denumerator of the fraction.

<math>
  <mroot>
    <msqrt>
      <mfrac>
        <mn>1</mn>
        <mn>2</mn>
      </mfrac>
      <mo>+</mo>
      <mn>4</mn>
    </msqrt>
    <mn>3</mn>
  </mroot>
  <mo>+</mo>
  <mn>0</mn>
</math>
msqrt-mroot example

The <msqrt> and <mroot> elements sets math-shift to compact. The <mroot> element increments scriptlevel by 2, and sets displaystyle to "false" in all but its first child. The user agent stylesheet must contain the following rule in order to implement that behavior:

mroot > :not(:first-child) {
  math-depth: add(2);
  math-style: compact;
}
mroot, msqrt {
  math-shift: compact;
}

If the <msqrt> or <mroot> element do not have their computed display property equal to block math or inline math then they are laid out according to the CSS specification where the corresponding value is described. Otherwise, the layout below is performed.

If the <mroot> has less or more than two in-flow children, its layout algorithm is the same as the mrow element. Otherwise, the first in-flow child is called mroot base and the second in-flow child is called mroot index and its layout algorithm is explained below.

Note
In practice, an <mroot> element has two children that are in-flow. Hence the CSS rules basically perform scriptlevel and displaystyle changes for the index.

The <msqrt> element generates an anonymous <mrow> box called the msqrt base.

3.3.3.1 Radical symbol

The radical symbol must only be painted if the visibility of the <msqrt> or <mroot> element is visible. In that case, the radical symbol must be painted with the color of that element.

Let dir be the computed direction of the <msqrt> or <mroot> element. The radical glyph is the glyph obtained as a result of running get a glyph corresponding to the U+221A SQUARE ROOT character given dir.

The radical gap is given by RadicalVerticalGap if the math-style is compact and RadicalDisplayStyleVerticalGap if the math-style is normal.

The radical target size for the stretchy radical glyph is the sum of RadicalRuleThickness, radical gap and the ink height of the base.

The box metrics of the radical glyph and painting of the surd are given by the algorithm to shape a stretchy glyph to the target size for the radical glyph in the block dimension.

3.3.3.2 Square root

The <msqrt> element is laid out as shown on Figure 14.

Figure 14 Box model for the <msqrt> element

The min-content inline size (respectively max-content inline size) of the math content is the sum of the preferred inline size of a glyph stretched along the block axis for the radical glyph and of the min-content inline size (respectively max-content inline size) of the msqrt base's margin box.

The inline size of the math content is the sum of the advance width of the box metrics of the radical glyph and of the inline size of the msqrt base's margin's box.

The line-ascent of the math content is the maximum between:

The line-descent of the math content is the maximum between:

The inline size of the overbar is the inline size of the msqrt base's margin's box. The inline offsets of the msqrt base and overbar are also the same and equal to the width of the box metrics of the radical glyph.

The alphabetic baseline of the msqrt base is aligned with the alphabetic baseline. The block size of the overbar is RadicalRuleThickness. Its vertical center is shifted away from the alphabetic baseline by a distance towards the line-over equal to the line-ascent of the math content, minus the RadicalExtraAscender, minus half the RadicalRuleThickness.

Finally, the painting of the surd is performed:

3.3.3.3 Root with index

The <mroot> element is laid out as shown on Figure 15. The mroot index is first ignored and the mroot base and radical glyph are laid out as shown on figure Figure 14 using the same algorithm as in 3.3.3.2 Square root in order to produce a margin box B (represented in green).

Figure 15 Box model for the <mroot> element

The min-content inline size (respectively max-content inline size) of the math content is the sum of max(0, RadicalKernBeforeDegree), the mroot index's min-content inline size (respectively max-content inline size) of the mroot index's margin box, max(−min-content inline size, RadicalKernAfterDegree) (respectively max(−max-content inline size of the mroot index's margin box, RadicalKernAfterDegree)) and of the min-content inline size (respectively max-content inline size) of B.

Using the same clamping, AdjustedRadicalKernBeforeDegree and AdjustedRadicalKernAfterDegree are respectively defined as max(0, RadicalKernBeforeDegree) and is max(−inline size of the index's margin box, RadicalKernAfterDegree).

The inline size of the math content is the sum of AdjustedRadicalKernBeforeDegree, the inline size of the index's margin box, AdjustedRadicalKernAfterDegree and of the inline size of B.

The line-ascent of the math content is the maximum between:

The line-descent of the math content is the maximum between:

The inline offset of the index is AdjustedRadicalKernBeforeDegree. The inline-offset of the mroot base is the same + the inline size of the index's margin box.

The alphabetic baseline of B is aligned with the alphabetic baseline. The alphabetic baseline of the index is shifted away from the line-under edge by a distance of RadicalDegreeBottomRaisePercent × the block size of B + the line-descent of the index's margin box.

Note
In general, the kerning before the root index is positive while the kerning after it is negative, which means that the root element will have some inline-start space and that the root index will overlap the surd.

3.3.4 Style Change <mstyle>

Historically, the mstyle element was introduced to make style changes that affect the rendering of its contents.

The <mstyle> element accepts the attributes described in 2.1.3 Global Attributes. Its layout algorithm is the same as the mrow element.

Note
<mstyle> is implemented for compatibility with full MathML. Authors whose only target is MathML Core are encouraged to use CSS for styling.

In the following example, mstyle is used to set the scriptlevel and displaystyle. Observe this is respectively affecting the font-size and placement of subscripts of their descendants. In MathML Core, one could just have used mrow elements instead.

<math>
  <munder>
    <mo movablelimits="true">*</mo>
    <mi>A</mi>
  </munder>
  <mstyle scriptlevel="1">
    <mstyle displaystyle="true">
      <munder>
        <mo movablelimits="true">*</mo>
        <mi>B</mi>
      </munder>
      <munder>
        <mo movablelimits="true">*</mo>
        <mi>C</mi>
      </munder>
    </mstyle>
    <munder>
      <mo movablelimits="true">*</mo>
      <mi>D</mi>
    </munder>
  </mstyle>
</math>
mstyle example

3.3.5 Error Message <merror>

The merror element displays its contents as an ”error message”. The intent of this element is to provide a standard way for programs that generate MathML from other input to report syntax errors in their input.

In the following example, merror is used to indicate a parsing error for some LaTeX-like input:

<math>
  <mfrac>
    <merror>
      <mtext>Syntax error: \frac{1}</mtext>
    </merror>
    <mn>3</mn>
  </mfrac>
</math>
merror example

The <merror> element accepts the attributes described in 2.1.3 Global Attributes. Its layout algorithm is the same as the mrow element. The user agent stylesheet must contain the following rule in order to visually highlight the error message:

merror {
  border: 1px solid red;
  background-color: lightYellow;
}

3.3.6 Adjust Space Around Content <mpadded>

The mpadded element renders the same as its in-flow child content, but with the size and relative positioning point of its content modified according to <mpadded>’s attributes.

The <mpadded> element accepts the attributes described in 2.1.3 Global Attributes as well as the following attributes:

The width, height, depth, lspace and voffset if present, must have a value that is a valid <length-percentage>.

In the following example, mpadded is used to tweak spacing around a fraction (a blue background is used to visualize it). Without attributes, it behaves like an mrow but the attributes allow to specify the size of the box (width, height, depth) and position of the fraction within that box (lspace and voffset).

<math>
  <mrow>
    <mn>1</mn>
    <mpadded style="background: lightblue;">
      <mfrac>
        <mn>23456</mn>
        <mn>78</mn>
      </mfrac>
    </mpadded>
    <mn>9</mn>
  </mrow>
  <mo>+</mo>
  <mrow>
    <mn>1</mn>
    <mpadded lspace="2em" voffset="-1em" height="1em" depth="3em" width="7em"
             style="background: lightblue;">
      <mfrac>
        <mn>23456</mn>
        <mn>78</mn>
      </mfrac>
    </mpadded>
    <mn>9</mn>
  </mrow>
</math>
mpadded example
3.3.6.1 Inner box and requested parameters

The mpadded element generates an anonymous <mrow> box called the mpadded inner box with parameters called inner inline size, inner line-ascent and inner line-descent.

The requested <mpadded> parameters are determined as follows:

  • The requested width is the resolved value of the width property. If the width attribute is present, valid and not a percentage then that attribute is used as a presentational hint setting the element's width property to the corresponding value.
  • If the height attribute is absent, invalid or a percentage then the requested height is the inner line-ascent. Otherwise the requested height is the resolved value of the height attribute, clamping negative values to 0.
  • If the depth attribute is absent, invalid or a percentage then the requested depth is the inner line-ascent. Otherwise the requested depth is the resolved value of the depth attribute, clamping negative values to 0.
  • If the lspace attribute is absent, invalid or a percentage then the requested lspace is 0. Otherwise the requested lspace is the resolved value of the lspace attribute, clamping negative values to 0.
  • If the voffset attribute is absent, invalid or a percentage then the requested voffset is 0. Otherwise the requested voffset is the resolved value of the voffset attribute.
    Note
    Negative voffset values are not clamped to 0.
3.3.6.2 Layout of <mpadded>

If the <mpadded> element does not have its computed display property equal to block math or inline math then it is laid out according to the CSS specification where the corresponding value is described. Otherwise, it is laid out as shown on Figure 16.

Figure 16 Box model for the <mpadded> element

The min-content inline size (respectively max-content inline size) of the math content is the requested width calculated in 3.3.6.1 Inner box and requested parameters but using the min-content inline size (respectively max-content inline size) of the mpadded inner box instead of the "inner inline size".

The inline size of the math content is the requested width calculated in 3.3.6.1 Inner box and requested parameters.

The line-ascent of the math content is the requested height. The line-descent of the math content is the requested depth.

The mpadded inner box is placed so that its alphabetic baseline is shifted away from the alphabetic baseline by the requested voffset towards the line-over.

3.3.7 Making Sub-Expressions Invisible <mphantom>

Historically, the mphantom element was introduced to render its content invisibly, but with the same metrics size and other dimensions, including alphabetic baseline position that its contents would have if they were rendered normally.

In the following example, mphantom is used to ensure alignment of corresponding parts of the numerator and denominator of a fraction:

<math>
  <mfrac>
    <mrow>
      <mi>x</mi>
      <mo>+</mo>
      <mi>y</mi>
      <mo>+</mo>
      <mi>z</mi>
    </mrow>
    <mrow>
      <mi>x</mi>
      <mphantom>
        <mo form="infix">+</mo>
        <mi>y</mi>
      </mphantom>
      <mo>+</mo>
      <mi>z</mi>
    </mrow>
  </mfrac>
</math>
mphantom example

The <mphantom> element accepts the attributes described in 2.1.3 Global Attributes. Its layout algorithm is the same as the mrow element. The user agent stylesheet must contain the following rule in order to hide the content:

mphantom {
  visibility: hidden;
}
Note
<mphantom> is implemented for compatibility with full MathML. Authors whose only target is MathML Core are encouraged to use CSS for styling.

3.4 Script and Limit Schemata

The elements described in this section position one or more scripts around a base. Attaching various kinds of scripts and embellishments to symbols is a very common notational device in mathematics. For purely visual layout, a single general-purpose element could suffice for positioning scripts and embellishments in any of the traditional script locations around a given base. However, in order to capture the abstract structure of common notation better, MathML provides several more specialized scripting elements.

In addition to sub-/superscript elements, MathML has overscript and underscript elements that place scripts above and below the base. These elements can be used to place limits on large operators, or for placing accents and lines above or below the base.

3.4.1 Subscripts and