[Tex/LaTex] latexdiff and lstlisting

latexdifflistings

I was wondering if it is possible to have latexdiff ignore all lstlistings from my documents when finding diffs. Most of my listings contain special characters. That works rather nicely within an lstlisting but when latexdiff tries to compare, such listings end up outside of the lstlisting and therefore need to be escaped manually. Is there a way to go around that?

Best Answer

You can use the approach given in https://tex.stackexchange.com/a/73649/15925 to tell latexdiff to ignore the lstlisting environment. Placing

 PICTUREENV=(?:picture|DIFnomarkup|lstlisting)[\w\d*@]*

in ld.cfg and running latexdiff -c ld.cfg j1.tex j2.tex >diff.tex where j1.tex is

\documentclass{article}

\usepackage{listings}

\begin{document}

Text.

\begin{lstlisting}
  for $a do
\end{lstlisting}

Text.

\end{document}

and j2.tex is

\documentclass{article}

\usepackage{listings}

\begin{document}

Text changed.

\begin{lstlisting}
  for $a do y
\end{lstlisting}

Text.

\end{document}

produces diff.tex containing:

\documentclass{article}
%DIF LATEXDIFF DIFFERENCE FILE
%DIF DEL j1.tex   Wed Oct 16 16:53:10 2013
%DIF ADD j2.tex   Wed Oct 16 16:53:33 2013

\usepackage{listings}
%DIF PREAMBLE EXTENSION ADDED BY LATEXDIFF
%DIF UNDERLINE PREAMBLE %DIF PREAMBLE
\RequirePackage[normalem]{ulem} %DIF PREAMBLE
\RequirePackage{color}\definecolor{RED}{rgb}{1,0,0}\definecolor{BLUE}{rgb}{0,0,1} %DIF PREAMBLE
\providecommand{\DIFadd}[1]{{\protect\color{blue}\uwave{#1}}} %DIF PREAMBLE
\providecommand{\DIFdel}[1]{{\protect\color{red}\sout{#1}}}                      %DIF PREAMBLE
%DIF SAFE PREAMBLE %DIF PREAMBLE
\providecommand{\DIFaddbegin}{} %DIF PREAMBLE
\providecommand{\DIFaddend}{} %DIF PREAMBLE
\providecommand{\DIFdelbegin}{} %DIF PREAMBLE
\providecommand{\DIFdelend}{} %DIF PREAMBLE
%DIF FLOATSAFE PREAMBLE %DIF PREAMBLE
\providecommand{\DIFaddFL}[1]{\DIFadd{#1}} %DIF PREAMBLE
\providecommand{\DIFdelFL}[1]{\DIFdel{#1}} %DIF PREAMBLE
\providecommand{\DIFaddbeginFL}{} %DIF PREAMBLE
\providecommand{\DIFaddendFL}{} %DIF PREAMBLE
\providecommand{\DIFdelbeginFL}{} %DIF PREAMBLE
\providecommand{\DIFdelendFL}{} %DIF PREAMBLE
%DIF END PREAMBLE EXTENSION ADDED BY LATEXDIFF

\begin{document}

Text \DIFaddbegin \DIFadd{changed}\DIFaddend .

\DIFdelbegin %DIFDELCMD < \begin{lstlisting}
%DIFDELCMD <   for $a do
%DIFDELCMD < \end{lstlisting}
%DIFDELCMD < %%%
\DIFdelend \DIFaddbegin \begin{lstlisting}
  for $a do y
\end{lstlisting}
\DIFaddend 

Text.

\end{document}

and compiling to give:

Sample output

As you can see changes in the text are highlighted, whereas the code is just that from the second file.

EDIT As asmeurer points out, you can avoid a config file by writing the command line

latexdiff --config='PICTUREENV=(?:picture|DIFnomarkup|lstlisting)[\w\d*@]*'

and this may be particularly relevant in a script or a makefile.

Related Question