With expl3
- We check if there's
_
inside the argument of \des
;
- if there is no
_
inside, we just append _{\textnormal{des}}
to the argument of \des
; and
- if there's indeed at least one
_
, we substitute it by a macro (\riccardo_dessb:n
) which in turn, when expanded, takes one argument (the original subscript) and transforms into _{<original subscript>,\textnormal{des}}
(basically _{<whatever>}
gets transformed to _{<whatever>,\textnormal{des}}
).
Here's the proper code
\documentclass{scrartcl}
\usepackage{xparse,mathtools}
\NewDocumentCommand\des{m}{\csname riccardo_des:nn\endcsname{#1}{_}}
\ExplSyntaxOn
\cs_new_protected:Npn \riccardo_des:nn #1 #2
{
\tl_set:Nn \l_tmpa_tl { #1 }
\tl_if_in:NnTF \l_tmpa_tl { #2 }
{ \tl_replace_all:Nnn \l_tmpa_tl { #2 } { \riccardo_dessb:n } }
{ \tl_put_right:Nn \l_tmpa_tl { \sb { \textnormal{des} } } }
\tl_use:N \l_tmpa_tl
}
\cs_new_protected:Npn \riccardo_dessb:n #1
{ \sb { #1 , \textnormal{des} } }
\ExplSyntaxOff
\begin{document}
$\des{a}$ and $\des{a_{b}^{c}}$
\end{document}

Addition
If you prefer a clear syntax like \des{a}_{b}^{c}
rather than \des{a_{b}^{c}}
, here's a solution using some code I posted a few weeks ago. It may look cumbersome, but the actual definition is much shorter.
\documentclass{scrartcl}
\usepackage{xparse,mathtools}
% begin addition for new arguments
\ExplSyntaxOn
\cs_new_protected:Npn \__xparse_count_type_k:w #1
{
\__xparse_single_token_check:n { #1 }
\quark_if_recursion_tail_stop_do:Nn #1 { \__xparse_bad_arg_spec:wn }
\__xparse_count_mandatory:N
}
\cs_new_protected:Npn \__xparse_count_type_K:w #1 #2
{
\__xparse_single_token_check:n { #1 }
\quark_if_recursion_tail_stop_do:nn { #2 } { \__xparse_bad_arg_spec:wn }
\__xparse_count_mandatory:N
}
\cs_new_protected:Npn \__xparse_add_type_k:w #1
{ \exp_args:NNo \__xparse_add_type_K:w #1 { \c__xparse_no_value_tl } }
\cs_new_protected:Npn \__xparse_add_type_K:w #1 #2
{
\__xparse_flush_m_args:
\__xparse_add_grabber_optional:N K
\tl_put_right:Nn \l__xparse_signature_tl { #1 { #2 } }
\__xparse_prepare_signature:N
}
\cs_new_protected:Npn \__xparse_add_expandable_type_k:w #1
{
\exp_args:NNo \__xparse_add_expandable_type_K:w #1 { \c__xparse_no_value_tl }
}
\cs_new_protected_nopar:Npn \__xparse_add_expandable_type_K:w #1 #2
{
\__msg_kernel_error:nnx { xparse } { invalid-expandable-argument-type } { K }
\__xparse_add_expandable_type_m:w % May be create this?
}
\cs_new_protected:Npn \__xparse_grab_K:w #1 #2 #3 \l__xparse_args_tl
{
\__xparse_grab_K_aux:NnnNn #1 { #2 } { #3 } \cs_set_protected_nopar:Npn
{ _ignore_spaces }
}
\cs_new_protected:Npn \__xparse_grab_K_long:w #1 #2 #3 \l__xparse_args_tl
{
\__xparse_grab_K_aux:NnnNn #1 { #2 } { #3 } \cs_set_protected:Npn
{ _ignore_spaces }
}
\cs_new_protected:Npn \__xparse_grab_K_trailing:w #1 #2 #3 \l__xparse_args_tl
{
\__xparse_grab_K_aux:NnnNn #1 { #2 } { #3 } \cs_set_protected_nopar:Npn
{ _ignore_spaces }
}
\cs_new_protected:Npn \__xparse_grab_K_long_trailing:w #1 #2 #3 \l__xparse_args_tl
{
\__xparse_grab_K_aux:NnnNn #1 { #2 } { #3 } \cs_set_protected:Npn
{ _ignore_spaces }
}
\cs_new_protected:Npn \__xparse_grab_K_aux:NnnNn #1 #2 #3 #4 #5
{
\exp_after:wN #4 \l__xparse_fn_tl ##1
{
\__xparse_add_arg:n { ##1 }
#3 \l__xparse_args_tl
}
\use:c { peek_meaning_remove #5 :NTF } #1
{ \l__xparse_fn_tl }
{
\__xparse_add_arg:n { #2 }
#3 \l__xparse_args_tl
}
}
\prop_put:Nnn \c__xparse_shorthands_prop { a } { k \sb }
\prop_put:Nnn \c__xparse_shorthands_prop { b } { k \sp }
\prop_put:Nnn \c__xparse_shorthands_prop { A } { K \sb }
\prop_put:Nnn \c__xparse_shorthands_prop { B } { K \sp }
\ExplSyntaxOff
% end
\NewDocumentCommand\des{ma}
{#1\IfValueTF{#2}{_{#2,\textnormal{des}}}{_{\textnormal{des}}}}
\begin{document}
$\des{a}$ and $\des{a}_{b}^{c}$
\end{document}