LaTeX + GIT - mark differences since specific commit

I've done this with a combination of git show and latexdiff. One caveat: this is for single-file documents, might be more complicated if you have split the document into several files and used \input/\include.

  1. Start by getting the version of a file from a previous commit, with

    git show <commit>:filename.tex > tmp.tex
    
  2. Run latexdiff to generate a new .tex file with the differences highlighted:

    latexdiff tmp.tex filename.tex > tmpdiff.tex
    
  3. Compile tmpdiff.tex as usual.

For git show you need to specify the complete path in the repository I think, so if it is placed in a subfolder you need path/to/filename.tex.

I wrote a script to automate this somewhat, and also clean up leftovers. Admittedly, I haven't used this much yet, but it may be a starting point.

#!/bin/bash    
git show $1:file.tex > tmp.tex
latexdiff tmp.tex file.tex > tmpdiff.tex
latexmk -pdf -interaction=nonstopmode tmpdiff.tex
mv -v tmpdiff.pdf Diffed.pdf
rm -v tmp.tex tmpdiff.*

As an alternative, here's what I've done in the past. It's simple but not git-specific (as it happens I was using git at the time though not very well). It's closer to your manual approach but using an approriate tool to reduce mistakes:

I used the changes package to mark up the changes as I make them. It needed a little customising to suit my needs (which were similar in that I needed to hand over a highlighted pdf to a non-LaTeX user):

\usepackage[authormarkup=none]{changes}
\setaddedmarkup{{\color{blue!75!black}#1}}
\setdeletedmarkup{\protect{\color{blue!25!gray}\sout{#1}}}
\definechangesauthor[name={Chris}]{CH}
\DeclareRobustCommand{\add}[1]{\added[id=CH]{#1}}
\DeclareRobustCommand{\del}[1]{\deleted[id=CH]{#1}}
\DeclareRobustCommand{\rep}[2]{\replaced[id=CH]{#1}{#2}}
\setremarkmarkup{\footnote{#1:\textcolor{blue}{#2}}}

The three \DeclareRobustCommand lines are merely convenience macros.

This allowed me to use \listofchanges as well.

However it didn't pick up the automated (find&replace/regex based) changes I made (equation~\ref->Equation~\ref, a lot of acronym \acs ->\acl etc.). I could have cooked up a regex find/replace to do this but in the circumstances it would have been unnecessary clutter (and made a huge listofchanges).

Note that by passing [final] either to \documentclass or by \usepackage[final]{changes} the changes aren't displayed.

A script to insert the \added, \deleted and \replaced macros based on a git diff would no doubt be possible but close to reinventing a bad version of latexdiff so I wouldn't recommend it.


git-latexdiff was written exactly for this: https://gitlab.com/git-latexdiff/git-latexdiff

I obviously have a strong biais since I'm the main author, but it seems to be the most advanced tool so far.

See also this question: Using latexdiff with git.

Tags:

Git

Changes