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.0.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.0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: unicodemath-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 07e2c1daf1187d86516e4aead4acf077048bfafd8fb0999320ba7d175a7dce2e
MD5 0198b76aba6f6eff64e8ced0eef35998
BLAKE2b-256 d7971d3e5aba11007399cbd9eb665d381bb84eb6f36ed9243afd82ac4043709b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: unicodemath-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 989dd97d2432716a66086ce0e626f7d2b888830e9a689b5fb43daabbcacd9063
MD5 b4cbadf1149f98719cbf29f16569697b
BLAKE2b-256 98e91be61d2dbaffd595f149824ce7800895c7e720e11196a6805139c58237d2

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