0

I want to pass an argument string to \NewDocumentCommand that allows me to apply different code expressions based on the specified option.

For instance, consider a command named product with an argument called type , where the latter can be either "direct" (in such case printing $#2 \times #3$) or "tensor" (in such printing $#2 \otimes #3$).

Karl
  • 1
  • 1
    Welcome! Can you please provide a minimal but complete example people can compile to help you? I don't understand what you mean by 'an argument called type'. Do you just want \NewDocumentCommand {mmm} and then test if #1 is direct or tensor? – cfr Oct 17 '23 at 01:31

2 Answers2

1

You can use the xstring package to do such conditions:

The \Product defined below takes an option for parameter (defaults to "direct" if not specified. You need to decide how to handle the error condition.

enter image description here

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xstring}

\NewDocumentCommand{\Product}{O{direct} m m}{% \IfStrEqCase{#1}{% {direct}{#2 \times #3}% {tensor}{#2 \otimes #3}% }[% \text{Unsupported case: #1.}% ]% }

\begin{document} $\Product{a}{b}$

$\Product[direct]{a}{b}$

$\Product[tensor]{a}{b}$

$\Product[wacky]{a}{b}$ \end{document}

Peter Grill
  • 223,288
0

The best option is to use l3 layer for this. texdoc interface3 to get the user guide.

Here is an example.

\documentclass{article}

\begin{document} \ExplSyntaxOn \cs_new:Npn \l_my_command:n #1 {

\tl_set:Nn \l_tmpa_tl{#1}
\str_case_e:nnTF {\l_tmpa_tl}  
{

           { none       } {               } 
           { rmfamily   } {  \rmfamily    } 
           { serif      } {   \rmfamily   } 
           { sans-serif } {  \sffamily    } 
           { sans       } {  \sffamily    }
           { sffamily   } {  \sffamily    }
           { ttfamily   } {  \ttfamily    } 

           { mono       } {  \ttfamily    } 

}
{ TRUE~ } { FALSE~ }

}

\l_my_command:n {mono } some~text\par

\l_my_command:n {rmfamily} some~text

\NewDocumentCommand{\MyCommand}{ m } { \l_my_command:n{#1}
}
\ExplSyntaxOff

\MyCommand{mono} Some other text

\end{document}

yannisl
  • 117,160