> 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/tex-latex-ji-ben-ming-ling/02-badness.md).

# \badness

#### 摘要

每次 TEX 建構一個盒子（例如，透過一個明確的 `\hbox` 或 `\vbox`）時，它會設定或計算該盒子的「badness」：一個數值，可視為用來衡量或分類盒子內容與該盒子契合得有多「好」的方法。最近建立的盒子的 badness 值會儲存在一個唯讀暫存器（記憶體位置）中，可由指令 `\badness`：例如透過 `\the\badness`。請注意，TEX 在其他排版作業中也會建立盒子（例如斷行），但此處我們將聚焦於明確的盒子建構指令。

#### 說明與範例

當 TEX 被要求使用下列盒子建構指令建立一個盒子時：

* `\hbox`
* `\vbox`
* `\vtop`

你可以選擇設定盒子的大小，並請 TEX 嘗試將你的內容塞入其中；例如 `\hbox to 20pt{<stuff>}` 會要求 TEX 嘗試將你的 `<stuff>` 放入一個寬度為 20pt 的盒子中。TEX 會盡力讓 `<stuff>` 透過拉伸或壓縮任何可用的 glue 來符合。

或者，如果你 *不* 透過撰寫以下內容來固定盒子的大小， `\hbox{<stuff>}`，TEX 就會直接建構一個 `\hbox` 寬度足以容納 `<stuff>` ，在那種情況下 TEX 會將該盒子的 badness 值設為 0。

當 TEX 決定計算某個盒子的 badness 時，它會將你的內容實際大小與必須容納它的盒子相應尺寸進行比較：你的內容是否太寬而無法符合指定的 `\hbox` 或太高而無法符合指定的 `\vbox` 或 `\vtop`？在計算 badness 的同時，TEX 也會考慮 *總* 盒子內各種類型 glue 的數量——也就是說，可用來協助產生所需尺寸盒子的總伸展量或收縮量有多少。

實務上，當 TEX 需要計算某個盒子的 badness 值時，它會使用（

$$\mathrm{badness}=100\left ({t \over s}\right )^3$$

其中 $$t$$ 是要容納的所需「額外寬度」，而 $$s$$ 是可用於伸展或收縮的 glue 量。

badness 的值通常介於 0 與 10000 之間，但有一個特殊值保留給那些即使考慮 glue 收縮後，仍然大到不足以容納內容的盒子：這些盒子會被分類為「overfull」，而 TEX 會為它們指定一個「特殊」badness 值 1000000。

好了，討論／理論到此為止，接下來我們來看一些範例。我們將建立 5 個盒子，並使用 5 `\count` 個暫存器來保存 `\badness` TEX 剛建立的盒子的值。稍後我們會使用這些 `\count` 暫存器輸出 `\badness` 相關盒子的值。

```
\newcount\aval\newcount\bval\newcount\cval\newcount\dval\newcount\eval
\setbox100=\hbox{Hello \TeX}\aval=\the\badness %我們沒有設定盒子大小
\setbox101=\hbox to25mm{Hello \TeX\hskip0pt plus5pt}\bval=\the\badness
\setbox102=\hbox to50mm{Hello \TeX}\cval=\the\badness
\setbox103=\hbox to50mm{Hello \TeX\hfill}\dval=\the\badness
\setbox104=\hbox to5mm{Overleaf}\eval=\the\badness
\setlength{\fboxsep}{0pt}% 在每個盒子周圍加上緊密邊框
\noindent 結果如下：\vskip5mm
\noindent 盒子 100（\fbox{\copy100}）的 \verb|\badness| 值為 \number\aval\par
\noindent 盒子 101（\fbox{\copy101}）的 \verb|\badness| 值為 \number\bval\par
\noindent 盒子 102（\fbox{\copy102}）的 \verb|\badness| 值為 \number\cval\par
\noindent 盒子 103（\fbox{\copy103}）的 \verb|\badness| 值為 \number\dval\par
\noindent 盒子 104（\fbox{\copy104}）的 \verb|\badness| 值為 \number\eval\par
```

下圖顯示輸出：

![](/files/e35f0db6c84bb36b3e3dabd0c10e5970e3caffe7)

* 盒子 100： `\hbox{Hello \TeX}`。這個有 `\badness` 0，因為 TEX 沒有被要求將內容塞入特定大小。
* 盒子 101： `\hbox to25mm{Hello \TeX\hskip0pt plus5pt}`。這個盒子有兩個 glue：單字「Hello」後的字間空白，以及來自 `\hskip` glue。然而，所需的盒子寬度為 25mm，這會產生很多需要填補的空間，但可用來填補的 glue 並不多。這種多餘空間與可用 glue 的組合導致 `\badness` 值為 6396。
* 盒子 102： `\hbox to50mm{Hello \TeX}`。這個盒子只有一個 glue 項目：單字「Hello」後的字間空白，而且有很多多餘空間需要填補。這種多餘空間與少量 glue 的組合會導致一個非常大的 `\badness` 值——高到 TEX 會將其「截斷」為最大值 10000。此外，字間空白被大幅拉伸以填滿盒子，因而在「Hello」與「TEX」之間出現巨大的空隙
* 盒子 103： `\hbox to50mm{Hello \TeX\hfill}`。這個盒子與盒子 102 類似，但它有兩個 glue：單字「Hello」後的字間空白，另外還有由 `\hfill`——一種所謂「無限可伸展」的 glue。因此，`\hfill` 可以按需要延伸得非常遠，並擴展以吸收所有多餘空間。由於這種「無限可伸展」的 glue，所產生的 `\badness` 為 0。
* 盒子 104： `\hbox to5mm{Overleaf}` 這個盒子與其後面的文字重疊，它有一個 `\badness` 值為 1000000。這個極大的 badness 值（以及文字重疊）是因為文字「Overleaf」太大，無法放入一個 `\hbox` 而其寬度只有 5mm 的盒子中——此外，也沒有可用的 glue 能夠吸收多餘寬度。TEX 將盒子 104 分類為「overfull」盒子，並將 `\badness` 設為特殊值 1000000。

#### 相關指令

TEX 提供多個與回報及顯示盒子 badness 相關的指令：

* `[\overfullrule](./07-overfullrule.md)`
* `[\hfuzz](./06-hfuzz.md)`
* `[\vfuzz](./06-hfuzz.md)`
* `[\hbadness](./05-hbadness.md)`
* `[\vbadness](./05-hbadness.md)`


---

# 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/tex-latex-ji-ben-ming-ling/02-badness.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.
