# Strange behaviour of \sbox

This question consists of two related parts. I already solved the original problem but still don't understand where it came from or why the solution works.

I use the subcaption package so I can place two subtables aside each other. Each subtable requires the intended width so I additionally use a savebox to first create the table, then measure the width of the box in order to get the optimal subtable width and last but not least print the contents of the box.

\newsavebox{\tablebox}
\sbox{\tablebox}{
\begin{tabular}{ccc}
1st & 2nd & 3rd \\
1   & 2   & 3   \\
4   & 5   & 6   \\
7   & 8   & 9   \\
\end{tabular}
}
% create fitting subtable
\begin{subtable}{\wd\tablebox}
\usebox{\tablebox}
\caption{Subable}
\end{subtable}


However, pdflatex started to give me the following warning

Underfull \hbox (badness 10000) in paragraph at lines 24--25

where 24 is the line the \usebox is at.

This makes no sense because the subtable should have the exactly right width. Through trial and error I found out that the warning only occurs when all of the following three conditions hold:

1. The given construct is used in the appendix (i.e. after \appendix).
2. The cleveref package is loaded.
3. There is no % at the end of the \usebox line.

I do not understand why any of these has an influence at all...

My second question came up while trying to located the error. I replaced the second table with a \rule of the same size, or at least that is what I tried to achieve. While the width ({\wd\tablebox}) looks fine to me the height of the replacement ({\ht\tablebox}) is wrong. It is much too narrow. Why?

Finally, a MWE that demonstrates all described effects. Please note that there is no warning for the actual table due to the % but there is one for the replacement \rule with the same width:

\documentclass[english]{scrreprt}
\usepackage{babel}
\usepackage{subcaption}
\usepackage{cleveref}

\begin{document}
\appendix

\newsavebox{\tablebox}
\begin{table}
% prepare table
\sbox{\tablebox}{
\begin{tabular}{ccc}
1st & 2nd & 3rd \\
1   & 2   & 3   \\
4   & 5   & 6   \\
7   & 8   & 9   \\
\end{tabular}
}
% create fitting subtable
\begin{subtable}{\wd\tablebox}
\usebox{\tablebox}% no warning for this table
\caption{Left Table}
\end{subtable}
%
\hspace{2ex}
%
% create fitting subtable
\begin{subtable}{\wd\tablebox}
\rule{\wd\tablebox}{\ht\tablebox}
\caption{Right Table}
\end{subtable}

\caption{Two Tables}
\end{table}

\end{document}

• Regarding the height of the table: The height is not the same as the total height (=height + depth). The baseline is in the middle of the table. – Ulrike Fischer Jul 14 '13 at 15:28
• @Ulrike Fischer Thanks for pointing this out. I'm still not too familiar with the box concept of TeX, so it simply did not occur to me that the table's baseline would be in the middle. – TableKing Jul 14 '13 at 17:02
• You have superfluous spaces in your \sbox: Add % at the end of the first line (after the {) and after \end{tabular}. – Martin Schröder Jul 14 '13 at 17:42
• @Martin Schröder These extra spaces should also be accounted for when the width of the resulting box is taken, so they are not related to my initial question. Nevertheless, thank you very much for your advise. I still don't understand why I sometimes need to comment line endings and sometimes I don't (like the line starting the tabular environment). Is this related to "horizontal" vs. "vertical" mode I keep reading about in various questions? – TableKing Jul 14 '13 at 17:57
• If you want a subtable which have the width of its contents, why don't you simply use \subcaptionbox? – Axel Sommerfeldt Jul 15 '13 at 18:52

## Underfull \hbox

The problem is package cleveref that sets an unwanted space. The line starts with an object that fills the complete line. Usually the following \caption calls \par that ends the previous paragraph and removes the latest space in the process. However, if there is a space in \caption by some code of \cleveref before, then only one space gets removed a the end of the paragraph. The other space causes a line break and it is removed at the start of the new line. But the new line has already started and is quite empty and LaTeX complains with Underfull \hbox.

An example for a plain TeX model:

\showboxdepth=1000
\tracingonline=1
\setbox0=\vbox{%
\noindent
\vrule height 1pt width \hsize\relax
\space\space
}
\showbox0
\csname @@end\endcsname\end


TeX prints as warning:

Underfull \hbox (badness 10000) in paragraph at lines 5--8

\hbox(0.0+0.0)x469.75499
.\glue(\rightskip) 0.0


And box 0 contains:

> \box0=
\vbox(13.0+0.0)x469.75499
.\hbox(1.0+0.0)x469.75499
..\rule(1.0+*)x469.75499
..\glue(\rightskip) 0.0
.\penalty 300
.\glue(\baselineskip) 12.0
.\hbox(0.0+0.0)x469.75499
..\glue(\rightskip) 0.0

! OK.
l.9 \showbox0


Inside the vertical box (\vbox) \noindent starts a new paragraph without indentation. At the end of the box TeX ends the paragraph (implicit \par). Then TeX removes a last space (implicit \unskip) to remove the last space. In this case the second \space and adds the space \parfillskip instead. Then the paragraph before breaking contains:

<\vrule ...> <\space> <\hskip\parfillskip>


The rule fills the complete line, thus TeX breaks the line after the rule before the space. At the start of the new line spaces are removed, thus both <\space> and <\hskip\parfillskip> are removed. The line only contains the automatically inserted \rightskip that does not contain stretchable component, see the box of the warning:

\hbox(0.0+0.0)x469.75499
.\glue(\rightskip) 0.0


Thus the box does not contain anything that would fill the line, the line is underfull.

Workarounds:

• Commenting the line end (you have already found it).
• \par at the end, then subsequent spaces are ignored in vertical mode.
• Loading cleveref via:

\edef\RestoreEndlinechar{\endlinechar=\the\endlinechar\relax}
\endlinechar=-1 %
\usepackage{cleveref}
\RestoreEndlinechar


(Spaces in warning messages might be missing.)

The unwanted line end of cleveref.sty 2013/03/22 v0.18.9 is located in the definition of \refstepcounter@noargs, line 196 and 242.

## Incomplete rule

You have simply forgotten, that a box can have a depth. The default behavior of a tabular is the base line in the middle. But also with option [b] you can have a usually small depth, if the last element is a normal row and not a \hline.

\raisebox can help to create a depth:

    \raisebox{-\dp\tablebox}{%
\rule{\wd\tablebox}{\dimexpr\ht\tablebox+\dp\tablebox\relax}%
}%

• I have written a bug report to Toby Cubitt, the author of package cleveref. – Heiko Oberdiek Jul 14 '13 at 15:47
• Thanks a lot for the explanation! I still don't understand why the extra line (the underfull/empty one) does not show up in the PDF - the visual result is always the same, whether I comment the line ending or not. – TableKing Jul 14 '13 at 16:50
• I have uploaded a new pre-release version of cleveref to my web site, which contains a fix for these line-end bugs. This new version will make its way to CTAN eventually. – Toby Cubitt Jul 14 '13 at 17:51
• @TobyCubitt: Wow, thanks for the very fast response and fix. – Heiko Oberdiek Jul 14 '13 at 20:47
• @TableKing: I tried to explain the underfull \hbox with more details in the updated answer at least. Further readings: "The TeXbook" by Donald E. Knuth or "TeX by Topic" by Victor Eikjhout. – Heiko Oberdiek Jul 14 '13 at 21:06