何时使用 \、{ } 或 ~ 来禁止吞噬宏后面的空格

何时使用 \、{ } 或 ~ 来禁止吞噬宏后面的空格

有多种方法可以确保宏后的空格不会被吞噬,不知道该如何决定使用哪种方法。在某些情况下,使用哪种解决方案很重要吗?

\documentclass{article}
\begin{document}
\TeX{} followed by curly brace is ok.\par
\TeX\ followed by a back slash is ok.\par
\TeX~followed by a tilde also works.\par
\end{document}

答案1

独特的选择是使用或不使用连接线~。连接线旨在使文档元素在两侧保持连接,类似于 HTML 的&nbsp。下面还有几个例子来说明差异:

在此处输入图片描述

\documentclass{article}
\begin{document}
\TeX{} followed by curly brace is ok.\par
\TeX\ followed by a back slash is ok.\par
\TeX~followed by a tilde also works.\par
\TeX{ }followed by a group works.\par
\TeX\@ followed by a \verb|\@| works. \par
\TeX\space followed by \verb|\space| works.

\hrulefill

Here is some random text, as much as is needed to fill the line okay. \TeX{} followed by something is ok.\par
Here is some random text, as much as is needed to fill the line okay. \TeX\ followed by something is ok.\par
Here is some random text, as much as is needed to fill the line okay. \TeX~followed by something is ok.\par
Here is some random text, as much as is needed to fill the line okay. \TeX{ }followed by something is ok.\par
Here is some random text, as much as is needed to fill the line okay. \TeX\@ followed by something is ok.\par
Here is some random text, as much as is needed to fill the line okay. \TeX\space followed by something is ok.

\end{document}

请注意,当没有引入换行符时,所有间距似乎都相等(顶部)。但是,一旦引入换行符,单词间的拉伸/收缩和可能的连字会导致单词移动,以“优化显示”;连接~避免在空格处换行(通过设置非常大的\penalty- 可以这么说,减少了有助于优化的可能性)。

那么,回答你的问题。“在某些情况下,使用哪种解决方案很重要吗?”是的,但这取决于使用的位置,如上所示。

有关 TeX Book 中关于领带的实际描述的更多信息,请参阅引用/参考时带或不带波浪号有什么区别?


xspace包裹提供了一种绕过此间距选择的方法,即在输入流中提前查看控制序列后面是否有空格。典型用法如下(以非常最小的形式):

\usepackage{xspace}% http://ctan.org/pkg/xspace
\newcommand{\TEX}{\TeX\xspace}

这将允许您输入\TEX followed by a space is ok.并获得适当的输出TeX followed by a space is ok.

答案2

比较输出:

\documentclass{article}
\begin{document}
\minipage{1.25cm}
\TeX{} followed by curly brace is ok.\par
\TeX\ followed by a back slash is ok.\par
\TeX~followed by a tilde also works.\par
\endminipage

\bigskip
foo  bar\par
foo\ \ bar\par
foo~~bar

\end{document}

在此处输入图片描述

默认空格 -- 控制空格 -- 非中断空格

\nonfrenchspacing在使用来固定非句子结尾的句号后的间距时,需要使用控制空格。~用于75~kg防止两部分之间换行。

相关内容