# LaTeX + GIT - mark differences since specific commit

I store LaTeX document in local GIT repository. Once I accomplish appropriate part of document I compile it to PDF and send it to my supervisor who is not familiar with LaTeX nor GIT.

However, he ask me to mark all changes which I made since previous PDF by red color. Currently, I do that manually, so I also need to remove it from my document, once changes are accepted. It is time consuming and source of mistakes.

Is there any automated way to:

• select GIT commit or specific date which will be used to compare against current document version,
• copy current document to temporary file and mark all differences by red font in that temporary file,
• compile temporary file to PDF?

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.*

• I actually prefer this to my own answer (so +1) but felt that the alternative was worth stating. – Chris H Sep 10 '15 at 12:17
• You should use something like mktemp instead of assuming the file names "tmp.tex" and "tmpdiff.tex" are unused. – Najib Idrissi Sep 11 '15 at 12:28
• @NajibIdrissi Well, I didn't assume, I knew that they were unused in the case I wrote the script for, and I did call the script a "starting point". But then I didn't even know about mktemp, so thanks for that hint. – Torbjørn T. Sep 11 '15 at 12:56
• If you have split your document into several tex-files (which I recommend), it's possible to use a tool like latexpand or similar to merge it into one tex-file. – Jonas Granholm May 27 '16 at 9:14
• @stephanmg I think I added it so that I'd always get a PDF, which probably seemed preferrable at the time. Of course, you might get a useless PDF ... – Torbjørn T. Feb 18 '20 at 16:08

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}
\setdeletedmarkup{\protect{\color{blue!25!gray}\sout{#1}}}
\definechangesauthor[name={Chris}]{CH}
\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.

• Just as information, this week a new package easyReview was uploaded to CTAN. From its documentation seems that it provides commands for replacing, suppressing, highlighting, ... parts of a .tex file. All review changes and comments can be switched off before printing but it doesn't automaticaly apply them. – Ignasi Sep 16 '15 at 7:39
• @Ignasi that looks quite interesting though I'm not sure it would have worked for me given the dependencies on both soul and todonotes, both of which I've had trouble with in the past (not insurmountable trouble I'm sure but not worth it at the time). – Chris H Sep 16 '15 at 9:30
• This is not what the OP wants, because he already made the changes. – stephanmg Feb 18 '20 at 15:06

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.

• This basically a shell script using git and latexdiff. – stephanmg Feb 18 '20 at 15:08

I think latex-diff can do this. You may need to update to get this particular feature. Basically, something like Torbjørn T.'s answer is implemented as part of latex-diff. There are also some dependencies which are likely not available on Windows.

You'll want to do something like

    latexdiff-vc --git --pdf -r rev file.tex


where rev is the hash of the commit you want to compare with. It should even work with tags

The manual has details.