ASCIIMath/MathML/LaTeX Expression Converter
py-asciimath is a Python library designed for converting mathematical expressions between ASCIIMath, MathML, and LaTeX formats. It provides functionality to convert from ASCIIMath to LaTeX or MathML, from MathML to LaTeX, and from LaTeX to ASCIIMath. The current version is 0.3.0. The project maintains an active development status, with ongoing updates and feature expansions, evidenced by recent major version bumps and support for new translation directions.
Warnings
- breaking Python 2.7 compatibility was officially dropped in version `0.2.3`. Users requiring Python 2.7 support must use `py-asciimath` version `0.2.2` or older.
- gotcha The `MathML2Tex` conversion relies on an XSLT MathML Library. While generally robust, unexpected behavior may occur with certain complex or non-standard MathML structures. Users are encouraged to report any issues.
Install
-
pip install py-asciimath
Imports
- ASCIIMath2MathML
from py_asciimath.translator.translator import ASCIIMath2MathML
- ASCIIMath2Tex
from py_asciimath.translator.translator import ASCIIMath2Tex
- MathML2Tex
from py_asciimath.translator.translator import MathML2Tex
- Tex2ASCIIMath
from py_asciimath.translator.translator import Tex2ASCIIMath
Quickstart
from py_asciimath.translator.translator import (ASCIIMath2MathML, ASCIIMath2Tex, MathML2Tex, Tex2ASCIIMath)
# Example: ASCIIMath to MathML
asciimath_expr = r"e^x > 0 forall x in RR"
asciimath2mathml = ASCIIMath2MathML(log=False, inplace=True)
mathml_output = asciimath2mathml.translate(
asciimath_expr,
displaystyle=True,
dtd="mathml2",
dtd_validation=False,
from_file=False,
output="string"
)
print("ASCIIMath to MathML:\n", mathml_output)
# Example: ASCIIMath to LaTeX
asciimath2tex = ASCIIMath2Tex()
latex_output = asciimath2tex.translate(asciimath_expr)
print("ASCIIMath to LaTeX:\n", latex_output)
# Example: LaTeX to ASCIIMath
latex_expr = r"\frac{-b \pm \sqrt{b^2-4ac}}{2a}"
tex2asciimath = Tex2ASCIIMath()
asciimath_from_tex_output = tex2asciimath.translate(latex_expr)
print("LaTeX to ASCIIMath:\n", asciimath_from_tex_output)