Skip to main content

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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unicodemath-0.1.1.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unicodemath-0.1.1-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file unicodemath-0.1.1.tar.gz.

File metadata

  • Download URL: unicodemath-0.1.1.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for unicodemath-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3a60f760f2fa0cef485697a7ce80ab9380c89f250f03829e60a73c95489315b3
MD5 666e3e2680f46a38cd1649b480420223
BLAKE2b-256 d36c90e3d4de534f2af95ec419af4fa2d5477c2916669000fb45fb8e5471c3f2

See more details on using hashes here.

File details

Details for the file unicodemath-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: unicodemath-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for unicodemath-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1ed947c7f313e5256d117d4ed579002304e2a72f2160c9044364fea05717215e
MD5 b632e59eee57f77fe79128f0bab78e08
BLAKE2b-256 ba6aca1dc02092b343470b0b6986dcc86e7b51862155818734b49e632db82c53

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page