Skip to main content

Simple utility that produces diffs in a LaTeX format

Project description

diff2latex

A simple utility that produces github-styled diffs in a synthesizable LaTeX format.

Requirements

  • lualatex for pdf generation (optional, only needed for PDF output)
  • python 3.7+

Installation

From PyPI (recommended)

pip install diff2latex

From source

  1. Clone this repository
    git clone https://github.com/divadiahim/diff2latex.git
    cd diff2latex
    
  2. Install dependencies and build the package
    pip install -r requirements.txt
    pip install -e .
    

Usage

Command Line Interface

  • Grab 2 files that you want to diff and generate a plain diff diff -u file_1 file_2 > example.diff.
  • To generate a LaTeX diff run diff2latex --highlight="default" build example.diff output. This will create a directory named output containing example.tex.
  • To additionally generate a pdf pass the --pdf-output flag.

Library Usage

You can also use diff2latex as a Python library for programmatic diff processing:

Simple Usage

import diff2latex

# Convert diff content to LaTeX
diff_content = """--- old.py
+++ new.py
@@ -1,2 +1,2 @@
-print("Hello")
+print("Hello, World!")
"""

latex_output = diff2latex.diff_to_latex(
    diff_content,
    highlight_style="github",
    file_extension=".py"
)

# Save to file
with open("output.tex", "w") as f:
    f.write(latex_output)

# Or create PDF directly
diff2latex.create_diff_pdf(diff_content, "output.pdf", highlight_style="github")

Class-based Processing

from diff2latex import DiffProcessor

# Create processor with default settings
processor = DiffProcessor(
    font_family="Monaco",
    highlight_style="monokai",
    file_extension=".cpp"
)

# Process multiple diffs with consistent styling
latex1 = processor.process(diff_content1)
latex2 = processor.process(diff_content2)

# Create PDFs
processor.create_pdf(diff_content1, "diff1.pdf")
processor.create_pdf(diff_content2, "diff2.pdf")

Advanced Usage

from diff2latex import Diff2Latex, CharColorizer
from io import StringIO

# Use core classes directly
colorizer = CharColorizer(style_name="github", ext=".py")
diff_io = StringIO(diff_content)
differ = Diff2Latex.build(diff_io, colorizer=colorizer)
latex_lines = differ.to_latex()

Available Functions

  • diff2latex.diff_to_latex(content, **kwargs) - Convert diff string to LaTeX
  • diff2latex.diff_file_to_latex(file_path, **kwargs) - Convert diff file to LaTeX
  • diff2latex.create_diff_pdf(content, output_path, **kwargs) - Create PDF directly
  • diff2latex.DiffProcessor(**kwargs) - Class-based processor for multiple diffs

Available Classes

All core classes are importable for advanced usage:

  • Diff2Latex - Main diff processing class
  • CharColorizer - Syntax highlighting
  • ColorMap - Color mapping utilities
  • CodeBlock, Cell, Line - Data models

See examples.py for more detailed usage examples.

Development

Setting up development environment

# Clone the repository
git clone https://github.com/divadiahim/diff2latex.git
cd diff2latex

# Run the development setup script
./setup_dev.sh

# Or manually:
pip install -e .
pip install -r requirements.txt

Testing

Run the smoke tests to ensure everything works:

python test_package.py

Publishing

  1. Update the version in diff2latex/__init__.py
  2. Build and check the package:
    ./publish.sh
    
  3. Upload to PyPI:
    # Test on TestPyPI first (recommended)
    python -m twine upload --repository testpypi dist/*
    
    # Then upload to PyPI
    python -m twine upload dist/*
    

TODOs

  • Add a horizontal diff style.
  • Add comprehensive unit tests
  • Add GitHub Actions CI/CD
  • Add documentation with examples

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

diff2latex-0.1.0.tar.gz (40.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

diff2latex-0.1.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file diff2latex-0.1.0.tar.gz.

File metadata

  • Download URL: diff2latex-0.1.0.tar.gz
  • Upload date:
  • Size: 40.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for diff2latex-0.1.0.tar.gz
Algorithm Hash digest
SHA256 970358dd6238d479389fc0a03d77c07595e84d86d1d2f920b9f39adddb522d1e
MD5 4af5dea5c7569d3d1ebfacb19cd765c2
BLAKE2b-256 8235f08ebba8673b830281ad10c6c3b1cd757338394c99c658885b4ce66ad606

See more details on using hashes here.

File details

Details for the file diff2latex-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: diff2latex-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for diff2latex-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6d508893a62a2d89345857dee8dd272a3ddc0db4c14df08eed73cfceea50b2d0
MD5 a02ce081f1df075ade5cc2d1381e8744
BLAKE2b-256 2d57e2354c4a0b02c2abb4fcd5da5783291d980faf446ac3199bd249febe3300

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page