PyLaTeX

1.4.2 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

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.")

view raw JSON →