16

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.

2
  • 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? Dec 25, 2013 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.
    – Old Nick
    Dec 25, 2013 at 18:41

4 Answers 4

12
\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.

5
  • 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 :)
    – Old Nick
    Dec 25, 2013 at 18:44
  • Original question! Damn!
    – Old Nick
    Dec 25, 2013 at 19:13
  • @dcmst You can actually remove a comment, when you misspelled something and repost it, instead of swearing ;-) Dec 26, 2013 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
    – Old Nick
    Dec 26, 2013 at 15:59
  • I switched the accepted answer because after playing a bit with it I'm very fascinated by the LaTeX 3 approach.
    – Old Nick
    Dec 26, 2013 at 16:00
14

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.

3
  • You don't need to hide ends of lines in the first argument of \ifboolexpr
    – cgnieder
    Dec 24, 2013 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, 2013 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, 2013 at 13:46
4

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}
1
  • 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
    – Old Nick
    Dec 24, 2013 at 12:24
3

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

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.