CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pylatex

A comprehensive Python library for programmatically creating and compiling LaTeX documents and snippets.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

document.mddocs/

Document Management

Core document creation, compilation, and structure management including sections, packages, and document-wide settings. The Document class serves as the top-level container for all LaTeX content and manages the document lifecycle from creation to PDF compilation.

Capabilities

Document Creation

The Document class represents a complete LaTeX document with preamble, packages, and content sections.

class Document(Environment):
    def __init__(self, default_filepath="default_filepath", *, 
                 documentclass="article", document_options=None, 
                 fontenc="T1", inputenc="utf8", font_size="normalsize", 
                 lmodern=True, textcomp=True, microtype=None, 
                 page_numbers=True, indent=None, geometry_options=None, 
                 data=None):
        """
        Create a new LaTeX document.
        
        Parameters:
        - default_filepath: str, default file path for output
        - documentclass: str, LaTeX document class ('article', 'report', 'book', etc.)
        - document_options: list, options for document class
        - fontenc: str, font encoding (default 'T1')
        - inputenc: str, input encoding (default 'utf8')  
        - font_size: str, default font size
        - lmodern: bool, use Latin Modern fonts
        - textcomp: bool, enable text companion fonts
        - microtype: bool | None, enable microtypography (None uses config default)
        - page_numbers: bool, include page numbers
        - indent: bool, enable paragraph indentation
        - geometry_options: list, page geometry options
        - data: initial content for document
        """

Usage example:

from pylatex import Document, Package

# Basic document
doc = Document('my_document', documentclass='article')

# Document with custom options
doc = Document(
    'technical_report',
    documentclass='report',
    document_options=['12pt', 'a4paper'],
    geometry_options=['margin=1in'],
    font_size='large'
)

File Generation

Generate LaTeX source files and compile to PDF with various compilation options.

def generate_tex(self, filepath=None):
    """
    Generate LaTeX source file.
    
    Parameters:
    - filepath: str, output file path (optional, uses default_filepath if None)
    """

def generate_pdf(self, filepath=None, *, clean=True, clean_tex=True, 
                 compiler=None, compiler_args=None, silent=True):
    """
    Compile document to PDF.
    
    Parameters:
    - filepath: str, output file path
    - clean: bool, remove auxiliary files after compilation
    - clean_tex: bool, remove .tex file after compilation
    - compiler: str, LaTeX compiler ('pdflatex', 'xelatex', 'lualatex')
    - compiler_args: list, additional compiler arguments
    - silent: bool, suppress compiler output
    
    Returns:
    - str: path to generated PDF file
    """

Document Customization

Modify document properties, add colors, and set LaTeX variables.

def add_color(self, name, model, description):
    """
    Add color definition to document.
    
    Parameters:
    - name: str, color name
    - model: str, color model ('rgb', 'cmyk', 'gray')
    - description: str, color specification
    """

def set_variable(self, name, value):
    """
    Set LaTeX variable in document.
    
    Parameters:
    - name: str, variable name
    - value: str, variable value
    """

def change_length(self, parameter, value):
    """
    Change LaTeX length parameter.
    
    Parameters:
    - parameter: str, length parameter name
    - value: str, new length value
    """

def change_page_style(self, style):
    """
    Change page style.
    
    Parameters:
    - style: str, page style name
    """

def change_document_style(self, style):
    """
    Change document style.
    
    Parameters:
    - style: str, document style name
    """

Usage example:

from pylatex import Document

doc = Document()

# Add custom color
doc.add_color('myblue', 'RGB', '0,0,255')

# Set document variables
doc.set_variable('title', 'My Document Title')
doc.set_variable('author', 'John Doe')

# Modify page layout
doc.change_length('parindent', '0pt')
doc.change_page_style('fancy')

Package Management

The Document class provides access to package management through the packages attribute.

class Package(CommandBase):
    def __init__(self, name, options=None):
        """
        LaTeX package declaration.
        
        Parameters:
        - name: str, package name
        - options: list or str, package options
        """

Usage example:

from pylatex import Document, Package

doc = Document()

# Add packages
doc.packages.append(Package('amsmath'))
doc.packages.append(Package('geometry', options=['margin=1in']))
doc.packages.append(Package('xcolor', options=['dvipsnames']))

# Packages are automatically added when using certain classes
from pylatex.math import Matrix
matrix = Matrix([[1, 2], [3, 4]])  # Automatically adds 'amsmath' package
doc.append(matrix)

Document Properties

Accessible Attributes

# Document content areas
doc.packages     # Package list
doc.preamble     # Preamble content
doc.variables    # Document variables

# Document metadata
doc.default_filepath    # Default output path
doc.documentclass      # Document class

Content Management

Documents act as containers and support all container operations:

from pylatex import Document, Section

doc = Document()

# Add content directly
doc.append('Some text')

# Create structured content
with doc.create(Section('Introduction')) as section:
    section.append('This is the introduction.')
    
# Append existing objects
section = Section('Methods')
section.append('This is the methods section.')
doc.append(section)

Compilation Requirements

PDF compilation requires a LaTeX distribution to be installed on the system:

  • Windows: MiKTeX or TeX Live
  • macOS: MacTeX or TeX Live
  • Linux: TeX Live (usually available in package managers)

The default compiler is pdflatex, but you can specify alternatives:

doc.generate_pdf(compiler='xelatex')  # For better Unicode support
doc.generate_pdf(compiler='lualatex')  # For advanced typography features

Install with Tessl CLI

npx tessl i tessl/pypi-pylatex

docs

base-classes.md

configuration.md

document.md

figures.md

index.md

layout.md

lists.md

math.md

quantities.md

references.md

sectioning.md

tables.md

text-formatting.md

tikz.md

utilities.md

tile.json