Using the LaTeX Diff utility latexdiff


latexdiff is a very useful tool for comparing different versions of latex files. It produces a merged file that highlights the deleted and added sections in color (with strikout and underlines). It is very easy to install (just put the perl script latexdiff or latexdiff-so in a directory on your path) and can be downloaded from

http://www.ctan.org/tex-archive/support/latexdiff/

Example usage:

~/bin/latexdiff old.tex new.tex > diff.tex
or
~/bin/latexdiff --append-safecmd="diffblock" \
   --append-textcmd="email,gwh" old.tex new.tex > diff.tex

The optional arguments above tells latexdiff that \email{...} and \gwh{...} are latex macro commands that take a text argument that can be marked up for changes, while \diffblock{...} is a command that can safely appear inside \DIFdel and \DIFadd commands but whose argument should not be individually analyzed for changes.

In the header of my latex files, I define the following macros for convenience and to guide latexdiff when it occasionally gets confused.


% Use \diffblock{text section} to mark a section that latexdiff should
% compare as one block (however this also suppresses line breaking for
% this text section):
\newcommand{\diffblock}[1]{#1}

% Latexdiff occasionally deletes paragraph breaks. Use the following
% command to force a line break and space (but it doesn't succeed in
% making a real paragraph with indentation).
\newcommand{\diffnewpar}{
\diffblock{ \\  %
 \\
}
}
 
\RequirePackage{color}
% use \ghw{??Here is a comment:  need to check references} 
% to insert highlighted comments:
\newcommand{\gwh}[1]{{\color{magenta} [GWH: #1]}}

% For clean output for a final draft or when you are not using
% latexdiff, uncomment the following so that the above commands
% are ignored:

% \renewcommand{\diffnewpar}{}
% \renewcommand{\gwh}[1]{}

There were two main types of changes to my latex file to get them to work smoothly with latexdiff. These changes were easy to implement. The first was to insure there were no spaces between any commands and their braces (as stated in the latexdiff man page). So I replaced expressions such as


   $\frac{numerator} {denominator}$

with

   $\frac{numerator}{denominator}$

   or

   $\frac{numerator}{
     denominator}$

The other change was put a diffblock around some subscript operations (particularly if they had spaces in them), such as replacing:

$ p_{x 0} $

with

$ \diffblock{p_{x 0}} $

The author of latexdiff is working on a new release that will handle displayed math more reliably. Although I haven't had a problem, you can turn off change marking of displayed math by doing:


latexdiff --config "PICTUREENV=(?:displaymath|equation)[\w\d*@]*"  ...

latexdiff 0.2 works fine with the default version of the perl package Algorithm::Diff (version 1.15) bundled with the stand-alone version latexdiff-so. But I can't get it to work with the latest version of the Perl package Algorithm::Diff, version 1.1901.

I recently tried a few other utilities for comparing different versions of latex files, and thought that latexdiff was by far the best. (The one caution is that it is a bit slow (at least at version 0.2), taking 40 seconds to compare two 90kByte files. If any perl experts out there are looking for a useful programming project, speeding up latexdiff would be very valuable). There are several utilities out there, but some of them are buggy and require workarounds involving editing of the latex files to produce useful output. Some of the utilities I tried included texdiff (the bash shell and perl versions), ldiff, pdiff (which comes with a2ps), some of which use wdiff. pdiff may be useful for producing a highlighted postscript file comparing plain text files. Of course the ediff compare tool in emacs is also good for comparing plain text files on your screen.

GWH: 2005/08/18