# Asymetric behaviour in user command with conditionals

I use frequently a user defined command to highlight modifications in latex documents.

\providecommand{\ifeqthenelse}[4]{\edef\tempa{#1}\def\tempb{#2}\ifx\tempa\tempb {#3} \else {#4}\fi}

\providecommand{\changed}[5]{{\ifeqthenelse{#3}{#5}{{\color{black}#1}}{\color{#4}#1}}%
{\ifeqthenelse{#3}{#5}{}{\color{grey}\ifeqthenelse{#2}{}{}{(}#2\ifeqthenelse{#2}{}{}{)~}}}%
{\ifeqthenelse{#3}{#5}{}{\ifeqthenelse{#3}{}{}{[}{\it\color{#4}#3}\ifeqthenelse{#3}{}{}{]~}}}}

\providecommand{\mychanged}[3]{\changed{#1}{#2}{#3}{blue}{accept}}


The last command mychanged is actually used when making modifications. Its first argument will be the new text and its second argument the old text. Finally the third argument is used by others to accept my modifications by providing accept. Now I find that the behaviour of mychanged is asymmetric in the first two arguments in the following sense: I can use all kinds of complex latex like equations, citations and references in the first argument, while in the second argument citations and references often lead to latex errors. For this reason I always need to modify the old text. This second argument enters the conditionals of the ifeqthenelse command so I suspect that this is why it does not work. Is there a suitable replacement for ifeqthenelse e.g. a better way to construct a if-then-else conditional which will accept even citations etc. in a string comparison? Note that, preferably, I would like to avoid using packages escpecially if they are not platform/distribution independent.

Here is a full example. It works but stops working if you uncomment the second line in the document enviroment.

\documentclass[a4paper,10pt]{article}

\usepackage{color}

\definecolor{blue}{rgb}{0,0,0.5}
\definecolor{grey}{rgb}{0.5,0.5,0.5}

\providecommand{\ifeqthenelse}[4]{\edef\tempa{#1}\def\tempb{#2}\ifx\tempa\tempb {#3} \else {#4}\fi}

\providecommand{\changed}[5]{{\ifeqthenelse{#3}{#5}{{\color{black}#1}}{\color{#4}#1}}%
{\ifeqthenelse{#3}{#5}{}{\color{grey}\ifeqthenelse{#2}{}{}{(}#2\ifeqthenelse{#2}{}{}{)~}}}%
{\ifeqthenelse{#3}{#5}{}{\ifeqthenelse{#3}{}{}{[}{\it\color{#4}#3}\ifeqthenelse{#3}{}{}{]~}}}}

\providecommand{\mychanged}[3]{\changed{#1}{#2}{#3}{blue}{accept}}

\begin{document}

\section{test}

This is a \mychanged{cite \cite{Fukugita:1986hr}}{}{}.
%This is a \mychanged{}{cite \cite{Fukugita:1986hr}}{}.

\bibliographystyle{apsrev}
\begin{thebibliography}{1}
\expandafter\ifx\csname natexlab\endcsname\relax\def\natexlab#1{#1}\fi
\expandafter\ifx\csname bibnamefont\endcsname\relax
\def\bibnamefont#1{#1}\fi
\expandafter\ifx\csname bibfnamefont\endcsname\relax
\def\bibfnamefont#1{#1}\fi
\expandafter\ifx\csname citenamefont\endcsname\relax
\def\citenamefont#1{#1}\fi
\expandafter\ifx\csname url\endcsname\relax
\def\url#1{\texttt{#1}}\fi
\expandafter\ifx\csname urlprefix\endcsname\relax\def\urlprefix{URL }\fi
\providecommand{\bibinfo}[2]{#2}
\providecommand{\eprint}[2][]{\url{#2}}

\bibitem[{\citenamefont{Fukugita and Yanagida}(1986)}]{Fukugita:1986hr}
\bibinfo{author}{\bibfnamefont{M.}~\bibnamefont{Fukugita}} \bibnamefont{and}
\bibinfo{author}{\bibfnamefont{T.}~\bibnamefont{Yanagida}},
\bibinfo{journal}{Phys. Lett. B} \textbf{\bibinfo{volume}{174}},
\bibinfo{pages}{45} (\bibinfo{year}{1986}).
\end{thebibliography}

\end{document}


Ilustration for swallowed spaces in current version (there is no space between "a" and "test" but it would be very nice if there was one)

\documentclass[a4paper,10pt]{article}

\usepackage{color}

\definecolor{blue}{rgb}{0,0,0.5}
\definecolor{grey}{rgb}{0.5,0.5,0.5}

\newcommand{\printifnonempty}[2]{\if\relax\detokenize{#1}\relax\else#2\fi}
\newcommand{\changed}[5]{%
\def\temp{#3}%
\def\accept{#5}%
\ifx\temp\accept
#1
\else
\textcolor{#4}{\printifnonempty{#1}{#1}}%
\textcolor{grey}{\printifnonempty{#2}{(#2)}}%
\textcolor{#4}{\printifnonempty{#3}{[#3]}}%
\fi
}

\begin{document}

\section{Test}

This is a\changed{ test}{ old}{comment}{blue}{accept}.

\end{document}

-
Note, that in the critical cases I always compare with an empty string, so the ifeqthenelse command might be reduced to something simpler in these cases. –  highsciguy Apr 20 '12 at 14:53
The definition is asymmetric: there's \edef for \tempa and \def for \tempb. A real "use case" should be good for suggesting some improvement. –  egreg Apr 20 '12 at 15:21
Yes, but as I stated also the mychanged command uses only the second argument (not the first) in calls of ifeqthenelse. I included a full example in the question –  highsciguy Apr 20 '12 at 16:00
I get no error if I uncomment the second example, after having changed \edef into \def. –  egreg Apr 20 '12 at 16:09

The error is in the \edef that starts the definition of \ifeqthenelse. Change it into \def.

However there's a neater way to do it:

\def\accept{accept}
\newcommand{\mychanged}[3]{%
\def\temp{#3}%
\ifx\temp\accept
#1
\else
\textcolor{blue}{\printifnonempty{#1}{#1}}%
\textcolor{grey}{\printifnonempty{#2}{(#2)}}%
\fi
}
\newcommand{\printifnonempty}[2]{\if\relax\detokenize{#1}\relax\else#2\fi}

-
This is not completely equivalent to the above (wrt. argument #3) but it can be modified in an obvious way. In this context: why is the \def\temp{#3} needed? –  highsciguy Apr 20 '12 at 16:51
@user429540 It's for the following test: we check if #3 is equal to accept. –  egreg Apr 20 '12 at 16:56
Ah I overlooked this because my actual case is slightly different (which was why I needed the \edef). Except for the small difference it is even better than my old solution because it does not add spurious extra white space. –  highsciguy Apr 20 '12 at 17:01
@user429540 Using \edef is quite dangerous: for example \cite doesn't survive it, but also \textbf and many other commands. –  egreg Apr 20 '12 at 17:02
Maybe one more comment. The above swallows leading spaces in the first argument. Is there a way to fix this? –  highsciguy Apr 25 '12 at 17:49