Documentation version 0.1.0a4
Conversions
On this page

MathML to LaTeX

mathconv turns a MathML formula into the LaTeX a careful author would type: \frac, \sqrt, \sum_{k=1}^n, \begin{pmatrix}, with one spelling for each construct whichever tool wrote the MathML. Every output is accepted by KaTeX and MathJax.

Convert MathML to LaTeX

Output · LaTeX
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
● fully convertedOpen in the playground

What the output looks like#

MathML describes a formula as a tree of elements: mfrac for a fraction, msup for a superscript, mtable for a matrix. mathconv reads that tree, repairs broken markup, and writes each construct with the command people use most for it. Two documents that encode the same formula differently, say one with mfenced and one with mo brackets, give the same LaTeX, so the output can be compared, deduplicated and searched.

Some rules that shape the output:

  • Spacing around operators follows the MathML operator dictionary, so a+b=c comes out as a + b = c.
  • Brackets get \left and \right only around tall content, such as a fraction.
  • Function names use their command, \sin, \lim, or \operatorname for the others.
  • A matrix becomes pmatrix, bmatrix or another environment chosen from its fences, a piecewise definition becomes cases, and equation numbers become \tag.
  • Content MathML, the markup that describes meaning rather than layout, is written in the notation of hand-written mathematics.

The LaTeX output style section of the documentation states every rule with examples.

Examples#

Each example below was converted by the release of mathconv that this site runs, when the page was built.

Quadratic formula ● fully converted

MathML

<math display="block">
  <mi>x</mi>
  <mo>=</mo>
  <mfrac>
    <mrow>
      <mo>&minus;</mo><mi>b</mi>
      <mo>&PlusMinus;</mo>
      <msqrt>
        <msup><mi>b</mi><mn>2</mn></msup>
        <mo>&minus;</mo>
        <mn>4</mn><mi>a</mi><mi>c</mi>
      </msqrt>
    </mrow>
    <mrow><mn>2</mn><mi>a</mi></mrow>
  </mfrac>
</math>

LaTeX

x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
Sum with limits ● fully converted

MathML

<math display="block">
  <munderover>
    <mo>&sum;</mo>
    <mrow><mi>k</mi><mo>=</mo><mn>1</mn></mrow>
    <mi>n</mi>
  </munderover>
  <msup><mi>k</mi><mn>2</mn></msup>
  <mo>=</mo>
  <mfrac>
    <mrow>
      <mi>n</mi>
      <mo>(</mo><mi>n</mi><mo>+</mo><mn>1</mn><mo>)</mo>
      <mo>(</mo><mn>2</mn><mi>n</mi><mo>+</mo><mn>1</mn><mo>)</mo>
    </mrow>
    <mn>6</mn>
  </mfrac>
</math>

LaTeX

\sum_{k=1}^n k^2 = \frac{n(n + 1)(2n + 1)}{6}
Matrix in parentheses ● fully converted

MathML

<math display="block">
  <mi>A</mi>
  <mo>=</mo>
  <mrow>
    <mo>(</mo>
    <mtable>
      <mtr><mtd><mi>a</mi></mtd><mtd><mi>b</mi></mtd></mtr>
      <mtr><mtd><mi>c</mi></mtd><mtd><mi>d</mi></mtd></mtr>
    </mtable>
    <mo>)</mo>
  </mrow>
</math>

LaTeX

A = \begin{pmatrix}a & b \\ c & d\end{pmatrix}
Cases ● fully converted

MathML

