asciimath2mathml
Convert AsciiMath strings to MathML markup.
Overview
asciimath2mathml is a zero-dependency Python library that compiles AsciiMath notation into semantically accurate MathML. It's optimized for fast, stateless, in-memory execution and works in both standard Python environments and browser-based WebAssembly contexts via Pyodide.
Features
- Zero runtime dependencies - Only uses Python's standard library plus Lark parser
- Stateless functional API - No class instantiation required
- WebAssembly compatible - Runs in Pyodide without compiled extensions
- Graceful degradation - Falls back to escaped text on parse errors
- Formal EBNF grammar - Lark-based Earley parser for predictable handling
- Comprehensive coverage - Implements all core mathematical symbols, matrix styles, and delimiters. See
COVERAGE_CHECKLIST.mdandROADMAP.mdfor full specification plans.
Installation
pip install asciimath2mathml
Or for development:
pip install -e ".[dev]"
Usage
from asciimath2mathml import convert
# Inline formula
result = convert("x^2 + y^2 = z^2")
# <math xmlns="http://www.w3.org"><mrow>...</mrow></math>
# Display block (centered, larger)
result = convert("sum_(i=1)^n i^2", display_block=True)
# <math xmlns="http://www.w3.org"><mstyle displaystyle="true">...</mstyle></math>
API Reference
convert(asciimath_string: str, display_block: bool = False) -> str
Compiles an AsciiMath string into valid presentational MathML markup.
Parameters:
asciimath_string(str): Raw text formula notation input stringdisplay_block(bool): If True, wraps output in<mstyle displaystyle="true">for block-level display
Returns:
- str: Validated XML/XHTML string with MathML namespace declarations
Examples
Basic Operations
convert("x + 42")
# <math xmlns="http://www.w3.org"><mrow><mi>x</mi><mo>+</mo><mn>42</mn></mrow></math>
convert("x^2")
# <math xmlns="http://www.w3.org"><msup><mi>x</mi><mn>2</mn></msup></math>
convert("x_1")
# <math xmlns="http://www.w3.org"><msub><mi>x</mi><mn>1</mn></msub></math>
Complex Expressions
convert("(a + b) / (c - d)")
# <math xmlns="http://www.w3.org"><mfrac>...</mfrac></math>
convert("sqrt(x^2 + y^2)")
# <math xmlns="http://www.w3.org"><msqrt>...</msqrt></math>
convert("x_1^2 + y_1^2 = z_1^2")
# <math xmlns="http://www.w3.org"><mrow><msubsup>...</msubsup>...</mrow></math>
Element Mapping
| AsciiMath | MathML Element | Description |
|---|---|---|
| Numbers | <mn> |
Numeric literals |
| Identifiers | <mi> |
Variables, function names |
| Operators | <mo> |
Mathematical operators |
| Text in quotes | <mtext> |
Plain text |
x^y |
<msup> |
Superscript |
x_y |
<msub> |
Subscript |
x_a^b |
<msubsup> |
Combined sub/superscript |
a / b |
<mfrac> |
Fraction |
sqrt(x) |
<msqrt> |
Square root |
(...) |
<mrow> |
Grouped expression |
Development
python3 -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]" # editable install — required before running tests
Testing
# Unit tests
pytest tests/ --ignore=tests/performance -v
# Performance benchmarks
pytest tests/performance/ -v
Performance
The library targets sub-2.5ms conversion time per formula on standard hardware.
See tests/performance/ for benchmark profiles.
License
MIT
Project Structure
asciimath2mathml/
├── src/
│ └── asciimath2mathml/
│ ├── __init__.py # Public API
│ ├── core.py # Compiler execution loop
│ ├── grammar.lark # EBNF grammar definition
│ └── transformer.py # AST to MathML transformer
└── tests/
├── unit/ # Unit tests
└── performance/ # Performance benchmarks
Release files for asciimath2mathml 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| asciimath2mathml-0.2.0.tar.gz | 17.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| asciimath2mathml-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 30.3 kB
Release files / asciimath2mathml-0.2.0.tar.gz
| Download URL | asciimath2mathml-0.2.0.tar.gz |
|---|---|
| Size | 17.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
363c09e57a91c0cef79cf7d6a95ea63290ae066b33ea045641cb8d434a841cc3
|
|
BLAKE2b-256 checksum How to use checksums |
821bb56ff25912c7f4b4b46e22c53cbd3eec279c9a07ced6c4dad5d0315e259e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.5
|
Release files / asciimath2mathml-0.2.0-py3-none-any.whl
| Download URL | asciimath2mathml-0.2.0-py3-none-any.whl |
|---|---|
| Size | 12.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4ab49980dbbb652a28454388bf92fbc4ea33b80cb7230dcb07dc2cb461b9779d
|
|
BLAKE2b-256 checksum How to use checksums |
27e89e06a812cc055b849be986a871103bc74eb49dc7fa26061fcbd1d9e3e8be
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.5
|