> 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/tex-latex-yuan-yu/02-badness.md).

# \badness

#### 摘要

每当 TEX 构造一个盒子（例如，显式地通过一个 `\hbox` 或 `\vbox`）它会设置，或者计算，那个盒子的“坏度”：一个数值，可以把它看作一种衡量或分类盒子内容在该盒子中“适配得有多好”的方式。最近构造的盒子的坏度值会被保存在一个只读寄存器（一个内存位置）中，可通过命令 `\badness`：例如通过 `\the\badness`。请注意，TEX 也会在其他排版活动中创建盒子（例如断行），但这里我们将重点讨论显式的盒子构造命令。

#### 说明与示例

当 TEX 被要求使用以下盒子构造命令创建一个盒子时：

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

你可以选择设置盒子的大小，并让 TEX 尝试将你的内容放入其中；例如 `\hbox to 20pt{<stuff>}` 会要求 TEX 尝试将你的 `<stuff>` 放入一个宽度为 20pt 的盒子中。TEX 会尽最大努力使 `<stuff>` 其通过拉伸或收缩任何可用的胶水而适配。

或者，如果你 *不* 固定一个盒子的大小，而写作 `\hbox{<stuff>}`，TEX 只会构造一个 `\hbox` 其宽度足以容纳 `<stuff>` ，在这种情况下 TEX 会给该盒子分配 0 的坏度值。

当 TEX 决定计算一个盒子的坏度时，它会将你内容的实际尺寸与其必须适配的盒子的相应维度进行比较：你的内容是否太宽，无法适配指定的 `\hbox` ，或者太高，无法适配指定的 `\vbox` 或 `\vtop`？作为坏度计算的一部分，TEX 还会考虑 *总* 盒子内每种类型胶水的数量——也就是说，有多少总的拉伸或收缩量可用于帮助生成所需大小的盒子。

实际上，当 TEX 需要计算一个盒子的坏度值时，它使用（近似的）如下公式：

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

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

坏度值通常介于 0 和 10000 之间，但对于那些即使允许胶水收缩后也仍然太小而无法容纳内容的盒子，会保留一个特殊值：这些盒子被归类为“overfull”，TEX 会将它们的“特殊”坏度值设为 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 Here are the results:\vskip5mm
\noindent box 100 (\fbox{\copy100}) has a \verb|\badness| value of \number\aval\par
\noindent box 101 (\fbox{\copy101}) has a \verb|\badness| value of \number\bval\par
\noindent box 102 (\fbox{\copy102}) has a \verb|\badness| value of \number\cval\par
\noindent box 103 (\fbox{\copy103}) has a \verb|\badness| value of \number\dval\par
\noindent box 104 (\fbox{\copy104}) has a \verb|\badness| value of \number\eval\par
```

下图显示了输出：

![](/files/78c06434b168850e317ff16b66acd6c373db8998)

* 盒子 100： `\hbox{Hello \TeX}`。这具有 `\badness` 0，因为 TEX 并未被要求将内容适配到特定尺寸。
* 盒子 101： `\hbox to25mm{Hello \TeX\hskip0pt plus5pt}`。这个盒子有两份胶水：单词“Hello”后面的词间空白，以及来自 `\hskip` 胶水。然而，所需盒子的宽度为 25mm，这导致有很多空间需要填充，而可用于填充的胶水却不多。多余空间与可用胶水的这种组合会导致 `\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。这个非常大的坏度值（以及重叠的文本）是因为文本“Overleaf”太大，无法放入一个 `\hbox` 仅有 5mm 宽的盒子中——此外，也没有可用的胶水能够吸收多余宽度。TEX 将盒子 104 归类为“overfull”盒子，并将 `\badness` 其设为特殊值 1000000。

#### 相关命令

TEX 提供了若干与报告和显示盒子坏度相关的命令：

* `[\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://ayakaleaf-pro.ayaka.space/latex/zh-cn/tex-latex-yuan-yu/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.
