> 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/tex-latex-ji-yuan/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>` 通过拉伸或压缩任何可用的胶水使其适配。

或者，如果你 *不* 去固定盒子的尺寸，只需写 `\hbox{<stuff>}`，TEX 将直接构造一个 `\hbox` 其宽度足以容纳 `<stuff>` ，在这种情况下 TEX 会将该盒子的 badness 值设为 0。

当 TEX 决定计算一个盒子的 badness 时，它会将内容的实际尺寸与必须容纳它的盒子的相应维度进行比较：你的内容是否太宽，无法放入指定的 `\hbox` ，或者太高，无法放入指定的 `\vbox` 或 `\vtop`？ *总* 量——即盒子内每种胶水的总量，也就是有多少总拉伸或压缩可用来帮助生成所需大小的盒子。

在实践中，当 TEX 需要计算某个盒子的 badness 值时，它使用（近似的）以下公式：

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

其中 $$t$$ 是需要容纳的目标“超出宽度”，而 $$s$$ 是可用于拉伸或压缩的胶水量。

badness 的值通常介于 0 和 10000 之间，但有一个特殊值保留给那些即使考虑胶水收缩后也根本不足以容纳内容的盒子：这些盒子被归类为“溢出（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 %We do not set the box size
\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}% put a tight border around each box
\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/78c06434b168850e317ff16b66acd6c373db8998)

* 盒子 100： `\hbox{Hello \TeX}`。这有 `\badness` 0，因为 TEX 并未被要求将内容放入特定尺寸中。
* 盒子 101： `\hbox to25mm{Hello \TeX\hskip0pt plus5pt}`。这个盒子有两个胶水：单词“Hello”后面的字间空白，以及来自 `\hskip` 胶水。然而，目标盒子宽度为 25mm，这就产生了大量需要填充的空间，而可用来填充的胶水却不多。多余空间与可用胶水的这种组合导致 `\badness` badness 值为 6396。
* 盒子 102： `\hbox to50mm{Hello \TeX}`。这个盒子只有一个胶水项：单词“Hello”后面的字间空白，并且有大量多余空间需要填充。多余空间与少量胶水的这种组合会导致一个非常大的 `\badness` 值——高到 TEX 会将其“截断”到最大值 10000。此外，字间空白被大幅拉伸以填满盒子，导致“Hello”和“TEX”之间出现一个巨大的间隙
* 盒子 103： `\hbox to50mm{Hello \TeX\hfill}`。这个盒子与盒子 102 类似，但它有两个胶水：单词“Hello”后面的字间空白，以及由 `\hfill`——一种所谓的“无限柔性”胶水。因此，`\hfill` 可以按需要无限拉伸，并扩展以吸收所有多余空间。由于这种“无限柔性”胶水，结果 `\badness` 为 0。
* 盒子 104： `\hbox to5mm{Overleaf}` 这个盒子与其后面的文本重叠，它有一个 `\badness` 值为 1000000。这个非常大的 badness 值（以及重叠的文本）是因为文本“Overleaf”太大，无法放入一个 `\hbox` 宽度仅为 5mm 的盒子中；而且，另外也没有可用的胶水能够吸收多余宽度。TEX 将盒子 104 归类为“溢出”盒子，并将 `\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-cn/tex-latex-ji-yuan/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.
