ASCIIMath/MathML/LaTeX Expression Converter

0.3.0 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize different translator classes and use the `translate` method to convert mathematical expressions between ASCIIMath, MathML, and LaTeX formats. Various options like `displaystyle` and `dtd` can be passed to the `translate` method for fine-grained control over the output.

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)

view raw JSON →