A comprehensive Python library for programmatically creating and compiling LaTeX documents and snippets.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Text formatting commands and environments for controlling font sizes, colors, spacing, and basic document elements. These classes provide the building blocks for document layout and text appearance.
Environments that modify text size using LaTeX's standard size commands.
class HugeText(Environment):
def __init__(self, data=None):
"""
Environment for 'Huge' text size.
Parameters:
- data: str or LatexObject, content to format
"""
class LargeText(Environment):
def __init__(self, data=None):
"""
Environment for 'Large' text size.
Parameters:
- data: str or LatexObject, content to format
"""
class MediumText(Environment):
def __init__(self, data=None):
"""
Environment for 'large' text size.
Parameters:
- data: str or LatexObject, content to format
"""
class SmallText(Environment):
def __init__(self, data=None):
"""
Environment for 'small' text size.
Parameters:
- data: str or LatexObject, content to format
"""
class FootnoteText(Environment):
def __init__(self, data=None):
"""
Environment for 'footnotesize' text size.
Parameters:
- data: str or LatexObject, content to format
"""Usage example:
from pylatex import Document, Section
from pylatex import HugeText, LargeText, SmallText
doc = Document()
with doc.create(Section('Text Sizes')):
doc.append(HugeText('This is huge text'))
doc.append(LargeText('This is large text'))
doc.append('This is normal text')
doc.append(SmallText('This is small text'))
doc.append(FootnoteText('This is footnote-sized text'))Text coloring using the xcolor package with support for standard and custom colors.
class TextColor(ContainerCommand):
def __init__(self, color, data):
"""
Environment for colored text.
Parameters:
- color: str, color name or specification
- data: str or LatexObject, content to color
Requires:
- xcolor package
"""Usage example:
from pylatex import Document, TextColor
doc = Document()
# Standard colors
doc.append(TextColor('red', 'This text is red'))
doc.append(TextColor('blue', 'This text is blue'))
# Custom colors (define in document first)
doc.add_color('mygreen', 'RGB', '0,128,0')
doc.append(TextColor('mygreen', 'This text is custom green'))
# HTML-style colors
doc.append(TextColor('HTML', 'FF5733', 'This text uses HTML color'))Basic commands for controlling page breaks, line breaks, and spacing.
class NewPage(CommandBase):
"""Command that adds a new page (\\newpage)."""
class LineBreak(CommandBase):
"""Command that adds a line break (\\linebreak)."""
class NewLine(CommandBase):
"""Command that adds a new line (\\newline)."""
class HFill(CommandBase):
"""Command that fills horizontal space (\\hfill)."""Usage example:
from pylatex import Document, NewPage, LineBreak, NewLine, HFill
doc = Document()
doc.append('First paragraph.')
doc.append(NewLine())
doc.append('Second line of same paragraph.')
doc.append(LineBreak())
doc.append('Third line after line break.')
# Horizontal spacing
doc.append('Left text')
doc.append(HFill())
doc.append('Right text')
# Page break
doc.append('Content on first page.')
doc.append(NewPage())
doc.append('Content on second page.')Text size and color can be combined for complex formatting:
from pylatex import Document, TextColor, LargeText
doc = Document()
# Nested formatting
with doc.create(LargeText()) as large:
large.append(TextColor('red', 'Large red text'))
# Or create complex structures
colored_large = TextColor('blue', LargeText('Large blue text'))
doc.append(colored_large)Create documents with varied text formatting:
from pylatex import Document, Section
from pylatex import HugeText, TextColor, NewLine
doc = Document()
with doc.create(Section('Formatted Text Example')):
doc.append(HugeText('Main Title'))
doc.append(NewLine())
doc.append(TextColor('gray', 'Subtitle in gray'))
doc.append(NewLine())
doc.append('Regular paragraph text with ')
doc.append(TextColor('red', 'red highlighted'))
doc.append(' words.')Define colors once and use throughout the document:
from pylatex import Document, TextColor
doc = Document()
# Define custom colors
doc.add_color('primaryblue', 'RGB', '0,102,204')
doc.add_color('secondarygreen', 'RGB', '34,139,34')
doc.add_color('accent', 'CMYK', '0,0.5,1,0')
# Use throughout document
doc.append(TextColor('primaryblue', 'Primary color text'))
doc.append(TextColor('secondarygreen', 'Secondary color text'))
doc.append(TextColor('accent', 'Accent color text'))The text size classes correspond to LaTeX's standard size commands:
\\Huge (largest)\\Large\\large\\small\\footnotesize (smallest)Additional LaTeX sizes not directly implemented as classes can be used with generic commands:
from pylatex.base_classes import Command
# Other LaTeX sizes
tiny_text = Command('tiny')
scriptsize_text = Command('scriptsize')
normalsize_text = Command('normalsize')The xcolor package supports various color models:
TextColor('red!50!blue', 'Mixed color')Package('xcolor', options=['dvipsnames'])from pylatex import Document, Package, TextColor
doc = Document()
doc.packages.append(Package('xcolor', options=['dvipsnames']))
# Extended color names
doc.append(TextColor('ForestGreen', 'Forest green text'))
doc.append(TextColor('RoyalBlue', 'Royal blue text'))
doc.append(TextColor('Maroon', 'Maroon text'))Install with Tessl CLI
npx tessl i tessl/pypi-pylatex