# Xparse t argument type (underscore with optional parameter)

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 is

An 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}

• Aren't you just trying to input #1 -- which is _ into \textcolor. Actually, your _ must be followed by a second argument, which is \pi here – user31729 Jul 13 '16 at 21:21
• Related Question: Extending xparse for new arguments. – Peter Grill Jul 14 '16 at 6:18

The t argument specifier is a generalization of s that is the same as t*.

I don't think it's the right approach, but if you really want to go this way:

\NewDocumentCommand{\MyMacro}{t_}{A\IfBooleanT{#1}{\MyMacroAux}}
\NewDocumentCommand{\MyMacroAux}{m}{_{\textcolor{red}{#1}}}


Full example:

\documentclass{article}

\usepackage{amsmath}
\usepackage{xparse}
\usepackage{xcolor}

\NewDocumentCommand{\MyMacro}{t_}{A\IfBooleanT{#1}{\MyMacroAux}}
\NewDocumentCommand{\MyMacroAux}{m}{_{\textcolor{red}{#1}}}

\begin{document}

With a subscript: $\MyMacro_{\pi}$

Without any subscript: $\MyMacro$

\end{document}


In case the first part also depends on the presence of _:

\NewDocumentCommand{\MyMacro}{t_}{%
\IfBooleanTF{#1}
{A\MyMacroAux}
{\mathbf{A}}%
}
\NewDocumentCommand{\MyMacroAux}{m}{_{\textcolor{red}{#1}}}

• What you would recommend as the right approach? – Peter Grill Jul 13 '16 at 21:56
• @PeterGrill A command for red subscripts, probably. – egreg Jul 13 '16 at 21:59
• Oh, I see what you mean. The actual use case is a bit more complicated, but agree that if all was trying to was to color the subscripts a specific macro to do that would be the right way. – Peter Grill Jul 13 '16 at 22:01

Looks like an e-type argument to me

\documentclass{article}

\usepackage{amsmath}
\usepackage{xparse}
\usepackage{xcolor}

\NewDocumentCommand\MyMacro{e_}{%
\IfNoValueTF{#1}{%
\mathbf{A}
}{%
A_{\textcolor{red}{#1}}
}%
}

\begin{document}

With a subscript: $\MyMacro_{\pi}$

Without any subscript: $\MyMacro$

\end{document}

• If you are adding % to those lines even in math mode, you are missing two :) I like that k has been implemented. – Manuel Jul 13 '16 at 21:50
• Works great, except that k is marked as experimental so hesitant to use it at this time. – Peter Grill Jul 13 '16 at 21:52
• @PeterGrill Joseph is allowed to experiment with his own code though:-) – David Carlisle Jul 13 '16 at 22:32
• @DavidCarlisle: :-). I just meant it as a reason that this was not the accepted answer. – Peter Grill Jul 13 '16 at 23:52
• @Manuel This type of discussion is the reason the interface is down as experimental – Joseph Wright Jul 14 '16 at 7:28

Is this what is meant?

The t_ check works, but \textcolor{red}{#1} will try to typeset _ then, so {\pi} never enters the subscript.

In my opinion, the {\pi} is an 2nd optional argument. Either use g or the G{} type to grab this.

\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_G{}}{%
\IfBooleanTF{#1}{%
A_{\textcolor{red}{#2}}
}{%
\mathbf{A}
}%
}

\begin{document}

With a subscript: $\MyMacro_{\pi}$

Without any subscript: $\MyMacro$

\end{document}


• Yep that worlds fork the given MW. However, I tend to avoid trailing optional parameters as they have issues if there are extra spaces. Example $\MyMacro _ {\pi}$ fails with the use G. – Peter Grill Jul 13 '16 at 21:56
• @PeterGrill: Well, the given MW was too minimal then, wasn't it? ;-) – user31729 Jul 14 '16 at 13:21
• :-) It was a TMWE. – Peter Grill Jul 14 '16 at 19:41