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.

Is it possible to have something like

\def\fiveorsix{5,6 OR 6,5}

So that the following works:

\documentclass{standalone}

\def\fiveorsix{5,6 OR 6,5}

\makeatletter
\newcommand{\test}[1][]{
    \def\test@a{#1}
    \ifx\test@a\fiveorsix
        the optional argument is 5,6 or 6,5
    \else
        the optional argument is not 5,6 or 6,5
    \fi}
\makeatother

\begin{document}
\test[5,6]
\end{document}

(obviously this does not work)

Or is there a better approach to achieve my goal?

edit: I had to update the MnWE because the first version was actually too minimal.

share|improve this question
    
When you say "the optional argument is 5,6 or 6,5", are those numbers, so that 005,6 should also be recognized as 5,6, or should the optional argument be treated as a bunch or tokens (or characters), so that only exactly 5,6 and 6,5 should be recognized? What about spaces before and after the argument? –  Bruno Le Floch Dec 25 '13 at 18:16
    
@BrunoLeFloch token characters. To make it clear, it could be also dog,cat or cat,dog. Spaces are not relevant, I mean, it's ok if only 5,6 and not 5, 6 works. –  dcmst Dec 25 '13 at 18:41
add comment

4 Answers

up vote 10 down vote accepted
\documentclass{article}
\usepackage{expl3}[2013/07/24]
\ExplSyntaxOn
\newcommand{\test}[1][]{
  \str_case:nnTF {#1}
    {
      { 5,6 } { } % within those braces you could put code specific to the 5,6 case
      { 6,5 } { } % within those braces you could put code specific to the 6,5 case
    }
    { the~argument~is~5,6~or~6,5 }
    { the~argument~is~neither~5,6~nor~6,5 }
}
\ExplSyntaxOff
\begin{document}
\test[123]
\test[5,6]
\test[6,5]
\end{document}

Given the comments on your question, it seems that the optional argument is expected to be a pair of items separated by a comma. You may want to consider using xparse's \SplitArgument, which can split an argument at a given delimiter, with error reporting if the number of delimiters is not as expected. It also removes spaces around each item.

share|improve this answer
    
Hey, thank you. As I said in the original answer I was looking for alternative approaches, The MnWE was there just to explain what I wanted. The use of expl3 is very interesting, I'm gonna play with it :) –  dcmst Dec 25 '13 at 18:44
    
Original question! Damn! –  dcmst Dec 25 '13 at 19:13
    
@dcmst You can actually remove a comment, when you misspelled something and repost it, instead of swearing ;-) –  Henri Menke Dec 26 '13 at 13:40
1  
@HenriMenke You're very right, sorry for the swearing :). It's just that I'm prone to mispelling and the edit timeout doesn't help. My new year resolution for TeX.SE will be to stop posting comments :P –  dcmst Dec 26 '13 at 15:59
    
I switched the accepted answer because after playing a bit with it I'm very fascinated by the LaTeX 3 approach. –  dcmst Dec 26 '13 at 16:00
add comment

You could use the etoolbox (needs e-TeX):

\documentclass{article}

\usepackage{etoolbox}

\newcommand{\test}[1][]{%
    \ifboolexpr{
        test{\ifstrequal{#1}{5,6}} or
        test{\ifstrequal{#1}{6,5}
        }
    }
    {the optional argument is 5,6 or 6,5}
    {the optional argument is not 5,6 or 6,5}}

\begin{document} 
\test[3,2]

\test[5,6]
\end{document}

Results in:

the optional argument is not 5,6 or 6,5
the optional argument is 5,6 or 6,5

You may use other tests like \ifnumcomp. For details look at the etoolbox manual.

share|improve this answer
    
You don't need to hide ends of lines in the first argument of \ifboolexpr –  cgnieder Dec 24 '13 at 13:13
    
And the last if my mobile phone shows the code correctly... sometimes (not too often) a % too much can actually cause problems (think of \ifnum ) –  cgnieder Dec 24 '13 at 13:36
    
@cgnieder thx you are right, I missed that one. and thx for the info about % sign causing problems.(tex.stackexchange.com/questions/34844/…). –  someonr Dec 24 '13 at 13:46
    
thank you very much :) –  dcmst Dec 24 '13 at 13:50
add comment

Not exactly as required, but the idea should be clear:

\documentclass{article}

\begin{document}

\def\fivesix#1{\ifcase#1 the  argument is not 5 or 6
\or the  argument is not 5 or 6
\or the  argument is not 5 or 6
\or the  argument is not 5 or 6
\or the  argument is not 5 or 6
\or the  argument IS 5 or 6
\or the  argument IS 5 or 6
\else  the  argument is not 5 or 6\fi}

\fivesix{1}

\fivesix{5}

\end{document}
share|improve this answer
    
thanks, but I'm afraid my MnWE was too minimal and I can't export this solution to my actual problem, I'll update my question so that it's more like in the real code –  dcmst Dec 24 '13 at 12:24
add comment

Considering that you asked about the definition rather then the comparison, the exact answer to your question should be that it is not possible (in TeX). The macro expansion has to be unique. To achive this you have to specify in the definition under which circumstances the \fiveorsix macro should expand to '5,6' or '6,5'.

As far as you asked about alternate approaches to test whether the macro argument expands to '5,6' or '6,5' - and alredy got a bunch of answers - I add this generic solution for completeness:

\documentclass{article}

\def\fivsix{5,6}
\def\sixfiv{6,5}

\makeatletter
\newcommand{\test}[1][]{%
    \def\test@a{#1}
    \ifnum
      \ifx\test@a\fivsix 1\else\ifx\test@a\sixfiv 1\else 0\fi\fi
      =1
        the optional argument is 5,6 or 6,5
    \else
        the optional argument is not 5,6 and not 6,5
    \fi}
\makeatother

\begin{document}
\test[4,5]\par
\test[5,6]\par
\test[6,5]
\end{document}

orcond

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