\documentclass{article}
\usepackage[breaklinks]{hyperref}
\usepackage{graphicx}
\usepackage{parskip}
\usepackage{hologo}
\usepackage{xcolor}
\usepackage{libertinust1math}
\usepackage{libertine}
\usepackage{inconsolata}
\usepackage{microtype}
\usepackage{fancyvrb}
\title{Exploring \ufull or \ofull boxes and \bness calculations}
\author{Overleaf Support Team}
\date{May 2020}
\begin{document}
\input boxmacros
\maketitle
\textbf{NOTE: The calculations in this article will no longer be valid if you change the document text font.}
\section{Introduction}
This article explores the meaning of \ufull and \ofull boxes and examines the calculation of \bness using an \verb|\hbox| with \textit{finite} and \textit{infinite} glues. We have tried to provide a range of examples to demonstrate various aspects of \TeX{} engines' box-construction behaviour and the ``diagnostic messages'' issued by \TeX{} engines during the box-creation process. It is unlikely that we have covered every possible scenario but hope to have provided sufficient material to ``demystify'' some aspects of \TeX-based typesetting. We also hope to be forgiven for using various raw \TeX{} commands within the examples \texttt{:-)}.
\section{Background: fitting ``stuff'' into boxes}
Before going on vacation most of us dust-down our suitcases to fill them with a collection of materials we need for the trip. Of course, a suitcase is a three-dimensional (3D) box with a (mostly) fixed width, height and depth. Into that 3D box we might try to squeeze as much as we need (or think we need...). If we're lucky, and can travel light, our ``stuff'' might not completely fill the case, so it will have excess space and be \ufull\unskip---or we may try to squeeze too much into it, and fail, because the suitcase is \ofull\unskip.
\TeX{} engines face a not too dissimilar problem when tasked with packaging typeset material into the boxes it creates---\TeX's boxes are restricted to 2D but with the vertical dimension subdivided into the box's depth and height. Deep inside \TeX{} engines the process of creating boxes is referred to as \textit{packaging}: in effect, it involves taking a list of typeset items and packing them into a container to provide a ``boundary area'' which defines how those items will be arranged on the typeset page. The resultant packaged-up content container (a box) produces an \verb|\hbox| or one of the vertical variants: \verb|\vbox|, \verb|\vtop| or \verb|\vcenter|.
%Note that \verb|\vtop| and \verb|\vcenter| are specialized types of \verb|\vbox|---\verb|\vcenter| is used in math mode to vertically centre its content.
\section{Introduction to boxes and glue}
All \TeX{} engines (\Hologo{pdfTeX}, \Hologo{XeTeX}, \Hologo{LuaTeX} \textit{etc.}) typeset \Hologo{LaTeX} documents using Donald Knuth’s so-called \href{https://www.overleaf.com/learn/latex/Articles/Boxes_and_Glue:_A_Brief,_but_Visual,_Introduction_Using_LuaTeX}{\color{blue}boxes and glue model}: your typeset text or mathematics is assembled into a “box” and a \TeX{} engine uses flexible space called \textit{glue} to adjust the positioning of material within the box, helping to achieve a desirable typeset result. For example, when breaking a paragraph into lines of typeset text a \TeX{} engine uses so-called \textit{interword glue} to adjust spacing between individual words to achieve an optimal linebreak.
The term “glue” is probably not ideal and it may have been preferable to describe \TeX's flexible spacing as behaving like a spring---with its ability to stretch or shrink. Usage of the term glue was adopted early in the history of \TeX’s development---people quickly got accustomed to using ``glue'' so it soon became too late give it a better name, such as spring.
\subsection{A little more about glue}
If you are new to \Hologo{LaTeX} you may not have encountered the lower-level concept of \TeX{} glue so here an introduction to the key ideas. Because glue is a form of flexible spacing it is specified using three components: a fixed amount plus an amount it can stretch and an amount by which it can shrink. Glues can be specified to allow finite or infinite amounts of stretching and/or shrinking.
You can specify glue using commands such as \verb|\hskip| or \verb|\vskip|:
\begin{itemize}
\item \raggedright horizontal glue: \textbf{\texttt{\string\hskip}} \texttt{<fixed amount>} \texttt{\bfseries plus} \texttt{<amount to stretch>} \texttt{\bfseries minus} \texttt{<amount to shrink>}
\item \raggedright vertical glue: \textbf{\texttt{\string\vskip}} \texttt{<fixed amount>} \texttt{\bfseries plus} \texttt{<amount to stretch>} \texttt{\bfseries minus} \texttt{<amount to shrink>}
\end{itemize}
where
\begin{itemize}
\item\texttt{\bfseries plus} and \texttt{\bfseries minus} are keywords that \TeX{} engines understand
\item\texttt{<fixed amount>} is referred to as the \textit{normal} component of the glue
\item\texttt{<amount to stretch>} is referred to as the \textit{stretch} component of the glue
\item\texttt{<amount to shrink>} is referred to as the \textit{shrink} component of the glue.
\end{itemize}
\texttt{<fixed amount>} is required but \texttt{<amount to stretch>} and \texttt{<amount to shrink>} are both optional—if either, or both, are missing they are set to a value of 0.
In addition to \verb|\hskip| and \verb|\vskip| \TeX{} engines provide other primitive (built-in) glue-related commands that we won't address here. Those commands include:
\begin{itemize}
\item horizontal glue: \verb|\hfil|, \verb|\hfill|, \verb|\hfilneg|, \verb|\hss|
\item vertical glue: \verb|\vfil|, \verb|\vfill|, \verb|\vfilneg|, \verb|\vss|
\end{itemize}
together with \verb|\mskip| for inserting glue in math expressions.
\subsection{Finite and infinite glue units}
For glues specified with \verb|\hskip| and \verb|\vskip| the normal component of glue (\texttt{<fixed amount>}) has to be expressed in ``real world'' units; however, \TeX{} engines allow for the shrink component (\texttt{<amount to shrink>}) and the stretch component (\texttt{<amount to stretch>}) to be expressed in \textit{two} types of unit:
\begin{itemize}
\item \textit{finite} units: these are ``real word'' quantities such as points (\texttt{pt}), millimetres (\texttt{mm}), inches (\texttt{in}) \textit{etc.}
\item \textit{infinite} units: these are ``\TeX{} world'' quantities called \texttt{fil}, \texttt{fill} or even \texttt{filll}
\end{itemize}
\TeX{} engines use those strange ``infinite units'' to create glue components that are infinitely stretchable or infinitely shrinkable.
Each of \texttt{fil}, \texttt{fill} or \texttt{filll} represent “different orders” of infinite flexibility, with each one being more flexible than the next:
\[\texttt{fil} < \texttt{fill} < \texttt{filll}\]
Glue components expressed in finite units are said to have \textit{glue order} 0 but components expressed in infinite units have glue order 1, 2 or 3, as shown in the following table:\vskip\parskip
\tabskip=5mm
\halign{#&\hfil#\hfil\cr
\textbf{Units}&\textbf{Glue order}\cr
\texttt{pt}, \texttt{mm}, \texttt{in}, \textit{etc.}&0\cr
\texttt{fil}&1\cr
\texttt{fill}&2\cr
\texttt{filll}&3\cr}
Putting this all together:
\begin{itemize}
\item the normal component of glue (\texttt{<fixed amount>}) is mandatory and has to be expressed in ``real world'' units, so it is always glue order 0. Note that the normal component can have zero width by specifying 0pt, 0mm \textit{etc}.
\item the shrink component (\texttt{<amount to shrink>}) and the stretch component (\texttt{<amount to stretch>}) of glue are both optional and can be expressed in finite units (glue order 0) or infinite units of \texttt{fil}, \texttt{fill} \texttt{filll} with glue order 1, 2 or 3 respectively \end{itemize}
Just to add another term, the value of a glue's normal component is also called its \textit{natural width} which is the amount of space occupied by that glue if no stretching or shrinking takes place. Interested readers can find more information and a detailed worked example in the Overleaf article \href{https://www.overleaf.com/learn/latex/Articles/How\_TeX\_Calculates\_Glue\_Settings\_in\_an\_\%5Chbox}{\color{blue}How \TeX{} Calculates Glue Settings in an \texttt{\slash hbox}}.
\subsubsection{Example: glue with finite components}
Glue can be specified to have stretch or shrink components using any combination of finite or infinite units but we'll start with an example of glue with finite stretch and shrink.
If we specify a glue example such as \texttt{\string \hskip 2pt plus 1pt minus 0.5pt} this glue is comprised of the following:
\begin{itemize}
\item The \textit{normal component} value is 2pt (the normal component is always a finite value). This glue is also said to have a \textit{natural width} of 2pt.
\item The \texttt{plus} keyword specifies the amount of \textit{stretch}. Here, the glue has a finite stretch component value of 1pt.
\item The \texttt{minus} keyword specifies the amount of \textit{shrink}. Here, the glue has a finite shrink component value of 0.5pt.
\end{itemize}
By specifying this glue we are providing the \TeX{} engine with ``flexible space'' that is normally 2pt wide but we \textit{recommend}, to the \TeX{} engine, this glue can stretch by up to 1pt or shrink by up to 0.5pt. So, ideally, this glue (flexible space) can shrink to become as small as \(2\textrm{pt} - 0.5\textrm{pt}=1.5\textrm{pt}\) or stretch up to \(2\textrm{pt} + 1\textrm{pt}=3\textrm{pt}\).
If the \TeX{} engine needed to fill a particular box it could use our glue to create some space which occupies anything from 1.5pt up to 3pt. If, for whatever reason, the \TeX{} engine did not need to stretch or shrink this glue it would be used at its \textit{natural width} of 2pt---its normal component value.
\textbf{Caveat}: As we'll see below, \TeX{} engines \textit{will over-stretch finite glue} beyond our recommended value for \texttt{<amount to stretch>} but they \textit{will not over-shrink finite glue} below the amount specified by our \texttt{<amount to shrink>}. \TeX{} engines consider over-shrinking to be a ``very bad thing indeed!''.
\subsubsection{Example: glue with infinite components}
We can rewrite our previous example with infinite glue using \texttt{\string\hskip 2pt plus 1fil minus 0.5fil} which comprises the following:
\begin{itemize}
\item The \textit{normal component} value is 2pt (the normal component is always a finite value). This glue is also said to have a \textit{natural width} of 2pt.
\item The \texttt{plus} keyword specifies the amount of \textit{stretch}. Here, it has an infinite stretch component value of \texttt{1fil}.
\item The \texttt{minus} keyword specifies the amount of \textit{shrink}. Here, the glue has an infinite shrink component value of 0.5pt.
\end{itemize}
Our glue has “infinite” stretch and “infinite” shrink (in units of \texttt{fil}) so it can stretch or shrink to any desired value.
\subsubsection{Mixing finite and infinite glue components}
You can specify glue which has a mixture of finite and infinite components, such as \texttt{\string\hskip 2pt plus 1fil minus 0.5pt}. For this particular glue we would say it has an infinite stretch component (from the \texttt{plus 1fil}) but finite shrink component (from the \texttt{minus 0.5pt}). This glue can stretch out to any desired value but can only shrink down to a minimum of 1.5pt.
\subsubsection{Examples of glue errors}
If you try to use infinite glue as the normal component and write something like
\verb|\hskip 1fil plus 2pt minus3pt|
you'll receive an error:
\begin{Verbatim}[fontsize=\small]
! Illegal unit of measure (pt inserted).
<to be read again>
f
l.57 \hskip 1f
il plus 2pt minus3pt
Dimensions can be in units of em, ex, in, pt,
pc, cm, mm, dd, cc, nd, nc, bp, or sp;
but yours is a new one!
I'll assume that you meant to say pt, for printer's points.
...
\end{Verbatim}
If you try to omit the normal component completely and write something like
\verb|\hskip plus 2pt minus3pt|
you'll see errors such as these:
\begin{Verbatim}[fontsize=\small]
! Missing number, treated as zero.
<to be read again>
p
l.93 \hskip p
lus 2pt minus3pt
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)
! Illegal unit of measure (pt inserted).
<to be read again>
p
l.93 \hskip pl
us 2pt minus3pt
Dimensions can be in units of em, ex, in, pt,
pc, cm, mm, dd, cc, nd, nc, bp, or sp;
but yours is a new one!
I'll assume that you meant to say pt, for printer's points.
...
\end{Verbatim}
\section{Back to boxes}
We noted that the task of creating boxes is referred to as \textit{packaging}: fitting a list of typeset items into a container which provides a ``boundary area'' defining how those items will be arranged on the typeset page. A vital part of the packaging process is to determine how the \textit{glues} inside that box will behave: will they stretch or shrink? If so, which ones and by how much? The placement of individual items within the enclosing box area/dimensions is largely determined by stretching or shrinking of glues available in that box.
\subsection{Packing boxes}
Each time a \TeX{} engine is asked to package a list of typeset items into a new box it starts out by setting that box's \bness value to 0---only if it needs to, will \TeX{} later calculate an actual \bness value for that box. The \TeX{} engine proceeds to examine all the individual items contained in the list supplied for packaging---those items might be characters (glyphs), rules, glue, penalties, other boxes and so forth. Based on the nature of each item, \TeX{} will determine how each of them might affect the dimensions of the box it is constructing. The goal is to determine the \textit{natural size} of the box which is then compared to the user's \textit{desired size} so that \TeX{} can work out how to stretch or shrink the glue.
\TeX{} engines take particular care with any glue values detected in the list to be packaged. Only the normal component (natural width) of glue directly contributes to the \textit{natural size} dimensions of the box; the glue stretch and glue shrink components do not. Of course, a \TeX{} engine does not completely ignore the stretch and shrink component values of glues in the list being packaged. The \TeX{} engine keeps a running total of how much glue stretch and glue shrink it has seen for each \textit{order} of glue. Recall that finite units of glue have glue order 0 whereas infinite units of fil, fill and filll have glue order 1, 2 and 3 respectively.
After the \TeX{} engine has examined all items in the list to be packaged-up it will have calculated the natural size of the box. Recall that for glue items their natural size is their normal component (the \texttt{<fixed amount>}).
\TeX{} next compares the natural size with any desired size set by the user---for example, you set the desired size to \texttt{<dimension>} when writing \verb|\hbox to <dimension>{...}|. Note, if you do not specify a size for a box and write \verb|\hbox{...}| or \verb|\vbox{...}| those boxes will be have the natural size of the components within them and the glues will not need to shrink or stretch.
Clearly, the user can set a desired size which is the same as, greater than or less than the box's natural size. If we denote the box's natural size by \(\mathrm{{S}_{N}}\) and the user's desired size by \(\mathrm{{S}_{D}}\) then there are three possibilities:
\begin{enumerate}
\item \(\mathrm{{S}_{N}} > \mathrm{{S}_{D}}\): the natural size is greater than the desired size, so the box contents (i.e., the glues) need to \textit{shrink}.
\item \(\mathrm{{S}_{N}} < \mathrm{{S}_{D}}\): the natural size is less than the desired size, so the box contents (i.e., the glues) need to \textit{stretch}.
\item \(\mathrm{{S}_{N}} = \mathrm{{S}_{D}}\): the natural size is the same as the desired size, so the glues do not need to shrink or stretch. They will use their normal width value (their natural width arising from the \texttt{<fixed amount>} value).
\end{enumerate}
After examining \(\mathrm{{S}_{N}}\) and \(\mathrm{{S}_{D}}\) the \TeX{} engine knows if the glues need to stretch, shrink or assume their natural width.
We noted that during box construction \TeX{} engines keep a running total of the amount of stretch and shrink---for each glue order---seen in the list of items being packaged into a box. To select which glues will provide the necessary stretching or shrinking, a \TeX{} engine checks the values of total stretch or total shrink \textit{for each glue order} and picks the highest order that that has a non-zero value: that is how the glues are selected for each box---the highest glue order wins the race. For example, if a particular box needed to stretch its glue and the sum total of all stretch components was as follows:\vskip5pt
\tabskip=5mm
\halign{#&\hfil#\hfil&#\hfil\cr
\textbf{Units}&\textbf{Glue order}&\textbf{Total (stretch)}\cr
\texttt{pt}, \texttt{mm},\texttt{in}, \textit{etc.}&0&50 (\texttt{pt})\cr
\texttt{fil}&1&5 (\texttt{fil})\cr
\texttt{fill}&2&0 (\texttt{fill})\cr
\texttt{filll}&3&1 (\texttt{filll})\cr}
\TeX{} would choose the glue(s) with \texttt{filll} because it is the highest glue order that has a non-zero total. In this box, only glues with glue order 3 (\texttt{filll}) will be ``activated'' to take part in stretching: all glue(s) of a lower glue order will adopt a size determined by their natural width.
Further details and a fully worked example can be found in the Overleaf article \href{https://www.overleaf.com/learn/latex/Articles/How\_TeX\_Calculates\_Glue\_Settings\_in\_an\_\%5Chbox}{\color{blue}How \TeX{} Calculates Glue Settings in an \texttt{\slash hbox}}
\section{Boxes and their glue settings}
Once constructed, each box will have a certain width, height and depth. In addition, just after creating a new \verb|\hbox|, \verb|\vbox|, \verb|\vtop| or \verb|\vcenter| the \TeX{} engine will assign three glue-related parameter values to that freshly-minted box to record the stretching or shrinking behaviour of any glue within the list of items ``inside'' that box. Those parameters are used to ``set'' the glue in that box: making the glues occupy a particular amount of space by stretching or shrinking to the values necessary to arrange the items as required.
Those three parameters are called the \textit{glue set ratio}, \textit{glue order} and \textit{glue sign}.
\begin{itemize}
\item \textit{glue order}: An integer which determines the order of glue to be activated inside this box---finite glue (order 0) or one of the infinite glues fil, fill or fill with glue order 1, 2 or 3 respectively.
\item \textit{glue sign}: An integer (0, 1 or 2) which determines whether glues will neither shrink or stretch (0), only stretch (1) or only shrink (2)
\item \textit{glue set ratio}: A floating point number which is a multiplier applied to glue components and determines by how much each glue will stretch or shrink.
\end{itemize}
The glue order and stretching/shrinking of glue is utilized when a box is physically written out to the typeset page in the PDF file, enabling the box contents to be arranged on the output page as required.
Trivia: Calculation of glue set ratio is the only place that \TeX{} engines use floating point calculations.
\subsection{\textit{glue order}: an important parameter}
The \textit{glue order} assigned to a box is key to understanding the origin of \ufull and \ofull box warnings. The glue order determines which glues in a box are going to participate in any stretching or shrinking activities. For example, if a particular box is assigned a glue order of 2 it means that only those glues with stretch or shrink components in units of \texttt{fill} are to be used---all glues with a lower glue order do not take part and will occupy an amount of space determined by their natural width: the value of their \textit{normal component}. The \textit{glue sign} value determines whether those \texttt{fill} glue components are going to stretch or shrink.
\subsection{Glues and quality control: underfull or overfull?}
During creation of boxes destined to contain your typeset material, a \TeX{} engine examines the resulting box's ``quality'' by checking how much ``certain glues'' (see below) need to stretch or shrink to produce your box. The result of those checks are reported to you using \ufull or \ofull box diagnostic messages which tell you the box's ``\bness\unskip'' value, or an amount by which your content exceeds the given size (\texttt{<dimension>}) of that box. Note that is only during the initial box-creation process that \TeX{} engines issues \ufull or \ofull box diagnostic messages.
Recall that within each box \textit{only} those glues which have a particular \textit{order}---as identified by the box's \textit{glue order}---will participate in stretching or shrinking to align or arrange the contents of the box. The key point to note is that \TeX will \textit{only} report \ufull or \ofull boxes if, during the box's creation, the \TeX{} engine identifies that glues of order 0 need to be used for stretching or shrinking--the ``certain glues'' referred to above is glues of order 0.
If a box has glue order 1, 2 or 3 it means that glues containing infinite shrink or stretch components (in units of \texttt{fil}, \texttt{fill} or \texttt{filll}) are to be used (or ``activated'') for stretching or shrinking. Consequently, \TeX{} \textit{will not} report \ufull or \ofull warnings for such boxes because infinite glues are \textit{specifically designed} to stretch or shrink to any desired value---\TeX{} will not consider them to be \ufull or \ofull\unskip.
You can typeset the \bness value for the most recently constructed box by wri\-ting \verb|\the\badness| \textit{immediately after} that box is created. As soon as another box is constructed the \bness value for the previous box is lost because it is not stored or ``attached'' to the box which produced it. \bness is a transient value calculated on-the-fly during box construction---but only if \TeX{} needs to do so (i.e., when the glue order is 0).
``Box-fitting'' processes are an intrinsic aspect of all \TeX{} engines---such as during linebreaking where individual lines of a typeset paragraph are converted to an \verb|\hbox| of a fixed width, or the content of each page is fitted into a \verb|\vbox| of a fixed height. During linebreaking or page construction \TeX{} engines will report \ufull or \ofull box diagnostic messages which result from boxes that you have not specified explicitly. At first, it can be confusing to see numerous \ufull or \ofull \verb|\hbox| warnings which arise from linebreaking processes and calculations, not from boxes that \textit{you} have directly specified in your \TeX{} or \LaTeX{} code. Hopefully, this project will help you to better understand \TeX{} engines' \ufull or \ofull box messages, irrespective of their source.
\subsection{Using an \texttt{\string\hbox} to explore badness}
To obtain the width of a piece of text it is customary to put that text in an \verb|\hbox| and measure the width of that \verb|\hbox| using \verb|\the\wd\<boxnumber>|. For example, if we do
\verb|\setbox100=\hbox{Overleaf}\the\wd100|
\TeX{} informs us that the word ``Overleaf'' has width \the\ol{} (using the current font). What happens if we tell \TeX{} to fit ``Overleaf'' into an \verb|\hbox| that is just a tiny bit wider, say 0.00989pt, than we actually need to accommodate ``Overleaf''. We will ask the \TeX{} engine to use an \verb|\hbox| of width \the\olmax{} compared to \the\ol{}, which is actual width of the text. That's really not so bad... is it?
If we write
\verb|\setbox100=\hbox to |\texttt{\the\olmax}\verb|{Overleaf}|
\setbox100=\hbox to \the\olmax{Overleaf} we get a badness value of \the\badness!
\textit{What} happened? Why did \TeX{} report a badness value of 10000 for such a tiny increment in the width of the \verb|\hbox|? The reason is that our \verb|\hbox| has absolutely no glue that \TeX{} can use to absorb even the tiny extra width of 0.00989pt so it assigns a value of 10000 to the badness of \verb|\hbox100|.
Conversely, if we tell \TeX{} to fit ``Overleaf'' into an \verb|\hbox| that is 0.00989pt \textit{narrower} than we actually need to accommodate ``Overleaf'', i.e.,
\verb|\setbox100=\hbox to |\texttt{\the\olmin}\verb|{Overleaf}|
then we obtain a badness value of \setbox100=\hbox to \the\olmin{Overleaf}\the\badness---which is \textit{significantly} higher than \bness of 10000 reported for \verb|\hbox| 100 when it was 0.00989pt too wide! In essence, \TeX{} engines \textit{deplore} the use of boxes for which the content is too large for a given box and there is insufficient glue to provide the amount of shrinking required to absorb the extra space required by your content---hence \TeX{} engines assign the special \bness value of 1000000.
Trivia: Inside the source code of \TeX{} the \bness value of 10000 is referred to as ``infinitely bad'' but no name is given to the \bness value of 1000000---maybe that value is so bad that it leaves \TeX{} speechless...
\subsection{Adding a small but \textit{finite} glue}
\subsubsection{But first: \texttt{\string\hbadness}}
\TeX{} engines provide the \verb|\hbadness<threshold>| command which you can use to set a minimum \texttt{<threshold>} value for reporting the \bness value of \verb|\hbox|es. Any \verb|\hbox| whose \bness below (or equal to) the \texttt{<threshold>} (integer) value will not be reported as \ufull\unskip. Here, we'll assume \LaTeX's standard \verb|\hbadness| value of 1000---which \LaTeX{} sets using \verb|\hbadness=1000|. Note that the ``\texttt{=}'' sign is optional, you can also set \verb|\hbadness| by writing \verb|\hbadness1000|. There is an equivalent setting for \verb|\vbox|es: \verb|\vbadness|.
The current value of \verb|\hbadness| can be typeset using \verb|\the\hbadness|: the current value of \verb|\hbadness| is \the\hbadness.
\subsubsection{... back to the glue}
We'll build on our earlier example \verb|\hbox| which was 0.00989pt \textit{wider} than we need to accommodate “Overleaf”. Here, we'll add some glue to our \verb|\hbox| which provides the \TeX{} engine with some ``flexible space'' it can use to absorb the additional 0.00989pt of space inside our \verb|\hbox|.
Assuming \LaTeX's standard \verb|\hbadness| value of 1000, we can add a tiny amount of flexible, but \textit{finite}, \verb|\hskip| glue to our \verb|\hbox|---\verb|\hskip0pt plus|\(\delta\) where we'll vary \(\delta\) from 0.01pt down to 0.002pt:
\verb|\setbox100=\hbox to |\texttt{\the\olmax}\verb|{Overleaf\hskip0pt plus|\(\delta\)\verb|}|
In the following examples we see very different values of \bness resulting from the values of \(\delta\), even though differences in the glue are extremely small:
\setfinite{0.01}
\setfinite{0.007}
\setfinite{0.006}
\setfinite{0.005}
\setfinite{0.004}
\setfinite{0.003}
\setfinite{0.0025}
\setfinite{0.002}
The last 4 examples with \(\delta=0.004\textrm{pt}, 0.003\textrm{pt}, 0.0025\textrm{pt}, 0.0020\textrm{pt}\) all produce \ufull box warnings with increasingly large \bness values---even though the differences in \(\delta\) are minute. Note that between \(\delta=0.003\textrm{pt}\) and \(\delta=0.002\textrm{pt}\) the \bness rises rapidly---but that is due to the \bness calculation having a ``discontinuity'' designed by Knuth: any calculated \bness above \(2^{13} = 8192\) is reported as 10000 which \TeX{} considers ``infinitely bad''.
If we now set \verb|\hbadness=1509| (or use \verb|\hbadness1509|) and repeat one of our experiments with \(\delta=0.004\)pt our box still has the same \bness value but is no longer reported as \ufull\unskip:
{\hbadness=1509 \setfinite{0.004}}% temporarily set \hbadness inside a group
\subsection{Infinitely flexible glue}
In the above examples, \TeX{} reported non-zero badness values because we had used a small, but \textit{finite}, glue after ``Overleaf''\kern-0.75pt. If we repeat these experiments but switch to using \textit{infinite} glue, an \ufull box is not produced and the badness is always 0. If we now add tiny amounts of flexible \verb|\hskip| glue but this time with \textit{infinite} flexibility---\verb|\hskip0pt plus|\(\delta\) where we'll vary \(\delta\) from 0.01\texttt{fil} down to 0.002\texttt{fil}---we see \bness is 0 in every case.
\verb|\setbox100=\hbox to |\texttt{\the\olmax}\verb|{Overleaf\hskip0pt plus|\(\delta\)\verb|}|
%\hbadness=0
\setinfinite{0.01}
\setinfinite{0.007}
\setinfinite{0.006}
\setinfinite{0.005}
\setinfinite{0.004}
\setinfinite{0.003}
\setinfinite{0.0025}
\setinfinite{0.002}
The reason for the different behaviour of glue with \textit{finite} stretch (e.g., \(\delta=0.01\textrm{pt}\) \textit{etc.}), compared to glue with \textit{infinite} stretch (e.g., \(\delta=0.01\texttt{fil}\) \textit{etc.}) is that \TeX{} engines \textit{only} calculate \bness for boxes which have to stretch or shrink glue with order 0. Here, the glue in the \verb|\hbox| has infinite stretch in units of \texttt{fil}, so it will always have \bness of 0 and is not considered to be \ufull\unskip---\texttt{fil} glue is designed to stretch/shrink to any size.
\setbox100=\hbox to 34.98978pt{Overleaf\hskip0pt plus0.1pt\hskip0pt plus0.1fil}
\subsection{Calculating badness}
If the amount of available \textit{finite} glue is non-zero, \TeX{} calculates \bness using an \textit{approximation} to the formula
\[\bness=100\times\left(\frac{\textrm{amount of space to fill or absorb}} {{\textrm{amount of finite glue available}}}\right)^3\]
A \TeX{} engine is satisfied with an approximate value for \bness because it is used as an heuristic---it isn't necessary to calculate an \textit{exact} value, allowing \TeX{} to keep the calculation fast and efficient. Note that in the source code of the \TeX{} program Knuth writes
\begin{quote}
``Any badness of \(2^{13}\) or more is treated as infinitely bad, and represented by 10000.''
\end{quote}
where \(2^{13}=8192\). Knuth also observes that his approximation to computing the value of the \bness function
\begin{quote}
``... is capable of
computing at most 1095 distinct values, but that is plenty.''
\end{quote}
Taking one of our examples above, where we had to fill an additional space of 0.00989pt but only had 0.004pt of (\textit{finite}) glue available, \TeX{} reported a \bness value of 1509 but an exact calculation of \bness would be
\[\bness=100\times\left(\frac{{0.00989}}{{0.004}}\right)^3 = 1511.502...\]
but 1509 is close enough for \TeX's purposes.
\subsection{Over-stretching but never over-shrinking of \textit{finite} glue}
Let's use \verb|\hbox to 16pt{a<glue>b}| with various values of \verb|<glue>| between a and b.
Using \verb|\setbox101=\hbox{ab}\the\wd101| we can determine that the width of the text typeset ab (allowing for kerns) is \setbox101=\hbox{ab}\tempboxdim=\wd101\relax\the\tempboxdim.
Just out of interest, we can check for kerning by calculating the width of the individual characters and testing if their combined individual widths has the same value as the \verb|\hbox{ab}|---i.e., when they are typeset together. Width of character `a' is given by \verb|\setbox101=\hbox{a}\the\wd101|= \setbox101=\hbox{a}\the\wd101, and the width of `b' is given by \verb|\setbox101=\hbox{b}\the\wd101|=\setbox101=\hbox{b}\the\wd101, giving a combined width of 9.49999pt---which, up to 4th decimal place, is the same as the width when typeset together: \the\tempboxdim, so we can assume there is no kerning. The difference of 0.0001pt is a rounding error---it would produce rather invisible kerning!
\subsubsection{Over-stretching of \textit{finite} glue}
\verb|\hbox to 16pt{a b}| produces \hbox to 16pt{a b} with \bness=\the\badness. But where did the stretchy glue come from? The answer is the space character between a and b: \TeX{} converted that space character to a ``glob'' of interword glue---the value of which is specific to each font (and font size). Here, \TeX{} had to over-stretch the glue (arising from the space character) resulting in a \bness value of 3271.
\verb|\hbox to 16pt{a\hskip 1pt plus 2pt b}| uses an explicit \verb|\hskip| glue with only \texttt{2pt} of stretch (via the \texttt{plus 2pt}) and produces a result similar to using a space character: it results in \hbox to 16pt{a\hskip 1pt plus 2pt b} with \bness\unskip=\the\badness.
We can work out why. The desired width of our box is 16pt but what is the natural width of the components? We know that the width of `ab' is 9.49999pt but what about the glue? Recall that the glue's normal component contributes to the natural width, so a glue specified using \verb|\hskip 1pt plus 2pt| will contribute 1pt to the natural width of the box, giving a total natural width of \(9.49999+1=10.49999\)pt, meaning that the glue has to stretch to fill \(16-10.49999=5.50001\)pt of space. Our glue has a total 2pt of finite stretch but needs to fill up 5.50001pt of space in this box. Applying our \bness calculation
\[\bness=100\times\left(\frac{\textrm{amount of space to fill or absorb}}{{\textrm{amount of finite glue available}}}\right)^3\]
we can plug in our values to calculate
\[\bness=100\times\left(\frac{5.50001}{2}\right)^3 = 2079.698...\]
but 2073 is close enough for \TeX's purposes.
Because we used small finite glue values---from the interword space or the 2pt in our explicit \verb|\hskip| glue---\TeX{} was forced to significantly over-stretch those finite glues, which resulted in the \bness values of 3271 and 2073.
If we add more stretch by using \verb|\hbox to 16pt{a\hskip 1pt plus 5pt b}| the \verb|\hskip| glue has \texttt{5pt} of stretch (via the \texttt{plus 5pt}) and produces the result \hbox to 16pt{a\hskip 1pt plus 5pt b} with \bness\unskip=\the\badness. Again, we can check this by calculating
\[\bness=100\times\left(\frac{5.50001}{5}\right)^3 = 133.100...\]
which \TeX{} approximates to a \bness value of 132. The glue has much more stretch with which to fill the box, hence the smaller \bness value.
\subsubsection{No over-shrinking of \textit{finite} glue}
The previous example showed that a \TeX{} engine will, if necessary, over-stretch finite glues by far more that we specify (recommend). In the above example we specified \verb|\hskip 1pt plus 2pt|, allowing 2pt of finite stretch but the \TeX{} engine ignored our \textit{recommendation} because it was forced to stretch it to 5.50001pt to fill the \verb|\hbox|. However, although \TeX{} engines will over-stretch finite glue, \textit{they will not over-shrink} finite glues.
\subsubsection{An example with larger font size}
To make this example easier to discuss we'll use a very large point size of 50pt to display our text (also in blue):
\verb|\setbox100=\hbox{{\color{blue}\fontsize{50}{50}\selectfont ab}}|\vskip5pt
The width of \setbox100=\hbox{{\color{blue}\fontsize{50}{50}\selectfont ab}}\copy100{} is \the\wd100\relax.
Next we'll use \verb|\hbox to 40pt{a\hskip 0pt minus 7.49997pt b}| which asks the \TeX{} engine to typeset an \verb|\hbox| that is exactly 7.49997pt smaller than the width of the text `ab' (typeset at 50pt).
It produces\hbox to 40pt{{\color{blue}\fontsize{50}{50}\selectfont a\hskip 0pt minus 7.49997pt b}} with \bness\unskip=\the\badness. We used an explicit \verb|\hskip| glue with only \texttt{7.49997pt} of shrink (via the \texttt{minus 7.49997pt}). \TeX{} uses that \texttt{minus 7.49997pt} to ``squeeze'' our characters ab into this box---but only just, resulting in a ``perfect fit \bness\unskip'' value of 100. To understand the \bness value of 100 we only need to refer back to the calculation of \bness\unskip:
\[\bness=100\times\left(\frac{\textrm{amount of space to fill or absorb}}{\textrm{amount of finite glue available}}\right)^3\]
In our box we had 7.49997pt as the amount of finite glue available and 7.49997pt of space to fill (or in our case, absorb), hence:
\[\bness=100\times\left(\frac{7.49997}{7.49997}\right)^3 = 100\times (1)^3 = 100\]
What happens if we now reduce the width of the \verb|\hbox| to, say, 30pt via
\verb|\hbox to 30pt{a\hskip 0pt minus 7.49999pt b}|
asking \TeX{} to typeset an \verb|\hbox| that is exactly 17.49997pt smaller than the width of the text ab in 50pt type. We still use an explicit \verb|\hskip| glue with only \texttt{7.49997pt} of shrink (via the \texttt{minus 7.49997pt}) but this time the requested \verb|\hbox| is now 17.49997pt too small: will \TeX{} over-shrink our glue of 7.49997 to absorb the 17.49997pt---squashing the a and b closer together to absorb the extra 10pt reduction in the size of \verb|\hbox| we have requested?
It produces\hbox to 30pt{{\color{blue}\fontsize{50}{50}\selectfont a\hskip 0pt minus 7.49997pt b}}with \bness\unskip=\the\badness.
\TeX{} honoured our request for a 30pt wide \verb|\hbox| as you can see because the text of ``with \bness...'' overlaps the blue text. However, the ab in the 30pt-wide \verb|\hbox| are typeset with exactly the same overlap as the 40pt-wide \verb|\hbox|: the glue between them was not shrunk by more than the 7.49997pt because \TeX{} simply will not shrink finite glue below the minimum size stipulated in its specification. This is, of course, unlike the finite stretch component of glues which \TeX{} will happily over-stretch whenever necessary. Here, the \verb|\hskip 0pt minus 7.49997pt b}| will absorb up to 7.49997pt but no more.
We see that \TeX{} has reported \texttt{Overfull \string\hbox (10.0pt too wide) detected at line...} as you can also see in Overleaf. It also assigned this box the ``special maximum'' \bness of 1000000 because there was insufficient shrinkable glue to absorb the excess space of 10pt.
If we increase the shrink to 17.49997pt and write
\begin{verbatim}
\hbox to 30pt{{\color{blue}\fontsize{50}{50}\selectfont
a\hskip 0pt minus 17.49997pt b}}
\end{verbatim}
we duly get\hbox to 30pt{{\color{blue}\fontsize{50}{50}\selectfont a\hskip 0pt minus 17.49997pt b}}with \bness\unskip=\the\badness. A 30pt \verb|\hbox| with characters `ab' overlapping.
So, \TeX{} will happily overlap the characters ab but \textit{only} if there is sufficient glue shrink to absorb the excess space occupied by the text ab---here, accommodating a 30pt box size request by shrinking the space between a and b by 17.49997pt.
\subsubsection{Trying a tiny bit of infinite shrink}
We'll try reducing the \verb|\hbox| right down to 20pt wide by using a tiny amount of infinitely shrinkable glue (\texttt{0.001fil}) to see what happens.
\begin{verbatim}
\hbox to 20pt{{\color{blue}\fontsize{50}{50}\selectfont
a\hskip 0pt minus 0.001fil b}}
\end{verbatim}
we duly get\hbox to 20pt{{\color{blue}\fontsize{50}{50}\selectfont a\hskip 0pt minus 0.001fil b}}with \bness\unskip=\the\badness.
It has badness=0 \textit{and} is no longer reported as being overfull. The difference in behaviour arises because we used infinitely shrinkable glue (\texttt{0.001fil}).
\setlength{\fboxsep}{-\fboxsep}
%\fbox{\hbox to 20pt{{\color{blue}\fontsize{50}{50}\selectfont a\hskip 0pt minus 0.001fil b}}}
We can go even further and create a zero width \verb|\hbox|:
\begin{verbatim}
\hbox to 0pt{{\color{blue}\fontsize{50}{50}\selectfont
a\hskip 0pt minus 0.001fil b}}
\end{verbatim}
we duly get\hbox to 0pt{{\color{blue}\fontsize{50}{50}\selectfont a\hskip 0pt minus 0.001fil b}} with \bness\unskip=\the\badness.
The \texttt{0.001fil} glue has shrunk to absorb the entire width of the box, a total of 47.49997pt. In effect, it typesets the `a' then ``backspaces'' by 47.49997pt and typesets the `b'.
We'll fake this (using red text) with a \verb|\kern-47.49997pt|:
\begin{verbatim}
\hbox{{\color{red}\fontsize{50}{50}\selectfont
a\kern-47.49997pt b}}
\end{verbatim}
Result\hbox{{\color{red}\fontsize{50}{50}\selectfont
a\kern-47.49997pt b}} typeset with \verb|\kern-47.49997pt|.
Result\hbox to 0pt{{\color{blue}\fontsize{50}{50}\selectfont a\hskip 0pt minus 0.001fil b}} typeset with \texttt{0.001fil} glue shrink.
To see exactly what is going on, let's look at the placement of text.
Width of large red \setbox100=\hbox{{\color{red}\fontsize{50}{50}\selectfont
a}}\copy100{} is \the\wd100
Width of large red \setbox101=\hbox{{\color{red}\fontsize{50}{50}\selectfont
b}}\copy101{} is \the\wd101
When \TeX{} typesets \hbox to 0pt{{\color{red}\fontsize{50}{50}\selectfont a\hskip 0pt minus 0.001fil b}} it first moves forward by \copy100{} (+22.84998pt) then moves back by \(-\)47.49997pt (via \verb|\kern-47.49997pt| or \texttt{0,001fil}) then typesets \copy101{} moving forward again by (+24.65pt), giving a total movement of 0pt (within \TeX's rounding errors!).
\subsection{Understanding \texttt{\string\hfuzz}}
Here, we'll take a look at the primitive (built-in) \TeX{} command \verb|\hfuzz| which can be used to influence \TeX's decision as to whether it considers a box to be \ofull and reported as such.
Let's go back to our first example, \verb|\setbox100=\hbox{Overleaf}\the\wd100| where \TeX{} informs us that (using the current font) the word ``Overleaf'' has width \setbox100=\hbox{Overleaf}\the\wd100.
\hbadness1000
Putting ``Overleaf'' into an \verb|\hbox| that is exactly 34pt:
\texttt{\string\setbox100=\string\hbox{} to 34pt\{Overleaf\}}
\setbox100=\hbox to 34pt{Overleaf} we get a corresponding badness value of \the\badness{} and a message \texttt{Overfull \string\hbox{} (0.97989pt too wide) detected at line...}, which is not surprising.
\hfuzz=0.97989pt If we now set \verb|\hfuzz=0.97989pt| and repeat the experiment \texttt{\string\setbox100=\string\hbox{} to 34pt\{Overleaf\}} we still get an \verb|\hbox| with \bness of \setbox100=\hbox to 34pt{Overleaf}\the\badness{} but \verb|\hbox| 100 is no longer reported as \ofull.
In the next section we'll take a closer look at \verb|\hfuzz|.
\subsubsection{Adding shrink glue: effect on Overfull warning and \texttt{\string\hfuzz}}
\noindent When \TeX{} reports an \ofull \verb|\hbox| or \verb|\vbox| the amount (in points, pt) by which the \verb|\hbox| is too wide or the \verb|\vbox| is too high is calculated using difference between the excess width and the total amount of zero order shrink glue in the box. We'll work through some examples to explain this in more detail.
%\hbadness=500
\noindent If we write \verb|\hbox to 20pt{\kern25pt}\the\badness| to create an empty box we see space\hbox to 20pt{\kern25pt} and badness of \the\badness. The \texttt{\string\hbox} is set to 20pt but the \texttt{\string\kern} of 25pt is 5pt wider than the \texttt{\string\hbox} and there is no shrinkable glue available to absorb the 5pt. As a result, \TeX{} sets the ``special badness'' value of 1000000 and reports \texttt{Overfull \string\hbox (5pt too wide) detected at line...}.
If we now add some flexible glue by writing
\verb|\hbox to 20pt{\kern25pt\hskip0pt minus1pt}|
we see some empty space: \hbox to 20pt{\kern25pt\hskip0pt minus1pt} and the same ``special badness'' of \the\badness---also due to insufficient shrinkable glue being available. However, \TeX{} now reports \texttt{Overfull \string\hbox (4pt too wide) detected at line...}. The box is no longer 5pt too wide but 4pt because \TeX{} has accounted for the 1pt of flexible glue in its report of an \texttt{Overfull \string\hbox}.
Going further, if we now add more flexible glue by writing
\verb|\hbox to 20pt{\kern25pt\hskip0pt minus1pt\hskip0pt minus2.5pt}|
there is a total flexible glue of \(\textrm{1pt} + \textrm{2.5pt}=\textrm{3.5pt}\). Now, the box is only \(\textrm{5pt}-\textrm{3.5pt}=\textrm{1.5pt}\) too wide, as you will also see reported by Overleaf.
\hbox to 20pt{\kern25pt\hskip0pt minus1pt\hskip0pt minus2.5pt}
\includegraphics[scale=0.85]{overfull.png}
This example demonstrates that the amount by which a box is reported as \ofull takes into account the total finite shrink glue available in the box---i.e., it is not just the excess width of the non-glue content. In our example, the \texttt{\string\kern25pt} exceeded the desired width of 20pt by 5pt. The total flexible shrink is now capable of absorbing 3.5pt but that still leaves 1.5pt that cannot be absorbed by shrinking the glue---1.5pt is the amount by which the \texttt{\string\hbox} is reported as \ofull\unskip. Recall that \TeX{} engines \textit{will not} shrink finite glue below the minimum size specified by its shrink component value.
If we want to stop \TeX{} reporting this \texttt{Overfull \string\hbox} we need to set \texttt{\string\hfuzz=1.5pt}, after which \TeX{} will no longer report this space \hfuzz=1.5pt\hbox to 20pt{\kern25pt\hskip0pt minus1pt\hskip0pt minus2.5pt} created via an empty \verb|\hbox| as being \ofull\unskip. \verb|\vfuzz| is the equivalent command for \verb|\vbox|es.
\end{document}
\begin{eqnarray}
\textrm{badness}&=&100\times\left(\textrm{amount of space to fill or absorb}\over {\textrm{amount of finite glue available}}\right)^3\\[3pt]
&=&100\times\left(10\textrm{pt}\over
\textrm{5pt}\right)^3\\[0pt]
&= &100\times (2)^3\\
&= &800\\
\end{eqnarray}
Although \TeX{} accounts for totals of the glue stretch/shrink components, it ignores them for the purpose of computing a box's natural size---the ``natural width'' for an \verb|\hbox| or the ``natural height'' for a \verb|\vbox|.
If a user has asked \TeX{} to use a specific size for that box then \TeX{} needs to be concerned with how ``well'' your content will actually fit into that box. If you don't specify a size for the box the \TeX{} engine will simply create a box as big as it needs---it will not need to stretch or shrink any glue, they just adopt their ``normal'' size.
When a \TeX{} engine typesets some content by fitting it into box of a fixed size (\texttt{<dimension>}), such as \verb|\hbox to <dimension>{...}| or \texttt{\string\vbox{} to <dimension>}\verb|{...}| the \TeX{} engine tries to make the content ``fit'' into that box---as best it can. Whilst typesetting the material, a \TeX{} engine calculates a total for the ``natural size'' of all items in your content, compares that to the given \texttt{<dimension>} value and determines how much it needs to stretch or shrink glues in that box---in order to make your content ``fit'' into a space of size \texttt{<dimension>}.
\TeX{} \textit{did not} shrink the glue by more than 2pt to absorb the 2.55559pt we needed. We might think that \TeX{} would further shrink the glue to squeeze the characters ab together, absorbing the 2.55559pt shortfall. In addition, we might also assume that the badness calculation formula would produce a badness value of
\[\textrm{badness}=100\times\left(2.55559\over 2\right)^3 \approx208\]
But no, the badness is reported as the maximum possible value \TeX{} provides for: a huge 1000000---here, \TeX{} did not use the badness calculation. Instead, when processing this \verb|\hbox| \TeX{} recognized it was an instance where the glue is finite and the amount of shrink it has (here, 2pt) is insufficient to absorb the shortfall in the space we provided (here, 2.55559pt). As a result, \TeX{} just assigned its maximum badness value of 1000000, flagging this as ``very bad indeed'' and refusing to shrink the glue any further than the 2pt we assigned to it (to be precise, it sets the glue ratio to 1.0).
\textbf{Note}: The above badness calculation isn't quite the whole story on how \TeX{} assigns badness to every box in all circumstances, but it gives a useful insight to what is going on inside \TeX{}.
Note that even if we set \verb|\hfuzz=0.97988pt|, a difference of 0.00001pt \TeX{} will once again complain \texttt{Overfull \string\hbox (0.41675pt too wide) detected at line...}
\hfuzz=0.979884pt\texttt{\string\hfuzz=0.97988pt\string\setbox100=\string\hbox{} to 34pt\{Overleaf\}}\setbox100=\hbox to 34pt{Overleaf}