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 pylatexVerified import paths — ran on the pinned version, not inferred.
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.
To insert unescaped LaTeX code, wrap the string with `NoEscape()` from `pylatex.utils` or `pylatex`. Example: `doc.append(NoEscape('\\textbf{raw text}'))`.Update your imports from `from pylatex.document import Document` to `from pylatex import Document` and similarly for other core components.
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.
Pass the `compiler` argument to `generate_pdf`: `doc.generate_pdf('filename', compiler='xelatex')`.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.
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`.
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`.