> For the complete documentation index, see [llms.txt](https://ayakaleaf-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://ayakaleaf-pro.ayaka.space/latex/zh-cn/shen-du-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》一书第70页） $$\mathrm \TeX \mathrm{book}$$），“glue” 或许本应称为“spring”，但“glue”这个术语很早就被采用了，而且借用 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` 命令把 E 放入一个 `\hbox{...}` 中，以便将其下移来创建 $$\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://ayakaleaf-pro.ayaka.space/latex/zh-cn/shen-du-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.
