Is the ifthen package not recommended to use

conditionalsetoolbox

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.

\documentclass{scrartcl}
%%%%%%%%%%%%% WORKS FOR LUA + XELATEX + PDFLATEX ENGINES
\usepackage{pdftexcmds}
\makeatletter
\let\strcmp\pdf@strcmp
\makeatother
%%%%%%%%%%%%% WORKS ONLY FOR PDFLATEX AND XELATEX ENGINES
%\let\strcmp\pdfstrcmp
%%%%%%%%%%%%% END

\newcommand{\ifstringeqx}[4]{%
    \ifnum\strcmp{#1}{#2}=0 #3\else#4\fi
}
\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}

\edef\isitexpandable{\ifstringeqx{\hello\world}{HiJupiter}{Yes}{No}}

---$>$ \isitexpandable
\end{document}

enter image description here


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 the tokcycle package to perform the \ifnum while removing the influence of \else and \fi:

\makeatletter
\long\def\tctestifnum#1{\tctestifcon{\ifnum#1\relax}}
\long\def\tctestifcon#1{#1\expandafter\tc@exfirst\else\expandafter\tc@exsecond\fi}
\long\def\tc@exfirst#1#2{#1}
\long\def\tc@exsecond#1#2{#2}
\makeatother

\newcommand{\ifstringeqx}[4]{%
    \tctestifnum{\strcmp{#1}{#2}=0}{#3}{#4}}

Here's an example where the original definition with \else and \fi won't work, but this revised definition works fine.

\documentclass{scrartcl}
%%%%%%%%%%%%% WORKS FOR LUA + XELATEX + PDFLATEX ENGINES
\usepackage{pdftexcmds}
\makeatletter
\let\strcmp\pdf@strcmp
\makeatother
%%%%%%%%%%%%% WORKS ONLY FOR PDFLATEX AND XELATEX ENGINES
%\let\strcmp\pdfstrcmp
%%%%%%%%%%%%% END

\makeatletter
\long\def\tctestifnum#1{\tctestifcon{\ifnum#1\relax}}
\long\def\tctestifcon#1{#1\expandafter\tc@exfirst\else\expandafter\tc@exsecond\fi}
\long\def\tc@exfirst#1#2{#1}
\long\def\tc@exsecond#1#2{#2}
\makeatother

\newcommand{\ifstringeqx}[4]{%
    \tctestifnum{\strcmp{#1}{#2}=0}{#3}{#4}}


\def\Yes#1{#1 yes!}
\def\No#1{#1 no!}
\begin{document}
\def\hello{Hi}
\def\world{Earth}

\section{\ifstringeqx{\hello\world}{HiEarth}{\Yes}{\No}{Hell}}

\def\world{Saturn}
\def\planet{Saturn}
\ifstringeqx{\hello\world}{Hi\planet}{Yes}{No}

\edef\isitexpandable{\ifstringeqx{\hello\world}{HiJupiter}{\Yes}{\No}{Absolutely}}

---$>$ \isitexpandable
\end{document}

enter image description here