pylatexenc

2.10 · active · verified Fri Apr 10

pylatexenc is a simple Python library for parsing LaTeX code and converting between LaTeX and Unicode text. It's currently at version 2.10 and receives regular updates, mostly focusing on bug fixes and minor enhancements to its parsing and conversion capabilities.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core functionality of pylatexenc: converting a LaTeX string to plain Unicode text and converting a Unicode string back into LaTeX escape sequences.

from pylatexenc.latex2text import LatexNodes2Text
from pylatexenc.latexencode import unicode_to_latex

# LaTeX to Unicode text conversion
latex_input = r"""
\textbf{Hi there!} Here is \emph{an equation}:
\begin{equation}
\zeta = x + i y
\end{equation}
where $i$ is the imaginary unit.
"""
converter = LatexNodes2Text()
unicode_output = converter.latex_to_text(latex_input)
print(f"LaTeX to Unicode:\n{unicode_output}\n")
# Expected: LaTeX to Unicode:
# Hi there! Here is an equation:
# ζ = x + i y
# where i is the imaginary unit.

# Unicode text to LaTeX conversion
text_input = "À votre santé! The length of samples #3 & #4 is 3μm"
latex_encoded_output = unicode_to_latex(text_input)
print(f"Unicode to LaTeX:\n{latex_encoded_output}")
# Expected: Unicode to LaTeX:
# \`A votre sant\'e! The length of samples \#3 \& \#4 is 3\ensuremath{\mu}m

view raw JSON →