Registry / serialization / pylatex

pylatex

JSON →
library1.4.2pypypi✓ verified 88d ago

PyLaTeX is a Python library designed to simplify the creation of LaTeX files and snippets. It allows users to programmatically build LaTeX documents, from simple text to complex structures with figures, tables, and custom commands. The current version is 1.4.2, and it follows an infrequent but active release cadence.

pip install pylatex
INSTALL
IMPORT
SIG · PYLATEX
P
pylatex
serializationpythonv1.4.2
Install
5.3s avg
Import
82ms
Disk
98MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.4.2 · 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.10–3.945 runs
installs and imports cleanly · install 0.0s · import 0.085s · 18.2MB
glibc
py 3.10–3.945 runs
installs and imports cleanly · install 5.3s · import 0.079s · 19MB
98MB installed
● package 98MB
Code
Verified usage

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

Document
✓ from pylatex import Document
✗ from pylatex.document import Document
Pre-v1.0 imports moved core classes directly into the `pylatex` namespace.
Section
✓ from pylatex import Section
Command
✓ from pylatex import Command
Package
✓ from pylatex import Package
NoEscape
✓ from pylatex import NoEscape
Crucial for inserting raw LaTeX code without automatic escaping after v1.0.

This quickstart demonstrates creating a basic LaTeX document with sections, custom commands, and inserting raw LaTeX using `NoEscape`. It shows how to generate both a `.tex` file and attempts to generate a `.pdf` file, highlighting the prerequisite of a local LaTeX distribution for PDF generation.

from pylatex import Document, Section, Command, NoEscape from pylatex.utils import italic, NoEscape geom_options = ['a4paper', 'total={15cm,20cm}'] doc = Document(geometry_options=geom_options) with doc.create(Section('Introduction')): doc.append('Some text.') doc.append(Command('textbf', 'Bold text')) with doc.create(Section('Raw LaTeX Example')): doc.append('Here is some raw LaTeX, use NoEscape:') doc.append(NoEscape('\\begin{itemize}\n\\item First item\n\\item Second item\\end{itemize}')) # To generate a .tex file doc.generate_tex('full_document') # To generate a .pdf file (requires a local LaTeX distribution like TeX Live or MiKTeX) try: doc.generate_pdf('full_document', clean_tex=False) print("PDF generated successfully if LaTeX distribution is installed.") except Exception as e: print(f"Could not generate PDF (is LaTeX installed?): {e}") print("TeX file 'full_document.tex' created.")
Debug
Known issues
breakingStarting with PyLaTeX v1.0.0, all text added to documents is escaped by default. This change helps prevent LaTeX injection vulnerabilities but requires explicit action for raw LaTeX.
fix
To insert unescaped LaTeX code, wrap the string with `NoEscape()` from `pylatex.utils` or `pylatex`. Example: `doc.append(NoEscape('\\textbf{raw text}'))`.
affects: >=1.0.0
breakingIn v1.0.0, many core classes were moved directly into the top-level `pylatex` namespace, leading to import path changes.
fix
Update your imports from `from pylatex.document import Document` to `from pylatex import Document` and similarly for other core components.
affects: >=1.0.0
gotchaGenerating PDF files requires a local LaTeX distribution (e.g., TeX Live, MiKTeX) to be installed on your system and available in your PATH.
fix
Install a LaTeX distribution relevant to your operating system. For example, `sudo apt-get install texlive-full` on Ubuntu or download TeX Live/MiKTeX for other platforms.
affects: all
gotchaThe `generate_pdf` method uses `pdflatex` by default. If you need to use `xelatex` or `lualatex`, you must specify the compiler.
fix
Pass the `compiler` argument to `generate_pdf`: `doc.generate_pdf('filename', compiler='xelatex')`.
affects: all
Errors
Common errors & fixes
FileNotFoundError: [Errno 2] No such file or directory: 'pdflatex'
PyLaTeX's `generate_pdf` method could not find the `pdflatex` executable.
fix
This indicates that a LaTeX distribution (like TeX Live or MiKTeX) is not installed on your system or its executables are not in your system's PATH.
LaTeX Error: Missing \begin{document}
This error often occurs when invalid or incomplete LaTeX is generated, sometimes due to incorrect use of `NoEscape` or attempting to compile a malformed `.tex` file.
fix
Ensure all raw LaTeX inserted using `NoEscape` is syntactically correct and self-contained. Also, double-check that your `Document` object is properly initialized and populated before calling `generate_tex` or `generate_pdf`.
AttributeError: module 'pylatex' has no attribute 'Document'
You are likely trying to access `pylatex.Document` directly or using an outdated import statement like `from pylatex.document import Document`.
fix
For PyLaTeX v1.0.0 and later, the `Document` class (and most other core components) is directly available under the `pylatex` namespace. Use `from pylatex import Document`.
Upgrade
Version history
1.4.2latest on PyPI · released Oct 19, 2023
Audit
Dependencies
matplotliboptionalRequired for integrating Matplotlib figures into LaTeX documents.
PygmentsoptionalRequired for syntax highlighting of code in LaTeX documents.
Agent activity
6 hits · last 30 days
node
6
Resources
pylatex — pip install pylatex · libregistry