Xparse's new e-type argument (replacement for k-type argument)

The k argument type is not available any longer.

There is a big difference between the old k and the new e type. With

e{ABC}

(here A, B and C represent any three distinct tokens) the macro will look for any sequence of tokens in the form A{x}B{y}C{z}, but the order is arbitrary, so

A{x}B{y}C{z}
B{y}A{x}C{z}
C{z}B{y}A{x}

will result in the same token list to be passed in the macro replacement text as #<n>. Repeating one of the stated tokens will result in stopping the search for the next, so

A{x}A{Ouch}B{y}C{z}

will be the same as passing just A with argument {x} and nothing for the other two tokens.

The argument is normalized with respect to the order and #<n> will represent a sequence of braced token lists, with -NoValue- for the missing ones. For instance

\NewDocumentCommand{\test}{ e{ABC} }{%
  \showtokens{#1}%
}

with the input

\test
\test A{x}
\test A{x}B{y}C{z}
\test B{y}C{z}A{x}
\test B{y}A{x}C{z}
\test A{x}B{y}
\test C{z}

will result in showing

> {-NoValue-}{-NoValue-}{-NoValue-}.
> {x}{-NoValue-}{-NoValue-}.
> {x}{y}{z}.
> {x}{y}{z}.
> {x}{y}{z}.
> {x}{y}{-NoValue-}.
> {-NoValue-}{-NoValue-}{z}.

The consequence is that you should pass #1 to another macro for subsequent processing, even if there is only one embellishment prefix token; such a macro must have the required number of mandatory arguments.

\documentclass{article}

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

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

\begin{document}

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

Without any subscript: $\MyMacro$

\end{document}

Note that no braces should surround #1 in the code for \MyMacro. This is the normal procedure for “processed arguments”.

Update June 2017

The newer versions of xparse made a breaking change related to the e and E argument types. Now e<tokens> returns one argument in the replacement text for each token in <tokens>, so the code for this case must become

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

The argument(s) will contain -NoValue- (testable with \IfNoValueTF) if the corresponding token is not found.