只要我不在参数周围#3
和#4
的定义中添加空格,此代码就可以正确编译\vec
。我在文档中找不到有关此内容的任何信息xparse
,我一直以为在这些地方添加空格是 中的首选样式,LaTeX3
所以我想知道为什么会发生这种情况。我有一种感觉,这可能微不足道,但我没有看到它。
我的 MWE (包含@egreg 最初的代码):
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\usepackage{esvect}
% A smarter \vec command.
% Why doesn't it work when I put spaces around #3 or #4?
\RenewDocumentCommand{\vec}{ s m e{_^} }{%
\ensuremath{%
\IfBooleanTF {#1}
{\vv{#2}}
{\boldsymbol{#2}}
_{\IfValueT{#3}{#3}\vphantom{\smash[b]{|}}}
^{\IfValueT{#4}{#4}\vphantom{\smash[t]{\big|}}}
}%
}%
\begin{document}
\[
\vec{E}_{\textup{ball}}
\qquad
\vec{E}_{_{\textup{ball}}}
\]
\[
\vec{p}_{\textup{ball}}
\qquad
\vec{p}_{_{\textup{ball}}}
\qquad
\]
\[
\vec{F}
\qquad
\vec{F}_{\textup{proton}}
\qquad
\vec{F}^{\textup{elec}}
\qquad
\vec{F}^{\textup{elec}}_{\textup{proton}}
\qquad
\vec{F}_{\textup{proton}}^{\textup{elec}}
\]
\[
\vec{F}
\qquad
\vec{F}_{21}
\qquad
\vec{F}_{21}^{\textup{contact}}
\qquad
\vec{F}_{\textup{table on ball}}^{\textup{contact}}
\qquad
\vec{F}^{\textup{contact}}_{\textup{table on ball}}
\]
\end{document}
以下是没有相关空格的结果:
以下是包含相关空格的结果:
答案1
(继续此评论)
一个丑陋的解决办法:
- 标记化
e{^_}
之前\ExplSyntaxOn
,为了冻结 catcode_
,和 - 用其命令形式替换
_
(必需)和(仅用于保持一致) (su^
\sb
b-script) 和\sp
(su页er-script)分别用于将 JavaScript 脚本与 JavaScript 对象脚本 (.er-script) 结合使用。
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\usepackage{esvect}
% A smarter \vec command.
% Why doesn't it work when I put spaces around #3 or #4?
\def\temp{ \RenewDocumentCommand{\vec}{ s m e{_^} } }
\ExplSyntaxOn
\temp{%
\ensuremath{%
\IfBooleanTF {#1}
{\vv{#2}}
{\boldsymbol{#2}}
\sb{\IfValueT{ #3 }{#3}\vphantom{\smash[b]{|}}}
\sp{\IfValueT{ #4 }{#4}\vphantom{\smash[t]{\big|}}}
}%
}%
\ExplSyntaxOff
\begin{document}
\[
\vec{E}_{\textup{ball}}
\qquad
\vec{E}_{_{\textup{ball}}}
\]
\[
\vec{p}_{\textup{ball}}
\qquad
\vec{p}_{_{\textup{ball}}}
\qquad
\]
\[
\vec{F}
\qquad
\vec{F}_{\textup{proton}}
\qquad
\vec{F}^{\textup{elec}}
\qquad
\vec{F}^{\textup{elec}}_{\textup{proton}}
\qquad
\vec{F}_{\textup{proton}}^{\textup{elec}}
\]
\[
\vec{F}
\qquad
\vec{F}_{21}
\qquad
\vec{F}_{21}^{\textup{contact}}
\qquad
\vec{F}_{\textup{table on ball}}^{\textup{contact}}
\qquad
\vec{F}^{\textup{contact}}_{\textup{table on ball}}
\]
\end{document}