In HTML5, MathML stands for Mathematical Markup Language. These are XML content widely used in scientific contexts.
MathML not only determines how a formula is displayed but also describes the actual calculation.
Every valid MathML instance must be wrapped in <math> tags.
In addition, you must not nest a second <math> element in another, but you can have an arbitrary number of other child elements in it.
The math element is the root of all MathML expressions.
It was not supported in earlier versions of HTML.
The styles of the mi, mo and mtext tags could be put into a CSS rather than coding the style attribute on each tag:
Browser Support
It was not supported by all the major browsers. It was only supported by Mozilla Firefox, Safari and Chrome.
MathML is used to describe the presentation and the meaning of mathematical formulas using XML code.
Basic MathML Elements
Use this element to group any number of subexpressions horizontally.
Use this element to specify an identifier, that is, the name of a variable, a constant, a function, etc.
If this name is just one character long then the identifier is automatically rendered using an italic font, otherwise, the name is rendered using a normal, upright, font.
Use this element to specify an operator (e.g. "+"), a fence (e.g. "{") or a separator (e.g. ",").
The appropriate amount of space is added on the left and on the right of a mo depending on the textual contents of this element.
Example: if in the expression you replace <mo>+</mo> by <mo>,</mo> then this will suppress the space at the left of the mo element.
Use this element to specify a numeric literal.
For example, PI should be specified as <mi>PI</mi> and not as <mn>PI</mn> while 3.14 should be specified as <mn>3.14</mn> and not as <mi>3.14</mi>
Note: It is really important to use mi, mo, and mn appropriately because otherwise, the MathML rendering engine will not be able to apply its built-in typographic rules.
<math xmlns= "http://www.w3.org/1998/Math/MathML" display="inline">
The namespace of all MathML elements is "http://www.w3.org/1998/Math/MathML". Specifying that namespace is mandatory but it will be omitted in this tutorial for brevity.
display="inline"
Note the display="inline" attribute which specifies that the math element is to be displayed inline (``like a wordin a paragraph''). The other value is display=" block" that specifies that the math element is to be displayed as a block (``like a paragraph''). This attribute influences the typographic rules applied by the MathML rendering engine.
If you just want to type in plain text then the mtext element is used.
<mtext>then</mtext>
For explicit space between elements, the <mspace> element is used.
Do not attempt to insert one or more whitespace characters in the corresponding <class="sgmltag-element"> mtext element ( e.g. <class="literal"><mtext>if </mtext>).
Doing so is useless, because leading and trailing whitespace characters are automatically removed from <class="sgmltag-element">mi, < class="sgmltag-element">mo, < class="sgmltag-element">mn, and < class="sgmltag-element">mtext by the MathML processor. Instead, you need to insert a <class="sgmltag-element">mspace element in your MathML expression.
-
Width
This is an optional attribute that specifies the overall width of a < class="sgmltag-element">mspace element.
-
Height
This is an optional attribute that specifies the overall height above the baseline.
-
Depth
This is an optional attribute that specifies the overall height below the baseline. The value of these attributes is a number followed by one of the following units:
< class="literal">em, class="literal">ex,class="literal">px, < class="literal">in, <class="literal">cm, < class="literal">mm,< class="literal">pt, <class="literal">pc.
Example
- <!DOCTYPE html>
-
- <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta charset="utf-8" />
- <title>MathML Examples</title>
- </head>
- <body>
- <math xmlns="http://www.w3.org/1998/Math/MathML">
- <mrow>
- <mtext>if</mtext>
- <mspace depth="0.5ex" height="0.5ex" width="1ex"/>
- <mrow>
- <mi>a</mi>
- <mo>=</mo>
- <mi>b</mi>
- </mrow>
- <mspace depth="0.5ex" height="0.5ex" width="1ex"/>
- <mtext>then</mtext>
- <mspace depth="0.5ex" height="0.5ex" width="1ex"/>
- <mrow>
- <mrow>
- <mi>x</mi>
- <mi>a</mi>
- </mrow>
- <mo>=</mo>
- <mrow>
- <mi>x</mi>
- <mi>b</mi>
- </mrow>
- </mrow>
- </mrow>
- </math>
- </body>
- </html>
Output
To specify the Fraction, the mfrac attribute is used.
To change the style of the fraction we must use the attribute bevelled=" true".
Examplex-1/100
- <mfrac>
- <mrow>
- <mi>x</mi>
- <mo>-</mo>
- <mn>1</mn>
- </mrow>
- <mn>100</mn>
- </mfrac>
It is used to specify "square root."
Example
- <msqrt>
- <mi>x</mi>
- <mo>+</mo>
- <mi>y</mi>
- </msqrt>
mroot
Use this element to specify a radical with an arbitrary index.
Example
- <mroot>
- <mi>x</mi>
- <mn>3</mn>
- </mroot>
Note: Unlike msqrt, mroot may only have two child elements. The first child element is the base of the root.
The second child element is its index. If you need more than one element below the radical sign, then use an explicit mrow element.
Example
- <!DOCTYPE html>
-
- <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta charset="utf-8" />
- <title>MathML in HTML5</title>
- </head>
- <body>
-
- <math mode="display" xmlns="http://www.w3.org/1998/Math/MathML">
- <mrow>
- <mi>x</mi>
- <mo>=</mo>
- <mfrac>
- <mrow>
- <mo form="prefix">−</mo>
- <mi>b</mi>
- <mo>±</mo>
- <msqrt>
- <msup>
- <mi>b</mi>
- <mn>2</mn>
- </msup>
- <mo>−</mo>
- <mn>4</mn>
- <mo></mo>
- <mi>a</mi>
- <mo></mo>
- <mi>c</mi>
- </msqrt>
- </mrow>
- <mrow>
- <mn>2</mn>
- <mo></mo>
- <mi>a</mi>
- </mrow>
- </mfrac>
- </mrow>
- </math>
-
- </body>
- </html>
Output
Subscripts and Superscripts
It is used to attach a "subscript" to a base.
- <msub>
- <mi>x</mi>
- <mi>i</mi>
- </msub>
It is used to attach a "superscript" to a base.
- <msup>
- <mi>x</mi>
- <mi>j</mi>
- </msup>
It is used to attach both a subscript and a superscript to the base.
- <msubsup>
- <mi>x</mi>
- <mi>i</mi>
- <mi>j</mi>
- </msubsup>
Example
- <!DOCTYPE html>
-
- <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta charset="utf-8" />
- <title>MathML in HTML5</title>
- </head>
- <body>
- <math>
- <mrow>
- <mrow>
- <msup>
- <mi>a</mi>
- <mn>2</mn>
- </msup>
- <mo>+</mo>
- <msup>
- <mi>b</mi>
- <mn>2</mn>
- </msup>
- </mrow>
- <mo>=</mo>
- <msup>
- <mi>c</mi>
- <mn>2</mn>
- </msup>
- </mrow>
- </math>
- </body>
- </html>
Output
Underscripts and Overscripts
Underscripts and over scripts are similar to subscripts and superscripts, except that script elements are centered above and/or below the base element.
munder
It is used to attach an under script to the base.
- <munder>
- <mi>x</mi>
- <mo>─</mo>
- </munder>
mover
It is used to attach an over script to the base.
- <mover>
- <mi>v</mi>
- <mo>→</mo>
- </mover>
munderover
It is used to attach both an under script and an over script to the base.
- <munderover>
- <mi>x</mi>
- <mi>a</mi>
- <mi>b</mi>
- </munderover>
Ubiquitous mo element
To encode limits and integrals we use the mo element.
Example
- <!DOCTYPE html>
- <html>
- <head>
- <title>MathML in HTML5</title>
- </head>
- <body>
- <math>
- <mrow>
- <munderover>
- <mo>∫</mo>
- <mn>-1</mn>
- <mn>+1</mn>
- </munderover>
- <mfrac>
- <mrow>
- <mi>d</mi>
- <mi>x</mi>
- </mrow>
- <mi>x</mi>
- </mfrac>
- </mrow>
- </math>
- </body>
- </html>
Output
mtable
Matrices are specified using the mtable element. An mtable table element contains mtr row element and an mtr element contains mtd cell elements.
Example
- <!DOCTYPE html>
-
- <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta charset="utf-8" />
- <title>MathML Examples</title>
- </head>
- <body>
- <math xmlns="http://www.w3.org/1998/Math/MathML">
- <mrow>
- <mi>A</mi>
- <mo>=</mo>
- <mfenced open="[" close="]">
- <mtable>
- <mtr>
- <mtd><mi>x</mi></mtd>
- <mtd><mi>y</mi></mtd>
- </mtr>
- <mtr>
- <mtd><mi>z</mi></mtd>
- <mtd><mi>w</mi></mtd>
- </mtr>
- </mtable>
- </mfenced>
- </mrow>
- </math>
- </body>
- </html>
Output