Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

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)

share|improve this answer
add comment

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

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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