A simple utility that produces github-styled diffs in a synthesizable LaTeX format.
lualatexfor pdf generation (optional, only needed for PDF output)python3.7+
pip install diff2latex- Clone this repository
git clone https://github.com/divadiahim/diff2latex.git cd diff2latex - Install dependencies and build the package
pip install -r requirements.txt pip install -e .
- 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 namedoutputcontainingexample.tex. - To additionally generate a pdf pass the
--pdf-outputflag.
You can also use diff2latex as a Python library for programmatic diff processing:
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")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")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()diff2latex.diff_to_latex(content, **kwargs)- Convert diff string to LaTeXdiff2latex.diff_file_to_latex(file_path, **kwargs)- Convert diff file to LaTeXdiff2latex.create_diff_pdf(content, output_path, **kwargs)- Create PDF directlydiff2latex.DiffProcessor(**kwargs)- Class-based processor for multiple diffs
All core classes are importable for advanced usage:
Diff2Latex- Main diff processing classCharColorizer- Syntax highlightingColorMap- Color mapping utilitiesCodeBlock,Cell,Line- Data models
See examples.py for more detailed usage examples.
# 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.txtRun the smoke tests to ensure everything works:
python test_package.py- Update the version in
diff2latex/__init__.py - Build and check the package:
./publish.sh
- Upload to PyPI:
# Test on TestPyPI first (recommended) python -m twine upload --repository testpypi dist/* # Then upload to PyPI python -m twine upload dist/*
- Add a horizontal diff style.
- Add comprehensive unit tests
- Add GitHub Actions CI/CD
- Add documentation with examples