The original answer from list of figures and tables when there are no figures or tables?, does not give me any bad box warnings:
\def\wheninteresting#1{%
\setbox0\vbox{#1}%
\ifdim\ht0>35pt
\unvbox0
\fi}
However, an updated version from How to dynamically calculate the height of an empty list of things?, which uses newsavebox keeps giving me several bad boxes warnings
\newsavebox{\boxAforwhenlistisnotempty}
\newsavebox{\boxBforwhenlistisnotempty}
\newcommand{\whenlistisnotempty}[2]{
\savebox{\boxAforwhenlistisnotempty}{%
\vbox{#2}%
}%
\savebox{\boxBforwhenlistisnotempty}{%
\vbox{\large\contentsname\\%
[+\baselineskip]\\[+\afterchapskip]\\[+\midchapskip]\\}%
}%
\typeout{The height of the list '#1'
is '\the\ht\boxAforwhenlistisnotempty'
from '\the\ht\boxBforwhenlistisnotempty'}%
\ifdim\ht\boxAforwhenlistisnotempty>\ht\boxBforwhenlistisnotempty%
\usebox{\boxAforwhenlistisnotempty}%
\fi%
}
Console:
) (test3.toc) (test3.lof)
Underfull \hbox (badness 10000) in paragraph at lines 26--26
The height of the list 'List of Figures' is '131.77776pt' from '103.84pt'
Overfull \hbox (17.62482pt too wide) in paragraph at lines 26--27
[][]
(test3.lot)
Underfull \hbox (badness 10000) in paragraph at lines 30--30
The height of the list 'List of Tables' is '117.27776pt' from '103.84pt'
Overfull \hbox (17.62482pt too wide) in paragraph at lines 30--31
[][]
I would like to keep using the \newsavebox answer because it is very much clearer to read than the answer using \setbox0. Can the answer using \newsavebox be changed to stop giving bad box warnings or can the answer using \setbox0 become clearer to read?
Minimal example with bad boxes using \newsavebox:
\documentclass[12pt,a4paper]{memoir}
\newsavebox{\boxAforwhenlistisnotempty}
\newsavebox{\boxBforwhenlistisnotempty}
\newcommand{\whenlistisnotempty}[2]{
\savebox{\boxAforwhenlistisnotempty}{%
\vbox{#2}%
}%
\savebox{\boxBforwhenlistisnotempty}{%
\vbox{\large\contentsname\\[+\baselineskip]\\[+\afterchapskip]}%
}%
\typeout{The height of the list '#1'
is '\the\ht\boxAforwhenlistisnotempty'
from '\the\ht\boxBforwhenlistisnotempty'}%
\ifdim\ht\boxAforwhenlistisnotempty>\ht\boxBforwhenlistisnotempty%
\usebox{\boxAforwhenlistisnotempty}%
\fi%
}
\begin{document}
\tableofcontents
\whenlistisnotempty{\listfigurename}{
\listoffigures
}
\whenlistisnotempty{\listtablename}{
\listoftables
}
\chapter{First section}
\begin{figure}
\centering
Figure
\caption{Caption}
\end{figure}
\end{document}
Update
After @DavidCarlisle comment, I tried using \vbox, but it did not worked and the warnings still showing up:
\newbox\boxAforwhenlistisnotempty
\newbox\boxBforwhenlistisnotempty
\newcommand{\whenlistisnotempty}[2]{%
\setbox\boxAforwhenlistisnotempty\vbox{#2}%
\setbox\boxBforwhenlistisnotempty\vbox{%
\large\contentsname\\%
[+\baselineskip]\\[+\afterchapskip]\\[+\midchapskip]\\%
}%
\typeout{The height of the list '#1'
is '\the\ht\boxAforwhenlistisnotempty'
from '\the\ht\boxBforwhenlistisnotempty'}%
\ifdim\ht\boxAforwhenlistisnotempty>\ht\boxBforwhenlistisnotempty%
\unvbox\boxAforwhenlistisnotempty%
\fi%
}
Although, all warnings now are Underfull badness, instead of Overfull:
(test3.tpc)
Underfull \hbox (badness 10000) in paragraph at lines 48--48
Underfull \hbox (badness 10000) in paragraph at lines 48--48
The height of the list 'Short Table of Contents' is '137.05556pt' from '135.482
51pt'
[1{D:/User/Documents/latex/texmfs/data/pdftex/config/pdftex.map}] (test3.lof)
Underfull \hbox (badness 10000) in paragraph at lines 54--54
Underfull \hbox (badness 10000) in paragraph at lines 54--54
The height of the list 'List of Figures' is '149.05556pt' from '135.48251pt'
[2] (test3.lot)
Underfull \hbox (badness 10000) in paragraph at lines 60--60
Underfull \hbox (badness 10000) in paragraph at lines 60--60
The height of the list 'List of Tables' is '127.0pt' from '135.48251pt'
\setbox\whatevername\vbox... if you do this not\savebox(which is\setbox\whatevername\hbox.) – David Carlisle Nov 10 '19 at 14:02