PyLaTeX Documentation

repository·master·Indexed 25 days ago

https://github.com/jeltef/pylatex

A Python library for creating and compiling LaTeX files or snippets. It provides an extensible interface using a class hierarchy of LatexObject, Container, and Document to generate LaTeX code and compile it into PDFs. Supports automatic reruns via latexmk and optional dependencies for matrices, matplotlib, and quantities.

Tokens
1.2K
Snippets
3
Records
12
Agent score
79%

What's inside PyLaTeX

  1. Understand the two primary usage patterns of PyLaTeX

    master

    PyLaTeX is designed for two distinct workflows:

    1. Generating full PDFs: Best used when all content in the PDF is generated by Python (e.g., exporting data from a database).
    2. Generating LaTeX snippets: Best used when some text is written manually, but specific elements (like Matplotlib plots) need to be automatically generated and inserted into a report.
  2. How PyLaTeX works: Generating and Compiling

    master

    PyLaTeX is designed around two primary tasks:

    1. Generating LaTeX code: Using a system of Python classes to represent LaTeX components.
    2. Compiling LaTeX documents: Turning that generated code into a document (like a PDF).

    You can use PyLaTeX to wrap existing LaTeX code or to build new documents from scratch using its class hierarchy. Generated code can either be exported as raw LaTeX strings or compiled into a file.

  3. Using Containers and Documents to structure LaTeX

    master

    PyLaTeX uses Container objects to group other LaTeX classes. Containers behave like Python lists: they can be indexed and appended to.

    • Document: The most critical container. It represents a full LaTeX document. Use Document.generate_pdf() to compile the document into a PDF file. Unless you are only generating small snippets, you should wrap your content in a Document object.
    • Sections: PyLaTeX provides container classes for standard LaTeX sections, such as Section, Subsection, and Subsubsection. These can also be appended to and can contain other objects.
  4. Understand the PyLaTeX subclassing model

    master
    PyLaTeX relies extensively on class inheritance. When exploring the API, if a specific class appears to be missing methods or attributes you expect, check its parent classes. Many core functionalities and methods are defined in base classes and inherited by specialized subclasses.
  5. Handling raw LaTeX strings and escaping

    master

    By default, PyLaTeX escapes almost all strings for security and to ensure valid LaTeX generation. If you need to use raw LaTeX strings that should not be escaped, you have two primary methods:

    1. Use NoEscape

    Use the NoEscape string type (a subclass of str). This tells PyLaTeX to treat the string as raw LaTeX. Warning: Appending a NoEscape string to a regular str will result in a regular str (the most conservative type is chosen). Use it as a standalone object or ensure the final result remains NoEscape.

    2. Disable escaping on a container

    You can set the LatexObject.escape attribute to False on a container object. Warning: This affects all strings added to that object. Only use this for containers that are guaranteed not to hold potentially unsafe or unformatted text.

  6. Install LaTeX dependencies on Ubuntu

    master

    If you are using Ubuntu, you can install the necessary LaTeX processing tools and extra packages using apt-get. This includes latexmk and various TeX Live packages required for scientific and picture-based LaTeX content.

    sudo apt-get install texlive-pictures texlive-science \
    texlive-latex-extra latexmk
  7. Extending PyLaTeX functionality

    master

    To add support for new LaTeX features, extend the existing base classes. While all objects derive from LatexObject, it is usually more effective to extend specific base subclasses that match your needs, such as:

    • Environment
    • CommandBase

    Consult the API documentation to find the most appropriate base class for your extension.

  8. Install PyLaTeX with optional feature dependencies

    master

    Certain PyLaTeX features require additional libraries. You can install these dependencies using pip extras.

    To install support for matrices, matplotlib, and quantities, run:

    pip install pylatex[matrices]

    Note: The documentation mentions that features requiring additional libraries include:

    • matrices (requires Numpy)
    • matplotlib
    • quantities
  9. Install PyLaTeX via pip

    master

    Install the PyLaTeX library using pip. Note that after installing the Python package, you must also install a LaTeX processor (like TeX Live) and other system dependencies to enable file compilation.

    pip install pylatex
  10. Enable automatic LaTeX reruns with latexmk

    master

    If your LaTeX compiler requires multiple passes to complete a document (e.g., to resolve references or citations), PyLaTeX can handle this automatically if latexmk is installed on your system. PyLaTeX will detect the presence of latexmk and use it instead of a standard LaTeX compiler to ensure the document is fully processed.

    To enable this functionality, install latexmk using your system's package manager.

  11. Exporting LaTeX code from objects

    master

    Most PyLaTeX classes inherit from LatexObject and provide methods to retrieve their LaTeX representation:

    • LatexObject.dumps(): Returns the LaTeX-formatted code as a string.
    • LatexObject.generate_tex(): Writes the LaTeX-formatted code directly to a file.
  12. Creating custom commands with `Command`

    master

    You can create custom LaTeX commands using the Command class. Commands support:

    • Arguments: Supplied via {}.
    • Options: Supplied via [].

    Arguments and options can be passed as single strings or lists. For more complex structures, you can use Arguments or Options objects.