Aller au contenu

Summary

Each time TEX constructs a box (e.g., explicitly via an \hbox or \vbox) it sets, or calculates, that box’s “badness”: a numeric value which can be thought of as a way to measure, or classify, how “well” the content of a box fits inside that box. The badness value for the most-recently constructed box is saved in a read-only register (a memory location) which can be accessed by the command \badness: for example by \the\badness. Note that TEX also creates boxes during other typesetting activities (e.g. linebreaking) but here we’ll focus on explicit box-construction commands.

Explanation and examples

When TEX is asked to create a box using the following box-construction commands:

  • \hbox
  • \vbox
  • \vtop

you can choose to set the size of the box and ask TEX to try to fit your content into it; for example \hbox to 20pt{<stuff>} will ask TEX to try to fit your <stuff> into a box that is 20pt wide. TEX will do its best to make <stuff> fit by stretching or shrinking any available glue.

Alternatively, if you don’t fix the size of a box, by writing \hbox{<stuff>}, TEX will simply construct an \hbox that is sufficiently wide to accommodate <stuff> and in that case TEX will assign that box a badness value of 0.

When TEX decides to calculate the badness of a box it compares the actual size of your content with the appropriate dimension of the box in which it has to fit: is your content too wide to fit the specified \hbox or too high to fit the designated \vbox or \vtop? As part of the badness calculation TEX also takes into account the total amount of each type of glue within the box—i.e., how much total stretch or shrink is available to help produce a box of the desired size.

In practise, when TEX needs to calculates a box’s badness value it uses (an approximation to) the following formula:

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

where \(t\) is the desired “excess width” to be accommodated and \(s\) is the amount of glue available for stretching or shrinking.

The value of badness usually falls between 0 and 10000 but a special value is reserved for boxes that, even after allowing for the shrinking of glue, are simply not big enough to hold the content: those boxes are classified as “overfull” and TEX assigns them a “special” badness value of 1000000.

OK, so much for discussion/theory we’ll now look at some examples. We’ll create 5 boxes and use 5 \count registers to hold the \badness value of the box that TEX has just created. We later use those \count registers to output the \badness value of the relevant box.

\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

The following graphic shows the output:

  • box 100: \hbox{Hello \TeX}. This has \badness 0 because TEX was not asked to fit the content to a specific size.
  • box 101: \hbox to25mm{Hello \TeX\hskip0pt plus5pt}. This box has two glues: an interword space after the word “Hello” and 5pt coming from the \hskip glue. However, the desired box is 25mm wide which results in a lot of space to fill with not much glue to do so. That combination of excess space and available glue results in the \badness value of 6396.
  • box 102: \hbox to50mm{Hello \TeX}. This box has just one glue item: an interword space after the word “Hello” and has a lot of excess space to fill. That combination of excess space and small amount of glue results in a very large \badness value—so high that TEX “clips” it to the maximum value of 10000. In addition, the interword space is stretched considerably to fill up the box, resulting in a huge gap appearing between “Hello” and “TEX
  • box 103: \hbox to50mm{Hello \TeX\hfill}. This box is similar to box 102 but it has two glues: the interword space after the word “Hello” but it has a second very flexible glue provided by \hfill–a so-called “ininitely flexible” glue. Consequently,\hfill can stretch as far as required and expands to absorb all the excess space. Because of the “ininitely flexible” glue the resulting \badness is 0.
  • box 104: \hbox to5mm{Overleaf} This box overlaps the text following it has a \badness value is 1000000. The very large badness value (and overlapping text) is due to the text “Overleaf” being too big to fit in an \hbox that is only 5mm wide—and, in addition, there are no available glues capable of absorbing the excess width. TEX classifies box 104 as an “overfull” box and sets \badness to the special value of 1000000.

Related commands

TEX provides a number of commands related to reporting and displaying the badness of boxes:

Overleaf guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

Advanced TeX/LaTeX