latexdiff
latexdiff copied to clipboard
latexdiff with counters and \ifnum
% latexdiff --version
This is LATEXDIFF 1.2.0 (Algorithm::Diff 1.15 so, Perl v5.18.2)
(c) 2004-2016 F J Tilmann
Overview
I am trying to use latexdiff in a tex-project which uses structures of the following type:
\newcounter{IOF}
\setcounter{IOF}{1}
\subsection{Function-\ifnum \value{IOF}<10 0\fi\arabic{IOF}: do this}
\stepcounter{IOF}
latexdiff
deliveres good results whenever there is a change in the sectioning (see example). But, when a new heading is added, latexdiff
add \DIFadd{}'s around the counter (IOF
in my case) and the numeric condition \value{IOF}<10
:
\subsection{\DIFadd{Function-}\ifnum \value{\DIFadd{IOF}}\DIFadd{<10 0}\fi\DIFadd{\arabic{IOF}: do more things}}
leading to errors when compiling the latexdiff'ed file.
I could go around this issue by using regular expressions,
latexdiff ${oldfile} ${newfile} > $diff_file
sed -i -e 's/\\value{\\DIFadd{\(.*\)}}\(.\)/\\value{\1}\2/g' -e 's/\\DIFadd{<10\ 0}/<10\ \\DIFadd{0}/g' $diff_file
Example
Old file ifnum_counter_old.tex
\documentclass{article}
\begin{document}
\tableofcontents
\section{Functions}
\newcounter{IOF}
\setcounter{IOF}{1}
\subsection{Function-\ifnum \value{IOF} <10 0 \fi \arabic{IOF}: do this}
\stepcounter{IOF}
\subsection{Function-\ifnum \value{IOF} <10 0 \fi \arabic{IOF}: do same}
\stepcounter{IOF}
\end{document}
New file ifnum_counter_new.tex
\documentclass{article}
\begin{document}
\tableofcontents
\section{Functions}
\newcounter{IOF}
\setcounter{IOF}{1}
\subsection{Function-\ifnum \value{IOF}<10 0\fi\arabic{IOF}: do this}
\stepcounter{IOF}
\subsection{Function-\ifnum \value{IOF}<10 0\fi\arabic{IOF}: do something else}
\stepcounter{IOF}
\subsection{Function-\ifnum \value{IOF}<10 0\fi\arabic{IOF}: do more things}
\stepcounter{IOF}
This is the value of the counter IOF: \ifnum \value{IOF}<10 0\fi\arabic{IOF}
\end{document}
latexdiff-file ifnum_counter_latexdiff.tex
\documentclass{article}
%DIF LATEXDIFF DIFFERENCE FILE
%DIF DEL ifnum_counter_old.tex Thu Oct 20 15:09:35 2016
%DIF ADD ifnum_counter_new.tex Thu Oct 20 16:07:13 2016
%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}
\tableofcontents
\section{Functions}
\newcounter{IOF}
\setcounter{IOF}{1}
\subsection{Function-\ifnum \value{IOF}<10 0\fi\arabic{IOF}: do this}
\stepcounter{IOF}
\subsection{Function-\ifnum \value{IOF}<10 0\fi\arabic{IOF}: do \DIFdelbegin \DIFdel{same}\DIFdelend \DIFaddbegin \DIFadd{something else}\DIFaddend }
\stepcounter{IOF}
\DIFaddbegin
\subsection{\DIFadd{Function-}\ifnum \value{\DIFadd{IOF}}\DIFadd{<10 0}\fi\DIFadd{\arabic{IOF}: do more things}}
\stepcounter{IOF}
\DIFadd{This is the value of the counter IOF: }\ifnum \value{\DIFadd{IOF}}\DIFadd{<10 0}\fi\DIFadd{\arabic{IOF}
}\DIFaddend
\end{document}
latexdiff pdf:
latexdiff pdf (after the sed
fix):
sed -i -e 's/\\value{\\DIFadd{\(.*\)}}\(.\)/\\value{\1}\2/g' -e 's/\\DIFadd{<10\ 0}/<10\ \\DIFadd{0}/g' ifnum_counter_latexdiff.tex
as wanted.
\ifnum
is TeX rather than LaTeX syntax, and this is something that latexdiff explicitly cannot deal with (it is stated in the manual). The reason is that in order to process all TeX constructs it would essentially be necessary to recode the parsing done in TeX, which
I can suggest to use the ifthen package which implements conditionals with latex style commands. Alternatively embed the usage of ifnum
in a DIFnomarkup
environment (which you previously need to define to do absolutely nothing) (see page 3 of the latexdiff pdf manual)
Thanks for the explanation and for the maintenance of this great tool!
Probably I am missing something, but I tried out both of your suggestions without success.
I tried the ifthen
and etoolbox
packages for conditional implementations, but they produce processing errors when using them in the headings (i.e. \subsection
), due to some "paragraphing" in the table of contents
ERROR: Paragraph ended before @dottedtocline was complete.
Same is true using the \begin{DIFnomarkup}
and \end{DIFnomarkup}
alternative.
It seems that I cannot avoid the sed
workaround for this specific case.
I don't really know the ifthen package so I guess this simply might not work in a movable argument. However, I just tried the DIFnomarkup solution and it works for me. I edit the new file:
\newenvironment{DIFnomarkup}{}{}
\begin{document}
\tableofcontents
\section{Functions}
\newcounter{IOF}
\setcounter{IOF}{1}
\subsection{Function-\ifnum \value{IOF}<10 0\fi\arabic{IOF}: do this}
\stepcounter{IOF}
\subsection{Function-\ifnum \value{IOF}<10 0\fi\arabic{IOF}: do something else}
\stepcounter{IOF}
\begin{DIFnomarkup}
\subsection{Function-\ifnum \value{IOF}<10 0\fi\arabic{IOF}: do more things}
\stepcounter{IOF}
This is the value of the counter IOF: \ifnum \value{IOF}<10 0\fi\arabic{IOF}
\end{DIFnomarkup}
\end{document}
Of course, the markup in this block disappears (hence it is a workaround, not a solution)
Thanks again!
Inserting \DIFnomarkup{}
around the \ifnum \value{IOF}<10 0\fi
statement approaches the wanted effect (only the "zero" inside the \ifnum remains black, the rest of the added text becomes blue):
\documentclass{article}
\newenvironment{DIFnomarkup}{}{}
\begin{document}
\tableofcontents
\section{Functions}
\newcounter{IOF}
\setcounter{IOF}{1}
\subsection{Function-\ifnum \value{IOF}<10 0\fi\arabic{IOF}: do this}
\stepcounter{IOF}
\subsection{Function-\ifnum \value{IOF}<10 0\fi\arabic{IOF}: do something else}
\stepcounter{IOF}
\subsection{Function-\DIFnomarkup{\ifnum \value{IOF}<10 0\fi}\arabic{IOF}: do more things}
\stepcounter{IOF}
This is the value of the counter IOF: \DIFnomarkup{\ifnum \value{IOF}<10 0\fi}\arabic{IOF}
\end{document}