Registry / serialization / pylatexenc

pylatexenc

JSON →
library2.11pypypi✓ verified 26d ago

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.

pip install pylatexenc
INSTALL
IMPORT
SIG · PYLATEXENC
P
pylatexenc
serializationpythonv2.11
Install
1.6s avg
Import
53ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.11 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.056s · 18.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.050s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

LatexNodes2Text
from pylatexenc.latex2text import LatexNodes2Text
unicode_to_latex
from pylatexenc.latexencode import unicode_to_latex
from pylatexenc.latexencode import utf8tolatex
`utf8tolatex` is deprecated since `pylatexenc` 2.0; use `unicode_to_latex` instead for new code.
LatexWalker
from pylatexenc.latexwalker import LatexWalker

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
Debug
Known issues
breakingpylatexenc 2.0 introduced significant API changes from 1.x. Key changes include the deprecation of `keep_inline_math` in `LatexNodes2Text` in favor of `math_mode`, `macro_dict` in `LatexWalker` replaced by `latex_context`, and `pylatexenc.latexencode.utf8tolatex()` superseded by `unicode_to_latex()`.
fix
Review the 'New features in pylatexenc 2' documentation for detailed migration paths. Update method signatures and class instantiations to use the new API. For instance, replace `keep_inline_math=True` with `math_mode='verbatim'`.
affects: 2.0+
gotchaThe `latexwalker` module, while powerful for parsing LaTeX structure, is not a full LaTeX engine. It focuses on syntactic parsing and node representation, not typesetting or full macro expansion, which can lead to unexpected results if treated as a complete renderer.
fix
Understand that `pylatexenc` is for text processing and structural analysis, not for rendering. For full LaTeX compilation, use an actual TeX distribution.
affects: All versions
deprecatedMany internal classes and functions, especially within `pylatexenc.latexwalker` and node types, have been moved or renamed in preparation for `pylatexenc 3.0alpha`. For example, `len=` arguments were replaced by `pos_end=`, and node classes moved to the new `pylatexenc.latexnodes` module.
fix
While these changes are primarily in `3.0alpha` and later, be aware that relying on direct module paths or specific argument names from `2.x` for these internal components may lead to breakage when upgrading to `3.x`.
affects: 2.x (in anticipation of 3.0)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pylatexenc'
The `pylatexenc` library is not installed in the Python environment being used.
fix
Install the library using pip: `pip install pylatexenc`
pylatexenc.latexwalker.LatexWalkerParseError: Parse error: ...
The `LatexWalker` encountered a syntax error or an unsupported LaTeX construct in the input string, and `tolerant_parsing` mode was either not enabled or could not recover from the error.
fix
Ensure the LaTeX input is well-formed according to `pylatexenc`'s capabilities. For robustness, initialize `LatexWalker` or `LatexNodes2Text` with `tolerant_parsing=True` (e.g., `LatexWalker(latex_string, tolerant_parsing=True)`) to allow the parser to attempt to ignore syntax errors.
ValueError: Could not find LaTeX encoding for character 'X' (U+XXXX)
The `unicode_to_latex` function or `UnicodeToLatexEncoder` was called with `fail_bad_chars=True` (or equivalent `unknown_char_policy='fail'`) and encountered a Unicode character for which it has no known LaTeX escape sequence.
fix
Either set `fail_bad_chars=False` (or `unknown_char_policy='keep'` or `'replace'`) to handle unknown characters gracefully, or define a custom conversion rule for the specific Unicode character using a `UnicodeToLatexConversionRule` with `UnicodeToLatexEncoder`.
! Package inputenc Error: Unicode character (U+XXXX) (inputenc) not set up for use with LaTeX.
The LaTeX output generated by `pylatexenc` contains Unicode characters that the LaTeX compiler, specifically the `inputenc` package, cannot process because it's not configured to handle UTF-8 or the specific character.
fix
Ensure your LaTeX document preamble includes `\usepackage[utf8]{inputenc}` (for older LaTeX engines) or that you are using a modern LaTeX engine (like XeLaTeX or LuaLaTeX) that natively supports UTF-8, and ensure `pylatexenc` is converting non-ASCII characters to appropriate LaTeX escapes, or configure `pylatexenc.latexencode` to replace or ignore problematic characters.
Upgrade
Version history
2.11latest on PyPI · released Jul 25, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
Resources
pylatexenc — pip install pylatexenc · libregistry