是否可以为该命令中的可选参数提供逗号分隔的语法,即\ads{A,1,2,3,4}
或\ads{A}[1,2,3,4]
?(我想我更喜欢第一个变体。)尽管阅读全部 这 相关的 帖子 我 可以 寻找,到目前为止我还没有弄清楚如何应用这里的任何方法。
\documentclass{article}
\usepackage{xparse,amsmath}
% A command to apply adscripts to a base;
% the sequential definition of bases is to avoid spurious vspace from empty over-/under-sets;
% the ordering is anticlockwise from bottom.
\NewDocumentCommand{\ads}{o m o O{} O{} o}{%
\ifx#1t\def\baseA{\text{#2}}\else\def\baseA{#2}\fi% define the base to be text, if desired
\def\baseB{\baseA_{#3}^{#4}}% apply subscript and superscript
\IfValueTF{#3}{\def\baseC{\underset{#3}{\baseB}}}{\def\baseC{\baseB}}% apply underset
\IfValueTF{#6}{\def\baseD{\overset{#6}{\baseC}}}{\def\baseD{\baseC}}% apply overset
\baseD%
}
\begin{document}
\[
\ads[t]{A}[1][2][3][4]
\]
\end{document}
答案1
几乎可以肯定,这个命令可以缩短,但是这个命令写得非常快,而且似乎有效。
\documentclass{article}
\usepackage{amsmath}
\makeatletter
\def\newads@i#1,#2,#3,#4,#5,#6;{\def\myA{#1}\def\myB{#2}\def\myC{#3}\def\myD{#4}\def\myE{#5}}
\newcommand\newads[2][]{%
\expandafter\newads@i#2,,,,,;%
\def\baseB{\text{\myA}_{\myB}^{\myC}}%
\ifx\myD\empty
\def\baseC{\baseB}%
\else
\def\baseC{\underset{\myD}{\baseB}}%
\fi
\ifx\myE\empty
\def\baseD{\baseC}%
\else
\def\baseD{\overset{\myE}{\baseC}}%
\fi
\baseD}
\makeatother
\begin{document}
\[
\newads{A,1,2,3,4}\quad
\newads{A,,2,3,4}\quad
\newads{A,1,,,4}\quad
\newads{A,1,,\mathcal{X},\mathcal{Y}}
\]
\end{document}