4

In this post, a solution is given to the problem of defining a command which tests equality of two strings, sufficiently robust (in comparison to \ifthenelse) to admit complicated arguments (e.g. containing commands or environments). But now, i would like to adapt it to perform a test on 1 argument in the form "IF arg= a OR arg=b THEN ... ELSE ...", where "a" and "b" or fixed. My attempt is :

\makeatletter
\newcommand{\testOR}[1]{%
  \ifnum\pdfstrcmp{\unexpanded{#1}}{a}=\z@
    \expandafter\@secondoftwo
  \else
    \ifnum\pdfstrcmp{\unexpanded{#1}}{b}=\z@
            \expandafter\@secondoftwo%
    \else%
            \expandafter\@firstoftwo%
    \fi%
  \fi
}
\makeatother
\begin{document}

\testIfNotTrivial{a}{true}{false} - 
\testIfNotTrivial{b}{true}{false} - 
\testIfNotTrivial{coucou}{true}{false}

\end{document}

But it does not have the expected (from me) behaviour : false - False - true

7

Without a MWE this is untested but in your second branch the\expandafter only gets rid of one \fi you still have the outer \fi left so looks like you would want

\ifnum\pdfstrcmp{\unexpanded{#1}}{b}=\z@
        \expandafter\expandafter\expandafter\@secondoftwo
\else
        \expandafter\expandafter\expandafter\@firstoftwo
\fi

(no need for % after a command name)

6

You really should give LaTeX3 a try, if you have many tests like this:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\testOR}{m}
  {
   \bool_if:nTF
     {
      \str_if_eq_p:nn { #1 } { a } || \str_if_eq_p:nn { #1 } { b }
     }
  }
\ExplSyntaxOff
\begin{document}
\testOR{a}{true}{false} --
\testOR{b}{true}{false} --
\testOR{coucou}{true}{false} --
\testOR{\textbf{coucou}}{true}{false}
\end{document}

This gives

true – true – false – false

  • Ok, maybe you are right. I'm just afraid of learning new syntaxes... – nicolas roy Feb 7 '12 at 13:30
  • Where could i find an introduction to these latex3 commands ? – nicolas roy Feb 9 '12 at 8:47

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.