Documentation version dev
Documentation
On this page

Getting started

Install mathconv and convert MathML formulas to LaTeX or plain text in Python, one at a time or in batches.

You are reading the documentation of the development version on the dev branch, which is not released. The latest release is 0.1.0a4: this page in 0.1.0a4.

Installation#

mathconv is published on PyPI. While only pre-releases are available, pip needs --pre to install it:

pip install --pre mathconv

mathconv supports Python 3.10 and later. Wheels are available for Linux on x86_64 and aarch64 (with glibc or musl), macOS on x86_64 and arm64, and Windows on x86_64. On other platforms pip builds mathconv from the source distribution, which needs a Rust compiler.

For JavaScript and TypeScript, mathconv is published on npm as a WebAssembly build that converts the same way; JavaScript API reference covers it:

npm install @mathconv/mathconv

mathconv is in early development. Until version 1.0, the output and the API can change between releases, so pin the version when you need reproducible output, and read the changelog before you upgrade. The installed version is available from the package metadata:

from importlib.metadata import version

version("mathconv")

Converting a formula#

mathconv.convert takes one MathML formula as a string and returns LaTeX:

import mathconv

mathconv.convert("<math><mfrac><mi>a</mi><mn>2</mn></mfrac></math>")
# '\\frac{a}{2}'

The input is the markup of one math element, as found in a web page, an e-book or the XML of a journal article. Namespaced XML MathML and HTML5 MathML are both accepted, and markup around the formula is ignored, so a formula cut from a page with some of its surrounding text converts as well:

import mathconv

mathconv.convert('<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"><mml:msqrt><mml:mi>x</mml:mi></mml:msqrt></mml:math>')
# '\\sqrt{x}'
mathconv.convert("<p>Let <math><msub><mi>a</mi><mi>n</mi></msub></math> be a sequence.</p>")
# 'a_n'

Finding the formulas in a document is up to the caller; FAQ shows one way. For plain Unicode text instead of LaTeX, pass target="text":

import mathconv

mathconv.convert("<math><mfrac><mi>a</mi><mn>2</mn></mfrac></math>", target="text")
# 'a/2'

Converting many formulas#

A Converter holds a source format, a target format and their options, and converts any number of formulas with them. Its batch methods take a list, or any other iterable, of strings and return one output per input, in order:

import mathconv

converter = mathconv.Converter(delimiters="dollar")
converter.convert_batch([
    "<math><mi>x</mi><mo>=</mo><mn>1</mn></math>",
    '<math display="block"><munderover><mo>∑</mo><mrow><mi>k</mi><mo>=</mo><mn>1</mn></mrow><mi>n</mi></munderover><mi>k</mi></math>',
])
# ['$x = 1$', '$$\\sum_{k=1}^n k$$']

The delimiters option chose $…$ for the inline formula and $$…$$ for the display formula. Conversion options lists every option. Converters are immutable and can be shared between threads, and a batch call is the fastest way to convert many formulas, as Performance explains.

What a conversion returns#

convert and convert_batch return strings. result and result_batch return mathconv.Result objects, which also say whether the formula is a display formula, where the output was taken from, and which parts of the input could not be converted:

import mathconv

converter = mathconv.Converter()
result = converter.result('<math display="block"><mi>x</mi><mo>+</mo><mstack><mn>12</mn></mstack></math>')
result.output
# 'x + 12'
result.display
# True
result.taken_from
# 'markup'
[(fallback.code, fallback.detail) for fallback in result.fallbacks]
# [('elementary-math', 'mstack')]

A fallback records something that mathconv could not convert. The conversion still succeeds: here the mstack element, which lays out school arithmetic, is not converted, and its digits are kept as they stand. Broken markup does not raise an exception either; mathconv repairs what it can and records a fallback for the rest. An empty fallbacks list means that the whole formula was converted. Supported MathML lists the fallback codes.

Next steps#

Edit this page on GitHub