Using Git and latexdiff

2014-03-01

If you use git to manage your LaTeX documents, then you can see the differences in many ways. You can display word by word changes made in a form that looks like this:

@@ -3045,12 +3045,15 @@ \section{Proof of Proposition~\ref{ppnCLTFirstHitShort}}
We finally define the function $g$ that appearsappearing in Property (6).
Forfor $x = (q,\xi) \in \CM$, letby setting $g((q,\xi)) = \xi \in \mathbb{Z}^2$.

Or inspect changes line by line:

diff --git a/refs.bib b/refs.bib
index 349c0c3..65b8321 100644
--- a/refs.bib
+++ b/refs.bib
@@ -5556,7 +5585,7 @@
   pages                = {2636--2647}
 }

-@Book{           Rozovski90,
+@Book{           Rozovskii90,
   author       = {Rozovski{\u\i}, B. L.},
   title        = {Stochastic evolution systems},
   series       = {Mathematics and its Applications (Soviet Series)},

Or as a compiled PDF using latexdiff:

PDF diff

Here’s how to achieve the above. Edit ~/.gitconfig and add the following:

[alias]
wdiff = diff --color-words --ignore-all-space
ldiff = difftool -y -t latex

[difftool.latex]
cmd = latexdiff "$LOCAL" "$REMOTE"

Now typing

git ldiff HEAD~1 > diff.tex

runs latexdiff and puts the differences in diff.tex. Tex this as usual to get your PDF.

Alternately for a quick inspection of the differences at the TeX level, use git wdiff HEAD~1 for word diffs, or git diff HEAD~1 for line diffs. You can pipe the output to aha to make an html to share if you need to.

Skipping non-tex files.

The above solution might choke if you try and compare two non-TeX files (e.g. when a figure has changed between revisions). It also will be problematic if more than one tex file has changed. If the files are all independent, then you can solve both these problems easily. Put the following in ~/.gitconfig:

[alias]
wdiff = diff --color-words --ignore-all-space
ldiff = difftool -y -t latex

[difftool.latex]
cmd = ldiff "$LOCAL" "$REMOTE" "$MERGED"

Next create an executable file ldiff somewhere in your path with the following:

# Make sure inputs are tex files
LOCAL="$1"
REMOTE="$2"
MERGED="$3"

if [[ "${MERGED##*.}" == tex ]]; then
    output="${MERGED%.tex}-diff.tex"

    if [[ -f "$output" ]]; then 
        read -p "File $output exists. Overwrite? " confirm
        [[ "$confirm" != y && "$confirm" != yes ]] && exit 1
    fi

    latexdiff "$LOCAL" "$REMOTE" > "$output"
    echo "Generated $output"
else
    echo "Skipped $MERGED (non tex)."
fi

Now running

git ldiff HEAD~1

will produce a -diff.tex file for every TeX file that changed, and do nothing for other files. You can compile them all in one shot with latexmk.

A complete solution

If you want a full solution that handles changed included files use latexbatchdiff git-latexdiff or this fork of latexdiff. There’s also a useful discussion on StackExchange.

🗫 Comments

  • Dr.Feng
    It may have issues when colored text is already in tex file

    Dr.Feng (2021-08-11 23:51:42 EDT)

    Hi Prof. Iyer. Thanks for sharing the detailed instruction. I tried this when doing a paper revision. For revision, I already had some text highlighted with color, such as \textcolor{blue}{foo bar}, and that is because revision typically requires changes being highlighted. In this case, when compiling the resulted -diff.tex file, errors occurs. Have you encountered issues similar to this? Thanks.

  • Gautam Iyer
    Re: It may have issues when colored text is already in tex file

    Gautam Iyer (2021-08-12 16:26:48 EDT)

    Yes, many complex changes break latexdiff, and the resulting file won’t compile. Mostly I make revisions without any highlighting; then if the -diff file complies cleanly, I send the PDF to co-authors / journal. If it doesn’t, I use

    git wdiff @~1 | aha > changes.html
    

    and then send the changes.html file to my co-authors.

  • Dr.Feng
    Re: Re: It may have issues when colored text is already in tex file

    Dr.Feng (2021-10-09 03:16:41 EDT)

    Hi, Prof. Iyer. Thank you for the reply. Unfortunately, journals in my area topically require that changes be highlighted for revision/resubmission. But your answer offers an alternative to latexdiff. Thanks.

📮 Leave a comment (Spammers beware: All comments are moderated)

Sorry. There was an error submitting your comment. Please try again, or contact me if the problem persists.
Sending comment; please wait.
Thanks. Your comment was successfully submitted. It will appear here shortly if it isn't spam.