Skip to content

Instantly share code, notes, and snippets.

@Markus-de-Koster
Created May 7, 2024 16:19
Show Gist options
  • Select an option

  • Save Markus-de-Koster/0829208299ce685e97c601ddb23cc89e to your computer and use it in GitHub Desktop.

Select an option

Save Markus-de-Koster/0829208299ce685e97c601ddb23cc89e to your computer and use it in GitHub Desktop.
Mark changes in a LaTeX document in color

LaTeX changes in color

One of your papers was reviewed and you were asked to mark changes in a different color? No problem! Use this method to easily mark everything you changed in your LaTeX document since your submitted the original paper. Also applicable to non-science use cases.

Steps

  1. get the difference between two commits (which you want to compare) of the tex file and save the output
git diff <old-commit> <new-commit> -- path/to/file.tex > changes.diff
  1. Modified and added lines will be marked using the following script
python3 latex_diff_mark_changes.py
  1. Include the diff in the LaTeX document
\documentclass{article}
\usepackage[draft]{changes}  % Use 'draft' to show changes
\begin{document}

\input{formatted_changes.tex}

\end{document}
import re
def parse_diff(diff_file):
with open(diff_file, 'r') as file:
lines = file.readlines()
new_content = []
inside_diff = False
for line in lines:
if line.startswith('@@'):
inside_diff = True
continue
if inside_diff:
if line.startswith('+'):
content = line[1:].strip() # Remove the '+' sign
new_content.append(f'\\added{{{content}}}\n')
elif line.startswith('-'):
# Skipping removals
continue
return new_content
diff_content = parse_diff('changes.diff')
with open('formatted_changes.tex', 'w') as out_file:
out_file.writelines(diff_content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment