> 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-tw/shen-ru-wen-zhang/11-boxes-and-glue-a-brief-but-visual-introduction-using-luatex.md).

# Box 與 Glue：使用 LuaTeX 的簡短但具視覺化的介紹

## 供未來文章的導言

這篇文章是對使用……進行排版時所涉及的一些關鍵概念／模型的簡短導論 $$\mathrm \TeX$$: 盒子與膠水——為未來將會更詳細討論的文章「鋪陳場景」。後續文章將探討 $$\mathrm \TeX$$ 盒子，使用 $$\mathrm{Lua}\mathrm\TeX$$ 來產生如下範例的圖表（節點圖）：

![盒子的節點圖](/files/bbf51f95868b3f52e5c4a77bc0427ce5746110cf)

這張節點圖是由一個 `\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/b152b614e8047510a09261a7fa11454a6967388a)

這些節點圖顯示了 $$\mathrm \TeX$$ 盒子的深層內部結構——反映它們在 $$\mathrm \TeX$$ 中的儲存方式，並提供極具價值的圖形化描述，有助於更深入理解像是 `\hbox{...}` 或 `\vbox{...}`.

## 回到基礎……

即使作為一個新的，或只是偶爾使用的， $$\mathrm \LaTeX$$ 使用者，你很快就會遇到 $$\mathrm \TeX$$ 「盒子」的概念，或者 $$\mathrm \TeX$$ 其核心排版活動採用「盒子與膠水」模型。 $$\mathrm \TeX$$ 的排版演算法，大部分時間都花在建立並堆疊水平與垂直的盒子清單上。例如，當 $$\mathrm \TeX$$ 排版一段文字並將其分成一系列行時，它會把段落文字視為一串或一序列盒子，並使用這些字元盒子的寬度、高度與深度（實際上是字形）來找出最佳換行點，然後在排版後的文字行之間加入垂直空白（這種膠水稱為 `\baselineskip`）。段落中每一行排版結果本身也是一個盒子（包含其他盒子——例如字元），而排版後的段落各行（盒子）則垂直堆疊形成段落。最終，會產生最大的盒子：排版完成的頁面。顯然，這是一幅極度簡化的圖像，因為你還需要能夠移動並定位那些盒子，而且 $$\mathrm \TeX$$ 透過所謂的 *glue*來做到這一點：一種「彈性間距」的形式。Knuth 曾在《The $$\mathrm \TeX \mathrm{book}$$》第70頁評論說，「膠水」也許本該稱作「彈簧」，但這個術語很早就被採用了，而且借用 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/e777620a29ae1cf4deec074afda72646b1a071b5)](https://www.overleaf.com/latex/examples/using-a-luatex-callback-to-parse-typeset-paragraphs/mzbpwxtjshsj)

來示範這一點： $$\mathrm{Lua}\mathrm \TeX$$ 這個專案的程式碼是為了說明這篇文章而寫，並不是段落「剖析器」的完整實作——它忽略了若干

![節點類型，並且純粹是為了說明本文所討論的概念。](/files/73e4d90a00683097a489a4395a6a146bc30192d0)

在下方的放大影像區塊中，你可以看到各個段落行被陰影標示成灰色條帶——請注意行與行之間有白色空隙：垂直膠水， $$\mathrm \TeX$$ 在堆疊它們以形成段落時插入的。如果你想進一步了解排版段落中行與行之間插入的垂直膠水，可以用 Google 搜尋討論 `\baselineskip` 以及 `\lineskiplimit`.

![顯示盒子與膠水的已剖析段落](/files/08e62114fc9d8c81052b9fba0ac8df59b87bc873)

當 $$\mathrm \TeX$$ 讀取並排版一段文字時，會把字間空白字元轉換成膠水團塊，其精確數值是所用字型的屬性之一。在排版過程中，這些字間膠水會被拉伸或壓縮到被認為足以達成美觀換行的程度。在上圖中你應注意以下幾點：

* 灰色條帶是排版後段落各行的邊界；
* 每個字元都顯示在一個定義其尺寸的盒子中（如 $$\mathrm \TeX$$ 所見）；
* 黃色方塊顯示了 $$\mathrm \TeX$$ 為了達成美觀換行而在字與字之間插入的膠水。請注意以下幾點：
* 字間膠水的寬度會因行而異，因為 $$\mathrm \TeX$$ 會伸展或收縮它來達成每次換行；
* 在第三行中，句點字元後面的膠水比同一行中的其他膠水更寬。
* 紅色方塊顯示了何處使用了明確的 `\hbox{...}` 。該 `\TeX` 命令會將 E 放入一個 `\hbox{...}` 中，使其可以向下移動以建立 $$\mathrm \TeX$$ 標誌。

在範例段落的結尾，有一長條膠水稱為 `\parfillskip`：這是由 $$\mathrm \TeX$$ 排版引擎插入以填滿最後一行的空白。 ![parfillskip glue](/files/d12c56ec6c6d0cd4c8e5c569e973880e2552743e)

### 直到下一篇文章

我們希望這篇文章已對 $$\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-tw/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.
