包中参数的类型e
和描述是什么?有人能给我一个简单的例子来展示它们的用法吗?E
xparse
答案1
!
假设您常用的下标既可以是数学也可以是文本。您可能希望通过选择引入文本下标来简化输入。
为了让示例更有趣,你可以使用不同类型的变量来接受这些类型的下标。我们可以有标量、向量或映射。
\documentclass{article}
\usepackage{amsmath}
\NewDocumentCommand{\variable}{o m e{_!^}}{%
% #1 = style, #2 = variable name,
% #3 = math subscript, #4 = text subscript, #5 = superscript
% typeset the variable according to the style
\IfValueTF{#1}{#1{#2}}{#2}%
% typeset the superscript
\IfValueT{#5}{^{#5}}%
% now fix the subscripts
\IfValueTF{#3}{% there is a math subscript
% typeset it; if there is a textual subscript, add it after a comma
_{#3\IfValueT{#4}{,\mathrm{#4}}}%
}{% no math subscript
\IfValueT{#4}{_{\mathrm{#4}}}%
}%
}
\NewDocumentCommand{\scalar}{}{\variable}
\NewDocumentCommand{\vect}{}{\variable[\mathbf]}
\NewDocumentCommand{\map}{}{\variable[\mathsf]}
\begin{document}
\begin{gather*}
\scalar{a}\ne\vect{v}\ne\map{T} \\
\scalar{a}_{x}+\scalar{a}_{x}^{2} \\
\scalar{a}!{eff}+\scalar{a}!{eff}^{2} \\
\scalar{a}_{x}!{eff}+\scalar{a}_{x}!{eff}^2 \\
\vect{v}_{x}+\vect{v}_{x}^{2} \\
\vect{v}!{eff}+\vect{v}!{eff}^{2} \\
\vect{v}_{x}!{eff}+\vect{v}_{x}!{eff}^2 \\
\map{T}_{x}+\map{T}_{x}^{2} \\
\map{T}!{eff}+\map{T}!{eff}^{2} \\
\map{T}_{x}!{eff}+\map{T}_{x}!{eff}^2 \\
\map{T}!{eff}_{x}
\end{gather*}
\end{document}
最后一行显示输出被规范化为数学下标后的文本部分与输入顺序无关。
现在您意识到即使没有上标,您也想将下标向下移动。类型E
可以解决这个问题。只需更改修饰的顺序并将{}
其作为默认值提供即可^
。
这样做的缺点是,\scriptspace
即使没有任何修饰,也会被添加。我把修复留作练习。
\documentclass{article}
\usepackage{amsmath}
\NewDocumentCommand{\variable}{o m E{^_!}{{}}}{%
% #1 = style, #2 = variable name,
% #3 = math subscript, #4 = text subscript, #5 = superscript
% typeset the variable according to the style
\IfValueTF{#1}{#1{#2}}{#2}%
% typeset the superscript
^{#3}%
% now fix the subscripts
\IfValueTF{#4}{% there is a math subscript
% typeset it; if there is a textual subscript, add it after a comma
_{#4\IfValueT{#5}{,\mathrm{#5}}}%
}{% no math subscript
\IfValueT{#5}{_{\mathrm{#5}}}%
}%
}
\NewDocumentCommand{\scalar}{}{\variable}
\NewDocumentCommand{\vect}{}{\variable[\mathbf]}
\NewDocumentCommand{\map}{}{\variable[\mathsf]}
\begin{document}
\begin{gather*}
\scalar{a}\ne\vect{v}\ne\map{T} \\
\scalar{a}_{x}+\scalar{a}_{x}^{2} \\
\scalar{a}!{eff}+\scalar{a}!{eff}^{2} \\
\scalar{a}_{x}!{eff}+\scalar{a}_{x}!{eff}^2 \\
\vect{v}_{x}+\vect{v}_{x}^{2} \\
\vect{v}!{eff}+\vect{v}!{eff}^{2} \\
\vect{v}_{x}!{eff}+\vect{v}_{x}!{eff}^2 \\
\map{T}_{x}+\map{T}_{x}^{2} \\
\map{T}!{eff}+\map{T}!{eff}^{2} \\
\map{T}_{x}!{eff}+\map{T}_{x}!{eff}^2 \\
\map{T}!{eff}_{x}
\end{gather*}
\end{document}