<math display="block">
  <mrow>
    <mo>|</mo><mi>x</mi><mo>|</mo>
  </mrow>
  <mo>=</mo>
  <mrow>
    <mo>{</mo>
    <mtable columnalign="left">
      <mtr><mtd><mi>x</mi></mtd><mtd><mtext>if&nbsp;</mtext><mi>x</mi><mo>&ge;</mo><mn>0</mn></mtd></mtr>
      <mtr><mtd><mo>&minus;</mo><mi>x</mi></mtd><mtd><mtext>otherwise</mtext></mtd></mtr>
    </mtable>
  </mrow>
</math>

LaTeX

|x| = \begin{cases}x & \text{if }x \geq 0 \\ -x & \text{otherwise}\end{cases}
Subscripts, primes and accents ● fully converted

MathML

<math>
  <msubsup><mi>x</mi><mi>i</mi><mo>&prime;</mo></msubsup>
  <mo>+</mo>
  <msub><mi>y</mi><mrow><mi>i</mi><mo>+</mo><mn>1</mn></mrow></msub>
  <mo>=</mo>
  <mover><mi>v</mi><mo>&rarr;</mo></mover>
  <mo>&sdot;</mo>
  <mover><mi>n</mi><mo>^</mo></mover>
</math>

LaTeX

x_i' + y_{i+1} = \vec{v} \cdot \hat{n}
Content MathML ● fully converted

MathML

<math>
  <apply><eq/>
    <apply><power/>
      <apply><plus/><ci>a</ci><ci>b</ci></apply>
      <cn>2</cn>
    </apply>
    <apply><plus/>
      <apply><power/><ci>a</ci><cn>2</cn></apply>
      <apply><times/><cn>2</cn><ci>a</ci><ci>b</ci></apply>
      <apply><power/><ci>b</ci><cn>2</cn></apply>
    </apply>
  </apply>
</math>

LaTeX

(a + b)^2 = a^2 + 2ab + b^2

In Python#

Install the package with pip install --pre mathconv; it has prebuilt wheels for Linux, macOS and Windows and needs Python 3.10 or later. mathconv.convert takes one formula as a string. For many formulas, a Converter keeps the options and convert_batch converts a list in one call, which releases the GIL while it works.

import mathconv

mathml = '<math display="block"><mi>A</mi><mo>=</mo><mrow><mo>(</mo><mtable><mtr><mtd><mi>a</mi></mtd><mtd><mi>b</mi></mtd></mtr><mtr><mtd><mi>c</mi></mtd><mtd><mi>d</mi></mtd></mtr></mtable><mo>)</mo></mrow></math>'
mathconv.convert(mathml, target="latex")
# 'A = \\begin{pmatrix}a & b \\\\ c & d\\end{pmatrix}'

converter = mathconv.Converter(target="latex")
formulas = [
    '<math display="block"><mi>A</mi><mo>=</mo><mrow><mo>(</mo><mtable><mtr><mtd><mi>a</mi></mtd><mtd><mi>b</mi></mtd></mtr><mtr><mtd><mi>c</mi></mtd><mtd><mi>d</mi></mtd></mtr></mtable><mo>)</mo></mrow></math>',
    '<math><msubsup><mi>x</mi><mi>i</mi><mo>&prime;</mo></msubsup><mo>+</mo><msub><mi>y</mi><mrow><mi>i</mi><mo>+</mo><mn>1</mn></mrow></msub><mo>=</mo><mover><mi>v</mi><mo>&rarr;</mo></mover><mo>&sdot;</mo><mover><mi>n</mi><mo>^</mo></mover></math>',
]
converter.convert_batch(formulas)
# ['A = \\begin{pmatrix}a & b \\\\ c & d\\end{pmatrix}', "x_i' + y_{i+1} = \\vec{v} \\cdot \\hat{n}"]

The delimiters option wraps each formula in $…$ or \(…\), in the display form for display formulas. prefer_carried_tex returns the TeX a formula already carries in an annotation or alttext instead of converting the markup, and drop_labels leaves out equation numbers. Converter.result also returns the display flag and the list of anything that could not be converted. The conversion options and the Python API reference cover the details.

In JavaScript#

The npm package @mathconv/mathconv is the same engine compiled to WebAssembly, with the options of the Python package. It works in Node.js 22.12 and later, Deno 2.9 and later, bundlers such as webpack, Vite and esbuild, and browsers that load ES modules. Deno needs permission to read the package's .wasm file, granted with --allow-read. Node.js and Deno load the module when the package is imported; elsewhere, call await init() once before the first conversion.

npm install @mathconv/mathconv
import { Converter, convert } from "@mathconv/mathconv";

const mathml = '<math display="block"><mi>A</mi><mo>=</mo><mrow><mo>(</mo><mtable><mtr><mtd><mi>a</mi></mtd><mtd><mi>b</mi></mtd></mtr><mtr><mtd><mi>c</mi></mtd><mtd><mi>d</mi></mtd></mtr></mtable><mo>)</mo></mrow></math>';
convert(mathml, { target: "latex" });
// 'A = \\begin{pmatrix}a & b \\\\ c & d\\end{pmatrix}'

const converter = new Converter({ target: "latex" });
converter.convertBatch([
  '<math display="block"><mi>A</mi><mo>=</mo><mrow><mo>(</mo><mtable><mtr><mtd><mi>a</mi></mtd><mtd><mi>b</mi></mtd></mtr><mtr><mtd><mi>c</mi></mtd><mtd><mi>d</mi></mtd></mtr></mtable><mo>)</mo></mrow></math>',
  '<math><msubsup><mi>x</mi><mi>i</mi><mo>&prime;</mo></msubsup><mo>+</mo><msub><mi>y</mi><mrow><mi>i</mi><mo>+</mo><mn>1</mn></mrow></msub><mo>=</mo><mover><mi>v</mi><mo>&rarr;</mo></mover><mo>&sdot;</mo><mover><mi>n</mi><mo>^</mo></mover></math>',
]);
// ['A = \\begin{pmatrix}a & b \\\\ c & d\\end{pmatrix}', "x_i' + y_{i+1} = \\vec{v} \\cdot \\hat{n}"]

The JavaScript API reference covers loading the module and every call.

Coverage and speed#

  • Fully converted99.7%of 20,000 formulas
  • Accepted by KaTeX100.0%of 20,000 formulas
  • Accepted by MathJax100.0%of 20,000 formulas
  • Looks like author's LaTeX81.3%of 20,000 formulas
  • Looks like normalised LaTeX68.6%of 20,000 formulas
  • Speed, convert_batch806,479typical formulas a second
  • Speed, Rust core847,127typical formulas a second

Coverage is measured on 20,000 formulas held out for testing from the mathconv corpus, and speed on one core of a GitHub-hosted ubuntu-latest runner (AMD EPYC 9V74 80-Core Processor), for mathconv 0.1.0a4. Benchmarks has every figure, and the documentation describes the corpus evaluation and the speed measurement.

Known limits#

  • Each call converts the first math element of its input; finding the formulas in an HTML or XML document is up to the caller.
  • Elementary math layout (mstack, mlongdiv) and mglyph images have no LaTeX equivalent here; their text is kept and the result reports a fallback.
  • A character with no LaTeX command is written inside \text{}. KaTeX's strict mode may reject some of these characters, though its default mode and MathJax accept them.
  • Carried TeX returned with prefer_carried_tex is the author's own and is passed on unchecked.
  • Until version 1.0, the output may change between releases; pin a version for reproducible output.