Metadata-Version: 2.4
Name: mathconv
Version: 0.1.0a2
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Rust
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: Text Processing :: Markup :: XML
Classifier: Typing :: Typed
License-File: LICENSE
License-File: NOTICE
Summary: Converts MathML formulas to LaTeX or plain text in one consistent style
Keywords: mathml,latex,tex,math,formula,equation,conversion,katex,mathjax
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Changelog, https://github.com/viktor-shcherb/mathconv/blob/main/CHANGELOG.md
Project-URL: Documentation, https://github.com/viktor-shcherb/mathconv/blob/main/docs/02-usage.md
Project-URL: Homepage, https://github.com/viktor-shcherb/mathconv
Project-URL: Issues, https://github.com/viktor-shcherb/mathconv/issues
Project-URL: Repository, https://github.com/viktor-shcherb/mathconv

# mathconv

mathconv converts math formulas from MathML, the markup that web pages,
e-books and journal articles use for mathematics, into LaTeX, the
notation most people use to type mathematics, or into plain text. It is
a Python package with its core written in Rust.

```xml
<math>
  <msqrt>
    <msup><mi>x</mi><mn>2</mn></msup>
    <mo>+</mo>
    <mn>1</mn>
  </msqrt>
</math>
```

becomes `\sqrt{x^2 + 1}` in LaTeX and `sqrt(x^2 + 1)` as text.

mathconv is made for converting formulas in bulk: preparing text for
training language models, search indexing, accessibility tools,
migrating documents, and any other work where formulas taken from web
pages, articles or e-books need to become LaTeX.

- **One style:** a formula gives the same LaTeX whichever tool wrote its MathML.
- **Renders:** KaTeX and MathJax, the common web renderers, accept every output.
- **Fast:** <!-- speed:start -->about 740,000 typical formulas a second through `convert_batch`, 830,000 in the Rust core, on one core of a GitHub-hosted runner ([measured](https://github.com/viktor-shcherb/mathconv/blob/main/docs/07-testing.md#throughput) on 2026-12-03)<!-- speed:end -->.
- **Nothing silently lost:** broken markup is repaired, and anything left unconverted is kept as text and reported.

## Installation

```sh
pip install --pre mathconv
```

mathconv needs Python 3.10 or later and has prebuilt wheels for Linux,
macOS and Windows; `--pre` is needed while only pre-releases are
published. mathconv is in early development: until 1.0, output and API
can change between releases, so pin a version if you need reproducible
output ([changelog](https://github.com/viktor-shcherb/mathconv/blob/main/CHANGELOG.md)).

## Usage

```python
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)'
```

Each call takes one formula, in presentation markup (how it looks),
content markup (what it means) or a mix, as found in web pages and XML
documents; finding the formulas in a page is up to the caller. A
`Converter` keeps options, converts lists and returns details:

```python
converter = mathconv.Converter(delimiters="dollar")  # wrap each formula in $ signs
converter.convert_batch(["<math><mi>a</mi></math>", '<math display="block"><mi>b</mi></math>'])
# ['$a$', '$$b$$']
result = converter.result("<math><mi>E</mi><mo>=</mo><mi>m</mi><msup><mi>c</mi><mn>2</mn></msup></math>")
result.output, result.fallbacks  # ('$E = mc^2$', []): fallbacks lists anything left unconverted
```

## Quality

The [mathconv corpus](https://huggingface.co/datasets/viktor-shcherb/mathconv-corpus)
holds about 235,000 public MathML formulas from openly licensed
articles, Wikipedia, textbooks and common converters
([how this is measured](https://github.com/viktor-shcherb/mathconv/blob/main/docs/07-testing.md#corpus-evaluation)).

<!-- coverage:start -->

| From | To | Formulas | Fully converted | Accepted by KaTeX | Accepted by MathJax | Looks like author's LaTeX | Looks like normalised LaTeX |
| --- | --- | ---: | ---: | ---: | ---: | ---: | ---: |
| mathml | latex | 20,000 | 99.7% | 100.0% | 100.0% | 81.3% | 68.6% |
| mathml | text | 20,000 | 99.8% | - | - | - | - |

Measured on 20,000 formulas held out for testing from the [mathconv corpus](https://huggingface.co/datasets/viktor-shcherb/mathconv-corpus/tree/f84cf205ba12d02f5dc8558dcdf70896e026f66a) at revision `f84cf205ba12`. *Fully converted*: nothing was left unconverted. *Accepted*: the renderer displays the LaTeX without error. *Looks like*: where the source also gives LaTeX, both look the same in MathJax.

<!-- coverage:end -->

## Documentation

- [Usage](https://github.com/viktor-shcherb/mathconv/blob/main/docs/02-usage.md): the API and its options.
- [LaTeX output style](https://github.com/viktor-shcherb/mathconv/blob/main/docs/04-latex-style.md): what the output looks like and why.
- [Testing](https://github.com/viktor-shcherb/mathconv/blob/main/docs/07-testing.md): how quality is measured.
- [Contributing](https://github.com/viktor-shcherb/mathconv/blob/main/CONTRIBUTING.md): reporting problems and changing the code.

The other documents are in [docs](https://github.com/viktor-shcherb/mathconv/tree/main/docs).

## Contributing

If a formula converts wrongly, open an issue with the
[wrong-conversion form](https://github.com/viktor-shcherb/mathconv/issues/new/choose),
giving the MathML and the output you got.

## Licence

mathconv is released under the
[MIT licence](https://github.com/viktor-shcherb/mathconv/blob/main/LICENSE).
[NOTICE](https://github.com/viktor-shcherb/mathconv/blob/main/NOTICE)
covers the W3C data its symbol tables are generated from.

