# recursive multiple subscript and superscript with xparse

How can I define a new command with xparse, something like this:

\NewDocumentCommand\tensorkor{ m >{\SplitList{,}}o  >{\SplitList{,}}o }
{
?????
}


that i can execute

$\tensorkor{T}{^a,_b,_c,^r,^f}$


which should result in a code like this

${{{{{T^a}_b}_c}^r}^f}$


This is tricky? I dont get it with ProcessList{#1}

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\tensorkor}{m >{\SplitList{,}}O{}}
{\begingroup
\mathsf{#1}%
\newcommand\object{\vphantom{\mathsf{#1}}}%
\ProcessList{#2}{\dotensorkor}%
\endgroup}
\NewDocumentCommand{\dotensorkor}{m}
{%
{\object}#1%
}
\begin{document}
$\tensorkor{T}[^a,_b,_c,^r,^f]$
\end{document}


• Thas perfect :-), but how does it work; Does this not produce? : \mathit{T}\vphantom{\mathsf{T}}^a_b_c^r^f, why does it make brackets arround? ahhh i got it, vphantom is the phantom vertical space between each sub/superscript – Gabriel Apr 30 '13 at 16:12
• @user4514 I'm not sure to understand: the result would be different with just {}. – egreg Mar 14 '14 at 12:03
• @user4514 I still don't understand. – egreg Mar 14 '14 at 15:39
• (corrected the comment, sorry!) The \vphantom does NOT change the super/subscripts position. You would get the same result, if you just used {}. Consequently, if you replace T with something higher, the superscript will be too low (unlike the use of vphantom in your solution would suggest). For the reason and a solution have a look at tex.stackexchange.com/questions/165499/… – user4514 Mar 14 '14 at 17:37
• @user4514 You're wrong and right at the same time. A pair of braces is missing; but an ordinary atom is supposed to be in the \phantom, not \sum or \int. – egreg Mar 14 '14 at 17:45

Without using xparse. (Edited to provide) two versions, depending on whether the user actually wanted subscripts of subscripts (\recursa) or not (\recursb):

\documentclass{article}
\makeatletter
\newcounter{index}
\newcommand\recursa[1]{%
\def\theresult{}%
\getargsC{#1}%
\setcounter{index}{0}%
\whiledo{\value{index} < \narg}{%
\protected@edef\theresult{\bgroup\theresult\csname arg\roman{index}\endcsname\egroup}%
}%
\theresult%
}
\newcommand\recursb[1]{%
\def\theresult{}%
\getargsC{#1}%
\setcounter{index}{0}%
\whiledo{\value{index} < \narg}{%
\protected@edef\theresult{\theresult\csname arg\roman{index}\endcsname{}}%
}%
\theresult%
}
\makeatother
\parindent 0em\parskip 1em
\begin{document}
${{{{{T^a}_b}_c}^r}^f}$
$\recursa{T ^a _b _c ^r ^f}$
$\recursb{T ^a _b _c ^r ^f}$