I am not understanding how to use the t
type parameter with xparse
's \NewDocumentCommand
.
I am trying to define a macro that accepts one optional parameter, but the optional parameter is specified as a subscript using an underscore. That is I want to define something that behaves like
\def\MyMacro_#1{A_{\textcolor{red}{#1}}}
but with the subscript being optional. I need #1
to be parameter not simply a subscript added outside of the macro (i.e., \MyMacro
must be the one that invokes the actual subscript)
The MWE below yields:
but the desired result is:
with the color and subscript applied by \MyMacro
.
Notes:
The documentation states that
t
isAn optional ⟨token⟩, which will result in a value
\BooleanTrue
if ⟨token⟩ is present and\BooleanFalse
otherwise. Given as t⟨token⟩.but no examples are given.
Code:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\usepackage{xcolor}
%\newcommand*{\MyMacro}[1][]{}% Ensure we are not overwriting anything
%\def\MyMacro_#1{A_{\testcolor{red}{#1}}}% <-- Want this behavior but with the _{#1} being optional
\NewDocumentCommand{\MyMacro}{t_}{%
\IfBooleanTF{#1}{%
A_{\textcolor{red}{#1}}
}{%
\mathbf{A}
}%
}
\begin{document}
With a subscript: $\MyMacro_{\pi}$
Without any subscript: $\MyMacro$
\end{document}
#1
-- which is_
into\textcolor
. Actually, your_
must be followed by a second argument, which is\pi
here