unicodemath
Parse UnicodeMath and AsciiMath, the two main linear (plain-text) math notations, into a common tree, then emit LaTeX or anything else. UnicodeMath is the notation Microsoft Word uses natively for equations (Unicode Technical Note 28), so ∑_(i=1)^n i^2 or a^2+b^2=√(c^2) is valid input. AsciiMath is the pure-ASCII cousin: sum_(i=1)^n i^2, AA x in RR x^2>=0.
The parser is a small Pratt parser, a clean-room Python port of the design of Peter Jipsen's umath2latex.js. Its LaTeX output byte-matches that reference implementation across the sample set published with it.
Install
pip install unicodemath
Use
from unicodemath import parse, to_latex
to_latex(parse('∀x>0 x^a x^b = x^(a+b)'))
# '\\forall x > 0(x^ax^b = x^{a+b})'
to_latex(parse('int_0^oo e^(-x) dx', dialect='asciimath'))
# '\\int_0^\\infty e^{-x}dx'
Matrices work in both dialects: UnicodeMath ■(a&b@c&d) (and █(...) for an equation array, which becomes aligned), AsciiMath [[a,b],[c,d]].
to_latex(parse('[[a,b],[c,d]]', dialect='asciimath'))
# '\\begin{bmatrix}a&b\\\\c&d\\end{bmatrix}'
prefixform renders the parse tree in a compact op(arg,...) form, useful for checking how an expression was read:
from unicodemath import prefixform
prefixform(parse('|x+y| ≤ |x|+|y|'))
# '≤(|(+(x,y)),+(|(x),|(y)))'
Backends
parse returns a tree of Node objects, each with sym (the source symbol), kind (how it was constructed: infix, chain, prefixop, around, matrix, ...), and children in arg, arg2, arg3. A backend is a function from that tree to a string; to_latex is the one included, and dispatches on kind. New dialects register a tokenizer in unicodemath.core.TOKENIZERS.
Scope
The goal is the common mathematical core of both notations, not either spec in full. Known gaps: AsciiMath text() and accents (hat, vec, ...) are not supported, and greedy tokenizing means multi-letter names the symbol table doesn't know (like sinh) parse as juxtaposed letters. UnicodeMath control words (\alpha style) pass through as literal LaTeX names.
Development
Version lives in unicodemath/__init__.py as __version__; release uses fastship (ship-bump, ship-gh, ship-pypi). Tests: pytest -q. The Jipsen sample fixtures in tests/fixtures/ were generated one time from the reference JS and are the port contract: our output must byte-match them.
Release files for unicodemath 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| unicodemath-0.1.1.tar.gz | 16.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| unicodemath-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 31.3 kB
Release files / unicodemath-0.1.1.tar.gz
| Download URL | unicodemath-0.1.1.tar.gz |
|---|---|
| Size | 16.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3a60f760f2fa0cef485697a7ce80ab9380c89f250f03829e60a73c95489315b3
|
|
BLAKE2b-256 checksum How to use checksums |
d36c90e3d4de534f2af95ec419af4fa2d5477c2916669000fb45fb8e5471c3f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.2
|
Release files / unicodemath-0.1.1-py3-none-any.whl
| Download URL | unicodemath-0.1.1-py3-none-any.whl |
|---|---|
| Size | 15.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1ed947c7f313e5256d117d4ed579002304e2a72f2160c9044364fea05717215e
|
|
BLAKE2b-256 checksum How to use checksums |
ba6aca1dc02092b343470b0b6986dcc86e7b51862155818734b49e632db82c53
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.2
|