2

I would like to use latexdiff on a document where I have changed the label of my figures. Is there a way to ignore changes of the commands ref and label? Here is an example:

old_version.tex

\documentclass{article}

\begin{document}

\begin{figure}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\ref{fig:my_label}

\end{document}

new_version.tex

\documentclass{article}

\begin{document}

\begin{figure}
    \caption{Caption}
    \label{fig:my_new_label}
\end{figure}

\ref{fig:my_new_label}

\end{document}

In both cases, \ref{...} prints 1, i.e., there is no change in the output. Can I configure latexdiff to reflect this?

I tried latexdiff --exclude-textcmd="ref" old_version.tex new_version.tex and it still highlights the changed label as a change:

\documentclass{article}
%DIF LATEXDIFF DIFFERENCE FILE
%DIF DEL old_version.tex   Sun Jan  5 05:57:46 2020
%DIF ADD new_version.tex   Sun Jan  5 05:58:01 2020
%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
\providecommand{\DIFmodbegin}{} %DIF PREAMBLE
\providecommand{\DIFmodend}{} %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 LISTINGS PREAMBLE %DIF PREAMBLE
\RequirePackage{listings} %DIF PREAMBLE
\RequirePackage{color} %DIF PREAMBLE
\lstdefinelanguage{DIFcode}{ %DIF PREAMBLE
%DIF DIFCODE_UNDERLINE %DIF PREAMBLE
  moredelim=[il][\color{red}\sout]{\%DIF\ <\ }, %DIF PREAMBLE
  moredelim=[il][\color{blue}\uwave]{\%DIF\ >\ } %DIF PREAMBLE
} %DIF PREAMBLE
\lstdefinestyle{DIFverbatimstyle}{ %DIF PREAMBLE
        language=DIFcode, %DIF PREAMBLE
        basicstyle=\ttfamily, %DIF PREAMBLE
        columns=fullflexible, %DIF PREAMBLE
        keepspaces=true %DIF PREAMBLE
} %DIF PREAMBLE
\lstnewenvironment{DIFverbatim}{\lstset{style=DIFverbatimstyle}}{} %DIF PREAMBLE
\lstnewenvironment{DIFverbatim*}{\lstset{style=DIFverbatimstyle,showspaces=true}}{} %DIF PREAMBLE
%DIF END PREAMBLE EXTENSION ADDED BY LATEXDIFF

\begin{document}

\begin{figure}
    \caption{Caption}
    \DIFdelbeginFL %DIFDELCMD < \label{fig:my_label}
%DIFDELCMD < %%%
\DIFdelendFL \DIFaddbeginFL \label{fig:my_new_label}
\DIFaddendFL \end{figure}

\DIFdelbegin \DIFdel{\ref{fig:my_label}
}\DIFdelend \DIFaddbegin \DIFadd{\ref{fig:my_new_label}
}\DIFaddend 

\end{document}
Felix
  • 151

2 Answers2

4

You have to use option --exclude-safecmd=ref, not --exclude-textcmd. textcmd is for commands whose argument is text. Note that you can use --show-all to get a listing of the default settings for safe and text commands.

frederik
  • 1,405
  • This results in \DIFdelbegin \n%DIFDELCMD < \ref{fig:my_label}\n %DIFDELCMD < %%%\n \DIFdelend \DIFaddbegin \ref{fig:my_new_label}\n \DIFaddend (added \n to show positions of newlines). Can I get rid of the added macros entirely? – Felix Jan 06 '20 at 21:40
  • But with most markup styles, including the one chosen by you, all this markup is invisible in the output file (e.g., pdf). I am afraid there is no simple way to suppress this. Is hiding it in the output not enough? – frederik Jan 06 '20 at 21:49
  • It produces an extra spaces after the \ref{} command. But you are right, this is a minor issue. I'll accept your answer. – Felix Jan 06 '20 at 21:57
0

Not a solution, but a workaround for my case: I went to kate and used the following pattern to replace every occurrence of replaced figure references with the reference to the new figure label.

Find: \\DIFdelbegin[ \t\n\r]*\\DIFdel\{[ \t\n\r]*\\ref\{fig:[a-zA-Z0-9_\:\-]*\}[ \t\n\r]*\}[ \t\n\r]*\\DIFdelend[ \t\n\r]*\\DIFaddbegin[ \t\n\r]*\\DIFadd\{[ \t\n\r]*\\ref\{fig:([a-zA-Z0-9_\:\-]*)\}[ \t\n\r]*\}[ \t\n\r]*\\DIFaddend

Replace: \\ref{fig:\1}

(Note the final space in the find-pattern).

All the [ \t\n\r]* are there because latexdiff produces line breaks at random locations and at least my version of kate does not match newlines with \s*.

Felix
  • 151