{"id":7579,"library":"pylatex","title":"PyLaTeX","description":"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.","status":"active","version":"1.4.2","language":"en","source_language":"en","source_url":"https://github.com/JelteF/PyLaTeX","tags":["latex","document-generation","pdf","scientific-writing"],"install":[{"cmd":"pip install pylatex","lang":"bash","label":"Basic installation"},{"cmd":"pip install pylatex[matplotlib]","lang":"bash","label":"With Matplotlib support"},{"cmd":"pip install pylatex[highlighting]","lang":"bash","label":"With Pygments for syntax highlighting"}],"dependencies":[{"reason":"Required for integrating Matplotlib figures into LaTeX documents.","package":"matplotlib","optional":true},{"reason":"Required for syntax highlighting of code in LaTeX documents.","package":"Pygments","optional":true}],"imports":[{"note":"Pre-v1.0 imports moved core classes directly into the `pylatex` namespace.","wrong":"from pylatex.document import Document","symbol":"Document","correct":"from pylatex import Document"},{"symbol":"Section","correct":"from pylatex import Section"},{"symbol":"Command","correct":"from pylatex import Command"},{"symbol":"Package","correct":"from pylatex import Package"},{"note":"Crucial for inserting raw LaTeX code without automatic escaping after v1.0.","symbol":"NoEscape","correct":"from pylatex import NoEscape"}],"quickstart":{"code":"from pylatex import Document, Section, Command, NoEscape\nfrom pylatex.utils import italic, NoEscape\n\ngeom_options = ['a4paper', 'total={15cm,20cm}']\ndoc = Document(geometry_options=geom_options)\n\nwith doc.create(Section('Introduction')):\n    doc.append('Some text.')\n    doc.append(Command('textbf', 'Bold text'))\n\nwith doc.create(Section('Raw LaTeX Example')):\n    doc.append('Here is some raw LaTeX, use NoEscape:')\n    doc.append(NoEscape('\\\\begin{itemize}\\n\\\\item First item\\n\\\\item Second item\\\\end{itemize}'))\n\n# To generate a .tex file\ndoc.generate_tex('full_document')\n\n# To generate a .pdf file (requires a local LaTeX distribution like TeX Live or MiKTeX)\ntry:\n    doc.generate_pdf('full_document', clean_tex=False)\n    print(\"PDF generated successfully if LaTeX distribution is installed.\")\nexcept Exception as e:\n    print(f\"Could not generate PDF (is LaTeX installed?): {e}\")\n\nprint(\"TeX file 'full_document.tex' created.\")","lang":"python","description":"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."},"warnings":[{"fix":"To insert unescaped LaTeX code, wrap the string with `NoEscape()` from `pylatex.utils` or `pylatex`. Example: `doc.append(NoEscape('\\\\textbf{raw text}'))`.","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Update your imports from `from pylatex.document import Document` to `from pylatex import Document` and similarly for other core components.","message":"In v1.0.0, many core classes were moved directly into the top-level `pylatex` namespace, leading to import path changes.","severity":"breaking","affected_versions":">=1.0.0"},{"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.","message":"Generating PDF files requires a local LaTeX distribution (e.g., TeX Live, MiKTeX) to be installed on your system and available in your PATH.","severity":"gotcha","affected_versions":"all"},{"fix":"Pass the `compiler` argument to `generate_pdf`: `doc.generate_pdf('filename', compiler='xelatex')`.","message":"The `generate_pdf` method uses `pdflatex` by default. If you need to use `xelatex` or `lualatex`, you must specify the compiler.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"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.","cause":"PyLaTeX's `generate_pdf` method could not find the `pdflatex` executable.","error":"FileNotFoundError: [Errno 2] No such file or directory: 'pdflatex'"},{"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`.","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.","error":"LaTeX Error: Missing \\begin{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`.","cause":"You are likely trying to access `pylatex.Document` directly or using an outdated import statement like `from pylatex.document import Document`.","error":"AttributeError: module 'pylatex' has no attribute 'Document'"}]}