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

text-formatting.mddocs/

Text Formatting and Basic Elements

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.

Capabilities

Text Size Environments

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'))

Color 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'))

Page and Line Control Commands

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 Formatting Patterns

Combining Size and Color

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)

Multiple Text Elements

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

Document-wide Color Definitions

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'))

LaTeX Size Hierarchy

The text size classes correspond to LaTeX's standard size commands:

  • HugeText: \\Huge (largest)
  • LargeText: \\Large
  • MediumText: \\large
  • Normal text size (default)
  • SmallText: \\small
  • FootnoteText: \\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')

Color Models

The xcolor package supports various color models:

  • RGB: TextColor('red!50!blue', 'Mixed color')
  • HTML: Define colors with HTML hex codes
  • Named colors: 'red', 'blue', 'green', 'black', 'white', etc.
  • dvipsnames: Extended color names when using 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

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