I've seen several posts and users recommend avoiding the ifthen
package, however, I see it was recently updated (2020).
One feature I'm trying to exploit from it is an expanded string comparison. etoolbox
offers unexpanded string comparison via \ifstrequal
(I suppose the intention is to use this in macros to check argument strings?), or it can expand a string in the first argument (only one expansion?) via \ifdefstring
. My needs would require full expansion, say for concatenating two definitions. Please see MWE below
\documentclass{scrartcl}
\usepackage{ifthen}
\usepackage{etoolbox}
% \newcommand{\ifstringeqx}[4]{% will not work in section here, use robust cmd
\newrobustcmd{\ifstringeqx}[4]{%
\ifthenelse%
{\equal{#1}{#2}}%
{#3}%
{#4}%
}
\begin{document}
\def\hello{Hi}
\def\world{Earth}
\section{\ifstringeqx{\hello\world}{HiEarth}{Yes}{No}}
\def\world{Saturn}
\def\planet{Saturn}
\ifstringeqx{\hello\world}{Hi\planet}{Yes}{No}
\ifstringeqx{\hello\world}{HiJupiter}{Yes}{No}
\end{document}
10 year old post: Why is the ifthen package obsolete?
Best Answer
David's suggestion satisfies all the requirements.
\pdfstrcmp
expands its arguments before comparison and is itself expandable.Note the implementation is engine specific and so an extra package needs loading if lualatex is used.
SUPPLEMENT
If one is concerned with the
\else
and/or\fi
in the definition of\ifstringeqx
getting in the way of#3
and#4
doing their thing (for example, if they need to absorb tokens located beyond the\fi
), we can copy/paste a little bit of logic from thetokcycle
package to perform the\ifnum
while removing the influence of\else
and\fi
:Here's an example where the original definition with
\else
and\fi
won't work, but this revised definition works fine.