TeX 如何填充虚线

TeX 如何填充虚线

我正在研究 LaTeX 中的盒子和胶水,并尝试了这样的示例:

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}

\begin{document}
Algebra began with\\ computations similar to those of arithmetic, with letters standing for numbers.

Algebra began with\linebreak computations similar to those of arithmetic, with letters standing for numbers.

% kind of imitation of above with boxes:
\hbox to \textwidth{Algebra began with\hfill}

\hbox to \textwidth{Algebra began with}
\end{document}

我是否正确认为,如果是,\\TeX 会在行尾放置一个水平填充符(\hfill或),这样就不会出现未满的水平盒子,或者它会以其他方式工作?如果是,TeX 只有一个宽度,TeX 会拉伸所有空间以填充整个行(这将是一个未满的水平盒子)。那么 TeX 会以这种方式处理吗?\hfil\linebreakhbox\textwidth

答案1

你问的好像是在问 tex 的换行行为,但是这\\是一个 latex 定义的宏,它可以(在对可选参数进行一些检查之后)

\unskip \penalty10000 \hfil \penalty-10000

因此,该命令明确添加了填充胶水以避免水平盒子未充满。

相反\linebreak(在检查可选参数之后)

\penalty-10000

(实际上\linebreak做了更多的事情来规范休息周围的空白,但这可能与此无关)

答案2

的定义\\取决于上下文。

就你的情况而言,LaTeX 最终会这样做\nobreak\hfil\break。现在你可以检查,\nobreak本质上是\penalty10000禁止换行,\break\penalty-10000强制换行。

为什么是第一个?因为否则\hfil可能会被视为换行点(并消失)。相反,它会保留下来,并在其后强制换行。

相反,\linebreak最终会这样做\break,要求 TeX 在排版段落时在那里换行。

请注意,TeX 总是考虑整个段落,而不是像大多数文字处理器那样一次只考虑一行。比较以下两个示例。

\documentclass{article}

\begin{document}

This is some text that has a line break HERE\linebreak
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words.

This starts with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
and has a line break HERE\linebreak
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words.

\end{document}

在此处输入图片描述

在第一种情况下,TeX 别无选择,只能将单词之间的空格分散到顶行,导致框未满。相反,在第二段中,分散的空格可以分布在两行中。您可以检查,如果删除\linebreak,行会在“HERE”后面的“and”之后断开。

然而,如果我们尝试

This starts with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
and has a line break HERE\linebreak
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words.

再加一个“继续说废话”,我们会得到

在此处输入图片描述

因为段落无论如何都会不够饱满,而将拉伸集中在一行中会使不良程度最小。但如果我们用非零来帮助 TeX\emergencystretch

This starts with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
goes on with nonsense words,
and has a line break HERE\linebreak
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words
and goes on with nonsense words.{\emergencystretch=12em\par}

我们得到

在此处输入图片描述

在强制断裂之前,拉伸再次分布在线路中。

相关内容