[Tex/LaTex] LaTeX + GIT – mark differences since specific commit

changesgit

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?

Best Answer

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