PyLaTeX
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.
Common errors
-
FileNotFoundError: [Errno 2] No such file or directory: 'pdflatex'
cause PyLaTeX's `generate_pdf` method could not find the `pdflatex` executable.fixThis 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}cause 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.fixEnsure 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'
cause You are likely trying to access `pylatex.Document` directly or using an outdated import statement like `from pylatex.document import Document`.fixFor 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`.
Warnings
- breaking Starting 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.
- breaking In v1.0.0, many core classes were moved directly into the top-level `pylatex` namespace, leading to import path changes.
- gotcha Generating PDF files requires a local LaTeX distribution (e.g., TeX Live, MiKTeX) to be installed on your system and available in your PATH.
- gotcha The `generate_pdf` method uses `pdflatex` by default. If you need to use `xelatex` or `lualatex`, you must specify the compiler.
Install
-
pip install pylatex -
pip install pylatex[matplotlib] -
pip install pylatex[highlighting]
Imports
- Document
from pylatex.document import Document
from pylatex import Document
- Section
from pylatex import Section
- Command
from pylatex import Command
- Package
from pylatex import Package
- NoEscape
from pylatex import NoEscape
Quickstart
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.")