latexcodec is a Python library providing a lexer and codec for converting text between LaTeX markup and Unicode. It is particularly suited for handling short segments of LaTeX code, such as paragraphs or entries in a BibTeX file, rather than compiling full LaTeX documents. The current stable version is 3.0.1, and it maintains an active but measured release cadence.
pip install latexcodecVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to decode LaTeX strings to Unicode and encode Unicode strings to LaTeX using the `latex` and `ulatex` codecs registered by importing `latexcodec`. It also shows how to specify additional encodings and handle unrepresentable characters during encoding.
Upgrade to `latexcodec>=3.0.1`.
Evaluate migrating to `pylatexenc` for new projects or existing ones that require more robust LaTeX handling. (https://github.com/phfaist/pylatexenc)
Ensure use cases align with the library's scope. For full document parsing or compilation, consider dedicated LaTeX parsers or compilers.
Be aware of this behavior and implement post-processing if fully flattened Unicode is required. Unrecognized commands may require custom translation tables.
Use `codecs.encode(unicode_str, 'ulatex+utf8')` or `codecs.encode(unicode_str, 'ulatex', 'keep')`.
Account for these canonicalizations when comparing decoded output to original source or when expecting specific formatting.
Install the package using pip: `pip install latexcodec`
Use the `ulatex+utf8` codec for more robust encoding, or specify the `'keep'` error handler to retain unencodable characters. Example: `codecs.encode(unicode_str, 'ulatex+utf8')` or `codecs.encode(unicode_str, 'ulatex', 'keep')`.
Upgrade `latexcodec` to version 3.0.1 or later: `pip install --upgrade latexcodec`
Be aware of this intended behavior. If fully flattened Unicode is required, implement post-processing to handle or remove these unexpanded LaTeX commands, or consider using a full LaTeX parser like `pylatexenc` for more comprehensive LaTeX handling.