4

In latexdiff, the default is to protect citation commands with an \mbox so that they are printed properly in the default style. How can I add other commands to be protected by an \mbox, such as \SI{}{} or \cref{}?

new.tex

% arara: pdflatexmk
\documentclass{article}
\usepackage{siunitx}
\begin{document}
This is the new text \SI{300}{\meter\per\second}
\end{document}

old.tex

% arara: pdflatexmk
\documentclass{article}
\usepackage{siunitx}
\begin{document}
This is the old text \SI{600}{\meter\per\second}
\end{document}

Output of latexdiff --append-safecmd="SI" (compiling fails, but adding an \mbox around the siunitx commands succeeds):

% arara: pdflatexmk
%DIF LATEXDIFF DIFFERENCE FILE
%DIF DEL test_old.tex   Sun Dec 21 11:40:33 2014
%DIF ADD test.tex       Sun Dec 21 11:40:38 2014
\documentclass{article}
\usepackage{siunitx}
%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}
This is the \DIFdelbegin \DIFdel{old text \SI{600}{\meter\per\second}
 }\DIFdelend \DIFaddbegin \DIFadd{new text \SI{300}{\meter\per\second}
 }\DIFaddend\end{document}

3 Answers 3

5

Other commands are treated equivalently to the citation commands if defined in the following option:

--append-mboxsafecmd="cmd1,cmd2,..."

(requires latexdiff version 1.1.0 or higher). If the siunitx package is detected, this is done automatically for \SI.

1
  • Wow thank you for the update and considering this functionality in your program!
    – darthbith
    Commented Apr 15, 2015 at 16:20
4

There is no easy way to do this. The citation commands are currently (1.0.4) hard-coded. You would have to modify the source code of latexdiff to include \cref in this list. Look for the following lines in the code:

if ( defined $packages{"apacite"}  ) {
  print STDERR "DEBUG apacite citation commands\n"  if $debug;
  $citpatsafe=qr/^(?:mask)?(?:full|short)?cite(?:A|author|year)?(?:NP)?$/;
  $citpat='(?:mask)?(?:full|short|no)?cite(?:A|author|year|meta)?(?:NP)?';
} else {
  # citation command pattern for all other citation schemes
  $citpatsafe=qr/^cite.*$/;
  $citpat='(?:cite\w*|nocite)';
};

and change the lines in the else clause to

  $citpatsafe=qr/^(?:cite.*|cref)$/;
  $citpat='(?:cite\w*|nocite|cref)';

(untested, but should work). For \SI, this will not work, as the algorithm expects the 'citation' commands to have exactly one non-optional argument.

The problem with the citation commands arises from an incompatibility with the ulem package, and maybe the same is true for siunitx. As a work-around I could suggest trying a different markup style (e.g. CFONT, option -t CFONT) and then defining \SI,\cref as safe commands.

As an extremely dirty work-around, use this one-liner to post-process the latexdiff output before feeding it to latex/pdflatex to protect the \SI commands:

perl -pne 's/\\SI\{(.*?)}{(.*?)\}/\\mbox{\\SI{$1}{$2}}/g' file-diff.tex > file-diff-mod.tex

Note that this will surround all instances of \SI with an \mbox command, including those outside added or deleted blocks. Also, this will result in errors if the argument to any \SI command is nested, i.e. contains curly braces, or if there are spaces or newlines between the arguments of SI

1
  • For newer versions of latexdiff (1.1.0+) this answer is now superseded (see my other answer)
    – frederik
    Commented Apr 15, 2015 at 12:21
4

You can address this at the LaTeX end with something of a hack

\makeatletter
\AtBeginDocument{
  \let\UL@SI\SI
  \renewcommand\SI[3][]{\mbox{\UL@SI[#1]{#2}{#3}}}
}
\makeatother

which will work for most 'simple' cases.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .