2

I'm trying to implement a custom command that is called like this:

\mycommand[hello][world]{mandatory1}{mandatory2}

OR:

\mycommand[hello,world]{mandatory1}{mandatory2}

However, I can't figure out how one can check the value of the two options - I want to check if they are really "hello" and "world" and nothing else, not "x" and "y", etc.

The pseudocode would look something like this:

 \NewDocumentCommand{o o m m}{
    if(#1 == "hello"){...} else {...}
    if(#2 == "world"){...} else {...}
}

How can I do this? I already found \IfValueTF{...} in xparse, but I can't check the value of an option with that - only if it was given or not.

  • You want to do a string test: see for example tex.stackexchange.com/questions/43601/… or tex.stackexchange.com/questions/43670/…, or use a classical method \def\@tempa{#1}\def\@tempb{2}\ifx\@tempa\@tempb.... – Joseph Wright Jun 29 '15 at 8:44
  • You want the [..][..] to be mandatory or optional? I'm not sure I understand. What do you need the optional arguments to be by default? You can compare strings with \str_if_eq:nnTF from expl3. – Manuel Jun 29 '15 at 8:44
  • Manuel: The square bracket options should be optional. There is no default value for them, they can either be given or not. – ctitze Jun 29 '15 at 8:52
2

Thanks to the comments to my original question I was able to figure out a solution to my problem. However, I'm not sure if it is really appropriate. It works, but maybe there is a better solution?

\NewDocumentCommand\mycommand{o o m m}{
    \ifthenelse{\equal{#1}{hello}}
        {hello}
        {not hello}
    \ifthenelse{\equal{#2}{world}}
        {world}
        {not world}
}
  • 4
    Another option is with expl3 (which is loaded by xparse). Put \ExplSyntaxOn and \ExplSyntaxOff around that code and use \str_if_eq:nnTF { #1 } { hello } { yes ~ hello } { not ~ hello }. – Manuel Jun 29 '15 at 8:56
2

I use a similar command using xparse for setting languages:

\DeclareRobustCommand{\setlanguage}[1]{%
    \IfEq{#1}{da}{%
         \renewcommand{\contentsname}{Indhold} % TOC Name
         }%
         {}% if not, then do this
}%

In the document, you would use:

\setlanguage{da} % or some other ISO 639-1 code

Also see

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.