2

In this example:

\documentclass{article}
\usepackage{ifthen}

\newcommand{\objtitle}[1]{%                                                                                                                   
\ifthenelse{\equal{#1}{a}}{avalue}{}%                                                                                                         
\ifthenelse{\equal{#1}{b}}{bvalue}{}%                                                                                                         
}

\newcommand{\counterpart}[1]{%                                                                                                                
\ifthenelse{\equal{#1}{a}}{b}{}%                                                                                                              
\ifthenelse{\equal{#1}{b}}{a}{}%                                                                                                              
}
\begin{document}
\counterpart{a}
\objtitle{a}
\objtitle{\counterpart{a}}
\end{document}

\counterpart{a} and \objtitle{a} work separately but \objtitle{\counterpart{a}} produces and error:

! Argument of \equal has an extra }.
<inserted text> 
                \par 
l.17 \objtitle{\counterpart{a}}

Using \objtitle{\protect\counterpart{a}} eliminates the error message but produces no output.

What is the problem and how to solve?

4
  • 1
    \ifthenelse is (very) fragile and can not be used in the argument of \equal Oct 11 '18 at 19:41
  • @DavidCarlisle using \protect produces no output. Is there a solution?
    – Viesturs
    Oct 11 '18 at 19:45
  • 2
    yes \protect makes it safe but stops it executing at all so it will not be equal to anything. You do not want to use \ifthenelse at all, but you havn't given any description of what you actually want to do so not sure what to suggest. Oct 11 '18 at 19:56
  • 1
    related tex.stackexchange.com/questions/451960/…
    – user4686
    Oct 11 '18 at 21:33
2

\ifthenelse cannot be used nested. No way. Sorry, it's impossible.

If you want to do nestable case switching, you need to use more advanced tools.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\caseswitch}{O{}mm}
 {
  \str_case_e:nnF { #2 } { #3 } { #1 }
 }
\ExplSyntaxOff

\newcommand{\objtitle}[1]{%
  \caseswitch{#1}{
   {a}{avalue}
   {b}{bvalue}
  }%
}

\newcommand{\counterpart}[1]{%
  \caseswitch{#1}{
    {a}{b}
    {b}{a}
  }%
}

\begin{document}

\counterpart{a}

\objtitle{a}

\objtitle{\counterpart{a}}

\end{document}

You can also call \caseswitch with an optional argument; thus

\caseswitch[none of the above]{#1}{
   {a}{avalue}
   {b}{bvalue}
  }

would return “none of the above” if #1 is neither a nor b. With no optional argument, no match returns nothing.

The number of cases is arbitrary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.