> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/zh-cn/shen-ru-wen-zhang/11-boxes-and-glue-a-brief-but-visual-introduction-using-luatex.md).

# 盒子与胶水：使用 LuaTeX 的简短但可视化的介绍

## 为未来文章作介绍

本文简要介绍排版中涉及的一些关键概念/模型，使用 $$\mathrm \TeX$$：盒子与胶水——为未来将更详细展开的文章“铺设场景”。后续文章将探讨 $$\mathrm \TeX$$ 盒子，使用 $$\mathrm{Lua}\mathrm\TeX$$ 用于生成图表（节点图），例如以下示例：

![盒子的节点图](/files/8c257f3e17eddb1b868886f916e6a28fb3d5f482)

这个节点图是由一个 `\hbox{...}` 使用下面的 $$\mathrm \TeX$$ 代码：

```latex

\hbox to100pt{A\hskip4pt plus3pt minus 2pt B%
\hskip 0pt plus 2fil C\hskip 0pt plus 2fill D\hskip 0pt plus 3fill}
```

其外观如下（用边界矩形绘制）：

![示例 \hbox](/files/306537a45585339202ccb5b14795e9c8389a1b97)

这些节点图展示了 $$\mathrm \TeX$$ 盒子的深层内部结构——反映了它们在 $$\mathrm \TeX$$ 中的存储方式，并提供了宝贵的图形化说明，有助于更好地理解诸如 `\hbox{...}` 或 `\vbox{...}`.

## 回到基础……

即使作为一个新的，或只是随便看看， $$\mathrm \LaTeX$$ 用户，你也很快会遇到 $$\mathrm \TeX$$ “盒子”这一概念，或者 $$\mathrm \TeX$$ 使用“盒子与胶水”模型来完成其核心排版工作。的排版算法 $$\mathrm \TeX$$ 的大部分时间都花在创建并堆叠水平和垂直的盒子列表上。例如，当 $$\mathrm \TeX$$ 对一段文本进行排版并将其断成一系列行时，它会把段落文本看作由盒子组成的流或序列，并使用这些字符盒子（实际上是字形）的宽度、高度和深度来寻找最佳断行点，然后在排好版的文本行之间加入垂直空白（这种胶水称为 `\baselineskip`）。每一行排好的段落文本本身也是一个盒子（包含其他盒子——例如字符），而排版后的段落各行（盒子）会垂直堆叠以生成段落。最终，产生的是最大的盒子：排版后的页面。显然，这只是一个极度简化的图景，因为你还需要能够移动并定位这些盒子，而 $$\mathrm \TeX$$ 是通过所谓的 *胶*：一种“可伸缩间距”的形式。Knuth 曾评论（《The $$\mathrm \TeX \mathrm{book}$$）说，“glue”或许本该称为“spring”，但这个术语很早就被采用了，而且借用 Knuth 的双关语，它就此“黏住”了。

## 使用……来可视化一个排版后的段落 $$\mathrm{Lua}\mathrm\TeX$$

使用 $$\mathrm{Lua}\mathrm\TeX$$，一个排版后的段落可以被处理以显示用于排版它的组成部分：字符盒子，以及用于在单词之间留空的可伸缩胶水。我们创建了一个简单的 [$$\mathrm{Lua}\mathrm\TeX$$ （纯 $$\mathrm \TeX$$）项目](https://www.overleaf.com/latex/examples/using-a-luatex-callback-to-parse-typeset-paragraphs/mzbpwxtjshsj) 来演示这一点：

[![Screenshot of Overleaf](/files/0925c0f31ebd11ba2a25cd6b6edf0fb7b7b57bcf)](https://www.overleaf.com/latex/examples/using-a-luatex-callback-to-parse-typeset-paragraphs/mzbpwxtjshsj)

该项目的代码是为了说明这篇文章而编写的，并不是段落“解析器”的完整实现——它忽略了若干 $$\mathrm{Lua}\mathrm \TeX$$ 节点类型，纯粹是为了说明本文讨论的想法。

![盒子与胶水](/files/864f1789dc8bee2a30a05aff717a774acc0a6558)

在下面放大的图像部分，你可以看到各个段落行以灰色条带显示——注意行与行之间有空白：竖直胶水， $$\mathrm \TeX$$ 在堆叠它们以形成段落时插入的。如果你想了解更多关于排版段落中行与行之间插入的垂直胶水，可以用 Google 查找讨论 `\baselineskip` 和 `\lineskiplimit`.

![显示盒子和胶水的解析段落](/files/ddc28995d7495a6d07bb6161dd0267559005916f)

当 $$\mathrm \TeX$$ 在读取并排版一段文字时，它会把单词间的空格字符转换为胶水块，而其精确值取决于所使用的字体。在排版过程中，这些单词间胶水会被拉伸或压缩到被认为有助于实现优美断行的程度。在上图中你应注意以下几点：

* 灰色条带是排版后段落各行的边界；
* 每个字符都显示在一个定义其尺寸的盒子中（正如 $$\mathrm \TeX$$ 所看到的）；
* 黄色盒子显示了 $$\mathrm \TeX$$ 为实现优美断行而在单词之间插入的胶水。请注意以下几点：
* 单词间胶水的宽度会因行而异，因为 $$\mathrm \TeX$$ 会对其进行拉伸或压缩以实现每一处断行；
* 在第三行中，句号字符后面的胶水比同一行中的其他胶水更宽。
* 红色框显示了一个显式的 `\hbox{...}` 被使用的位置。 `\TeX` 该 `\hbox{...}` 命令的定义将 E 放入一个 $$\mathrm \TeX$$ 中，以便把它向下压，从而创建

在我们示例段落的末尾，有一条很长的胶水带，称为 `\parfillskip`：这是由 $$\mathrm \TeX$$ 排版引擎插入的，用于填满最后一行的空白。 ![parfillskip glue](/files/ce06bc5fbbc4d7b891f8dec23476c5e9c268a78f)

### 直到下一篇文章

我们希望这篇文章为理解 $$\mathrm \TeX$$中使用的盒子与胶水原理提供了一个有用的介绍。后续文章中，我们将使用 $$\mathrm{Lua}\mathrm\TeX$$ 更深入地探究 $$\mathrm \TeX$$ 盒子的内容——使用本文开头所示示例那样的节点图。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/zh-cn/shen-ru-wen-zhang/11-boxes-and-glue-a-brief-but-visual-introduction-using-luatex.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
