A lightweight LaTeX-to-Python parser
Project description
LaTeX-to-Python Parser (latex2py)
This is a lightweight LaTeX-to-Python parser.
We needed a parser that could convert LaTeX math expressions into Python-like expressions. However, SymPy is an extremely large library (~50 MB) and leads to bundle size issues when deploying code as an AWS lambda function (max. size 250MB). This codebase strips out the minimal code that we need, and is around ~200kb in size.
The parser is inspired by the sympy LaTeX parser, but instead of returning symbolic SymPy expressions, we return lines of Python-like code which could can then be evaluated in the interpreter.
Setup
Run poetry install to create a virtual environment and install dependencies.
You can also run poetry shell to activate the virtual environment.
Usage
from latex2py.parser import parse_latex
latex = r'\frac{1}{2} + \frac{3}{4}'
python = parse_latex(latex)
print(python) # "(1 / 2) + (3 / 4)"
# You can also evaluate the expression
result = eval(python)
print(result) # 1.25
Important Notes
- Integrals and derivatives are not supported yet.
- The
\\sumand\\prodcommands are mapped to specialSum()andProduct()functions that the calling code will have to implement. See the tests for examples. - Variables like
HelloWorld,Hello_World, or evenHello.worldare interpreted as Python-like variables and object properties. Usually, LaTeX would treat these as implicit multiplication. Multiplication must be made explicit using\\cdotor*.
You can also adjust the parsing behavior using the Config object - see latex2py/config.py for more details.
Tests
Run pytest tests to run the test suite. You can find examples of parseable LaTeX syntex there too.
Re-Generating the Grammar (Advanced)
If you make changes to LaTeX.g4, you will need to regenerate the parser using the following commands:
pip install antlr4-tools
# This will download the antlr4 jar file if you don't have it already.
cd src/latex2py/parser
antlr4 LaTeX.g4 -o gen
The generated files will be in the gen directory.
Publishing to PyPI (Maintainers)
- Update the version number in
pyproject.toml. - Run
poetry buildto build the package. - Run
poetry publishto publish the package to PyPI.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file latex2py-0.2.3.tar.gz.
File metadata
- Download URL: latex2py-0.2.3.tar.gz
- Upload date:
- Size: 50.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56f7d647bfb46bde5fafa57088b409f6c36ef12048a76a616b8bb465de7212fa
|
|
| MD5 |
95f2287780d43bed5752719db0ae3b52
|
|
| BLAKE2b-256 |
ce0aa3e242c2e637786f6c392c839636385151aa11c502332b7ca1b2e6dc732f
|
File details
Details for the file latex2py-0.2.3-py3-none-any.whl.
File metadata
- Download URL: latex2py-0.2.3-py3-none-any.whl
- Upload date:
- Size: 54.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac487bc012880093d2cb10178cf0eaf0d84a2f9b02db7201581ccb9ccfa1210a
|
|
| MD5 |
11cea8da421366231dc87c73a452f587
|
|
| BLAKE2b-256 |
7d40dd6e10090cfc2e263ba775c676abfbecb77617e99f9578529a9e05c8c991
|