MathML to LaTeX and text. One style, always renders.
mathconv converts math formulas from MathML, the markup of web pages, e-books and journal articles, into LaTeX that KaTeX and MathJax render, or into readable plain text. It is a Python package with a Rust core, made for converting millions of formulas.
pip install --pre mathconvnpm install @mathconv/mathconvTry it
Output · LaTeX
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}● fully convertedOpen in the playground
Key figures
- Coverage99.7%of 20,000 held-out formulas fully converted to LaTeX; 100.0% render in KaTeX and MathJax
- Speed810,000typical formulas a second through
convert_batch, on one core - Size213 kBthe whole converter as gzipped WebAssembly, running in the playground
- LicenceMITopen source, a Rust core with Python wheels for Linux, macOS and Windows
Conversions
- MathML to LaTeXConvert MathML to LaTeX that KaTeX and MathJax render, in one consistent style, from Python or in the browser. Worked examples, the Python calls for single formulas and batches, measured coverage and speed, and known limits.
- MathML to textConvert MathML formulas to readable plain Unicode text, such as sqrt(x^2 + 1), for search indexes, language models and screen-free contexts. Worked examples, the Python calls, measured coverage and speed, and known limits.
Usage
import mathconv
mathml = "<math><msqrt><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mn>1</mn></msqrt></math>"
mathconv.convert(mathml)
# '\\sqrt{x^2 + 1}'
mathconv.convert(mathml, target="text")
# 'sqrt(x^2 + 1)'
import { convert } from "@mathconv/mathconv";
const mathml = '<math><msqrt><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mn>1</mn></msqrt></math>';
convert(mathml);
// '\\sqrt{x^2 + 1}'
convert(mathml, { target: "text" });
// 'sqrt(x^2 + 1)'
Each call takes one formula, in presentation or content MathML. A Converter keeps options and converts lists; the documentation covers the API, the options and the output style.