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:
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.
I had no issues with this on first attempt and this is what I did. I use MiKTeX on Windows 8.
I used MiKTeX Package Manager (admin) to install latexdiff
.
I downloaded and installed Strawberry Perl.
I searched for "cmd" on start menu/window search and saw perl(command line)
I opened perl(command line)
and changed directory to the directory where my oldfile.tex
and newfile.tex
was (both in same folder).
Then I executed latexdiff oldfile.tex newfile.tex > diff.tex
on the shell prompt.
It generated a diff.tex
file in the same folder which I compiled using MiKTeX to get the diff.pdf
file.
Best Answer
As an alternative to running latexdiff locally and copying the file back to Overleaf you can run latexdiff at overleaf.
Here is a project with two versions of a tex file and a displayed diff
It is currently live on Overleaf but this link may not be good forever, but I show the files inline below.
https://www.overleaf.com/read/xhszmytdmqqg
main.tex
(the old version)main2.tex
(new version)latexmkrc
(to instruct overleaf to show the diff)When you want to see the "normal" typeset version of
main.tex
without the diff marking, just add a#
comment marker to thelatexmkrc
setting so it looks likethen recompiling
main.tex
ormain2.tex
will produce the normal typeset output.