Patch 2016 xparse to support k-type argument
With the current xparse
release (2017/12/16
), the following will re-add the k
-type argument:
\documentclass{article}
\usepackage{xparse}
\usepackage{xcolor}
\ExplSyntaxOn
\tl_if_exist:NF \c_novalue_tl
{ \tl_const:Nx \c_novalue_tl { \c__xparse_no_value_tl } }
\cs_if_exist:NF \__xparse_single_char_check:n
{ \cs_set_eq:NN \__xparse_single_char_check:n \__xparse_single_token_check:n }
\cs_if_exist:NF \__xparse_add_type_K:w
{
\cs_set_protected:Npn \__xparse_tmp:w #1
{
\cs_new_protected:Npn \__xparse_normalize_type_k:w ##1
{ \__xparse_normalize_type_K:w ##1 {#1} }
}
\exp_args:No \__xparse_tmp:w { \c_novalue_tl }
\cs_new_protected:Npn \__xparse_normalize_type_K:w #1#2
{
\__xparse_single_char_check:n {#1}
\quark_if_recursion_tail_stop_do:nn {#2} { \__xparse_bad_arg_spec:wn }
\tl_put_right:Nn \l__xparse_arg_spec_tl { K #1 {#2} }
\tl_put_right:Nn \l__xparse_last_delimiters_tl {#1}
\bool_set_false:N \l__xparse_grab_expandably_bool
\__xparse_normalize_arg_spec_loop:n
}
\cs_new_protected:Npx \__xparse_add_type_K:w #1#2
{
\exp_not:N \__xparse_flush_m_args:
\exp_not:N \__xparse_add_default:n {#2}
\cs_if_exist:NTF \__xparse_add_grabber:N
{ \exp_not:N \__xparse_add_grabber:N }
{ \exp_not:N \__xparse_add_grabber_optional:N }
K
\tl_put_right:Nn \exp_not:N \l__xparse_signature_tl { #1 }
\exp_not:N \__xparse_prepare_signature:N
}
\cs_new_protected:Npn \__xparse_grab_K:w #1#2 \__xparse_run_code:
{
\__xparse_grab_K:NnNn #1 {#2}
\cs_set_protected_nopar:Npn
{ _ignore_spaces }
}
\cs_new_protected:Npn \__xparse_grab_K_long:w #1#2 \__xparse_run_code:
{
\__xparse_grab_K:NnNn #1 {#2}
\cs_set_protected:Npn
{ _ignore_spaces }
}
\cs_new_protected:Npn \__xparse_grab_K_trailing:w #1#2 \__xparse_run_code:
{
\__xparse_grab_K:NnNn #1 {#2}
\cs_set_protected_nopar:Npn
{ }
}
\cs_new_protected:Npn \__xparse_grab_K_long_trailing:w #1#2 \__xparse_run_code:
{
\__xparse_grab_K:NnNn #1 {#2}
\cs_set_protected:Npn
{ }
}
\cs_new_protected:Npn \__xparse_grab_K:NnNn #1#2#3#4
{
\tl_set:Nn \l__xparse_signature_tl {#2}
\exp_after:wN #3 \l__xparse_fn_tl ##1
{
\__xparse_add_arg:n {##1}
}
\use:c { peek_meaning_remove #4 :NTF } #1
{ \l__xparse_fn_tl }
{
\__xparse_add_arg:o \c_novalue_tl
}
}
}
\ExplSyntaxOff
%
\NewDocumentCommand\MyMacro{k_}{%
\IfNoValueTF{#1}{%
\mathbf{A}%
}{%
A_{\textcolor{red}{#1}}%
}%
}
\begin{document}
With a subscript: $\MyMacro_{\pi}$
Without any subscript: $\MyMacro$
\end{document}
I have raised the transition question with the team, and will either update the answer here on the next release (if required) or arrange for k
-type to have some (limited) transitional support. Note though that this entire area is still very much in flux!
For the simple case where it is used with a single token e
and k
are more or less the same so if you just make \IfNoValue
a little less fussy abut the difference between {-NoValue-}
and -NoValue-
then you can alias k
to e
if it's not already defined so your MWE drops down to:
\documentclass{article}
\usepackage{xparse}
\usepackage{xcolor}
\makeatletter
\ExplSyntaxOn
\ifx\__xparse_normalize_type_k:w\undefined
\def\__xparse_normalize_type_k:w{\__xparse_normalize_type_e:w}
\let\xIfNoValueTF\IfNoValueTF
\def\IfNoValueTF#1{\expandafter\xIfNoValueTF\expandafter{\@car#1.\@nil}}
\fi
\ExplSyntaxOff
\NewDocumentCommand\MyMacro{k_}{%
\IfNoValueTF{#1}{%
\mathbf{A}%
}{%
A_{\textcolor{red}{#1}}%
}%
}
\begin{document}
With a subscript: $\MyMacro_{\pi}$
Without any subscript: $\MyMacro$
\end{document}