Documentation version 0.1.0a4
Conversions
On this page

MathML to text

mathconv writes a MathML formula as one line of plain Unicode text, with the notation people type in an email or a chat: ^ and _ for scripts, / for fractions, sqrt(…) for roots. Reading the text gives the same formula as the input, without any markup to render.

Convert MathML to text

Output · text
x = (−b ± sqrt(b^2 − 4ac))/(2a)
● fully convertedOpen in the playground

What the output looks like#

The text target is for places where formulas must be read as text: search indexes, the training text of language models, plain-text exports, and systems with no math renderer. Characters are kept as Unicode, so α, ∑ and ≤ stay themselves, and structure is written with as few parentheses as keep it unambiguous.

  • Scripts use ^ and _, with parentheses around compound scripts: x^(n + 1).
  • Fractions use /, with parentheses around compound numerators and denominators.
  • Roots are sqrt(x) and root(n, x); binomial coefficients are binom(n, k).
  • Matrices and tables are written in rows, and limits of sums and integrals as scripts.
  • Content MathML is written in ordinary notation, as (a + b)^2 for a power of a sum.

The text output section of the documentation states every rule with examples. The text writer has no options and writes no delimiters.

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>

text

x = (−b ± 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>

text

∑_(k=1)^n k^2 = (n(n + 1)(2n + 1))/6
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>

text

x_i' + y_(i+1) = vec(v) ⋅ hat(n)
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>

text

A = (a, b; c, d)
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>

text

|x| = {x, if x ≥ 0; −x, otherwise
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>

text

(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. Pass target="text" to mathconv.convert, or make a Converter for the text target and convert lists with convert_batch, 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="text")
# 'A = (a, b; c, d)'

converter = mathconv.Converter(target="text")
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 = (a, b; c, d)', "x_i' + y_(i+1) = vec(v) ⋅ hat(n)"]

The option 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: "text" });
// 'A = (a, b; c, d)'

const converter = new Converter({ target: "text" });
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 = (a, b; c, d)', "x_i' + y_(i+1) = vec(v) ⋅ hat(n)"]

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

Coverage and speed#

  • Fully converted99.8%of 20,000 formulas
  • Speed, convert_batch908,030typical formulas a second
  • Speed, Rust core977,201typical 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.
  • Plain text cannot show every layout: boxes, strikes and enclosures are written with names, and phantoms, which only take up space, are left out.
  • Elementary math layout (mstack, mlongdiv) and mglyph images keep their text and the result reports a fallback.
  • Until version 1.0, the output may change between releases; pin a version for reproducible output.