10

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}

2 Answers 2

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

enter image description here

7
  • 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, 2013 at 16:12
  • @user4514 I'm not sure to understand: the result would be different with just {}.
    – egreg
    Mar 14, 2014 at 12:03
  • @user4514 I still don't understand.
    – egreg
    Mar 14, 2014 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, 2014 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, 2014 at 17:45
7

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

\documentclass{article}
\usepackage{readarray}
\makeatletter
\newcounter{index}
\newcommand\recursa[1]{%
  \def\theresult{}%
  \getargsC{#1}%
  \setcounter{index}{0}%
  \whiledo{\value{index} < \narg}{%
    \addtocounter{index}{1}%
    \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}{%
    \addtocounter{index}{1}%
    \protected@edef\theresult{\theresult\csname arg\roman{index}\endcsname{}}%
  }%
\theresult%
}
\makeatother
\parindent 0em\parskip 1em
\begin{document}
What the user asked for:\\
$  {{{{{T^a}_b}_c}^r}^f} $

Producing what the user asked for:\\
$\recursa{T ^a _b _c ^r ^f}$

Perhaps this is what the user really wanted:\\
$\recursb{T ^a _b _c ^r ^f}$

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .