Skip to main content

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

Limitations

  • Integrals and derivatives are not supported yet.
  • The \\sum and \\prod commands are mapped to special Sum() and Product() functions that the calling code will have to implement. See the tests for examples.
  • Strings like hello are interpreted as implicit multiplication, so hello will be interpreted as h * e * l * l * o. This is a limitation of the parser, and you will have to handle this case in your code. In a future version, we may want to add a flag that determines whether this is implicit multiplication or a variable name.

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.
antlr4 LaTeX.g4 -o gen

The generated files will be in the gen directory.

Publishing to PyPI (Maintainers)

  1. Update the version number in pyproject.toml.
  2. Run poetry build to build the package.
  3. Run poetry publish to publish the package to PyPI.

Project details


Download files

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

Source Distribution

latex2py-0.1.4.tar.gz (49.8 kB view details)

Uploaded Source

Built Distribution

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

latex2py-0.1.4-py3-none-any.whl (52.2 kB view details)

Uploaded Python 3

File details

Details for the file latex2py-0.1.4.tar.gz.

File metadata

  • Download URL: latex2py-0.1.4.tar.gz
  • Upload date:
  • Size: 49.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/23.3.0

File hashes

Hashes for latex2py-0.1.4.tar.gz
Algorithm Hash digest
SHA256 d29f4052c816b8d1589c4890829ce6530b78eb77f8678336817a249c8d05fe00
MD5 ff2c12d539c6b037fb0545f0e38e7c61
BLAKE2b-256 aece954d0877edfe0adf721c92ee57350b716f7ccc96c249917e8cca664c9896

See more details on using hashes here.

File details

Details for the file latex2py-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: latex2py-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 52.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/23.3.0

File hashes

Hashes for latex2py-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d6a1fb2cf8586c528ffcf2cc355fc517ffc8a19090cc1173ad1d9a258d165f78
MD5 0fc6e75793f9b57f38e1ae4d3c82d047
BLAKE2b-256 17a9e652b7bc889adf38adc360c777290d9d75a2831ef3319aeb3ec2a8198c56

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