如何使用 xparse 定义一个新命令,如下所示:
\NewDocumentCommand\tensorkor{ m >{\SplitList{,}}o >{\SplitList{,}}o }
{
?????
}
我可以执行
$ \tensorkor{T}{^a,_b,_c,^r,^f} $
这应该导致像这样的代码
$ {{{{{T^a}_b}_c}^r}^f} $
这很棘手?我不明白ProcessList{#1}
答案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}
答案2
不使用 xparse。(已编辑以提供)两个版本,取决于用户是否实际想要下标的下标(\recursa
)或不想要(\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}