![用 expl3 替换 \xspace](https://linux22.com/image/464505/%E7%94%A8%20expl3%20%E6%9B%BF%E6%8D%A2%20%5Cxspace.png)
我已经多次听说它\xspace
不应该被使用并且不可靠。
为了找到更好的解决方案,我找到xpeek
并尝试使用文档中给出的示例。
\documentclass{scrlttr2}
\usepackage{fmtcount}
\usepackage{xpeek}
\ExplSyntaxOn
\tl_const:Nn \c_xsp_exceptions_tl { ,;:.!? }
\NewDocumentCommand \xsp {}
{
\xpeek_collect_do:nn \c_empty_tl
{
\xpeek_if_in:NNTF \c_xsp_exceptions_tl \l_peek_token
{ } { ~ }
}
}
\NewDocumentCommand{\numbertext}{m}
{
\int_compare:nNnTF {#1} > {12}
{ % true
#1\xsp
}
{ % false
\numberstringnum{#1}\xsp
}
}
\NewDocumentCommand{\numbertextnoxsp}{m}
{
\int_compare:nNnTF {#1} > {12}
{ % true
#1
}
{ % false
\numberstringnum{#1}
}
}
\NewDocumentCommand{\numbertextnoxspunits}{m}
{
\numbertextnoxsp{#1} units
}
\ExplSyntaxOff
\begin{document}
numberstring:
\numberstringnum{13} units. \numberstringnum{13}.
\bigskip
numbertext:
\numbertext{12}units. \numbertext{12}.
\numbertext{12} units. \numbertext{12}.
\numbertext{13}units. \numbertext{13}.
\numbertext{13} units. \numbertext{13}.
\bigskip
numbertextnoxsp:
\numbertextnoxsp{12}units. \numbertextnoxsp{12}.
\numbertextnoxsp{12} units. \numbertextnoxsp{12}.
\numbertextnoxsp{13}units. \numbertextnoxsp{13}.
\numbertextnoxsp{13} units. \numbertextnoxsp{13}.
\bigskip
numbertextnoxspunits:
\numbertextnoxspunits{12}. \numbertextnoxspunits{12} it is.
\numbertextnoxspunits{12}. \numbertextnoxspunits{12} it is.
\numbertextnoxspunits{13}. \numbertextnoxspunits{13} it is.
\numbertextnoxspunits{13}. \numbertextnoxspunits{13} it is.
\end{document}
然而,这似乎也不起作用,因为最后两行显示了不同的结果,而且我也不指望它们......
输出为:
答案1
没有任何理由\xspace
在带有参数的命令末尾使用或其变体。
在没有参数的情况下执行命令也没有什么理由\xspace
,但那是另一个游戏。
\documentclass{article}
\usepackage{fmtcount}
\ExplSyntaxOn
\NewDocumentCommand{\numbertext}{m}
{
\int_compare:nNnTF {#1} > {12}
{ % true
#1
}
{ % false
\numberstringnum{#1}
}
}
\ExplSyntaxOff
\begin{document}
numberstring:
\numberstringnum{13} units. \numberstringnum{13}.
\bigskip
numbertext:
\numbertext{12}units. \numbertext{12}.
\numbertext{12} units. \numbertext{12}.
\numbertext{13}units. \numbertext{13}.
\numbertext{13} units. \numbertext{13}.
\end{document}
难道有什么意外的事情发生?
你补充说
\NewDocumentCommand{\numbertextnoxspunits}{m}
{
\numbertextnoxsp{#1} units
}
并抱怨输出中数字后面没有空格。定义中没有空格,因为内部\ExplSyntaxOn
空格被忽略了。你想要
\NewDocumentCommand{\numbertextnoxspunits}{m}
{
\numbertextnoxsp{#1}~units
}
因为在那种情况下~
代表空间标记。