[Tex/LaTex] How to stop \newsavebox giving me bad boxes warnings or how to use better names with \setbox

badnessboxeslistsmemoirtable of contents

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'

Best Answer

I addition to @DavidCarlisle comment, I manage to fully fix them by replacing \\[+\afterchapskip] by \vskip\afterchapskip

\newbox\boxAforwhenlistisnotempty
\newbox\boxBforwhenlistisnotempty

\newcommand{\whenlistisnotempty}[2]{%
  \setbox\boxAforwhenlistisnotempty\vbox{#2}%
  \setbox\boxBforwhenlistisnotempty\vbox{%
    \large\contentsname\vskip\baselineskip\vskip\baselineskip
  }%
  \typeout{The height of the list '#1'
      is '\the\ht\boxAforwhenlistisnotempty'
      from '\the\ht\boxBforwhenlistisnotempty'}%
  \ifdim\ht\boxAforwhenlistisnotempty>\ht\boxBforwhenlistisnotempty%
    \unvbox\boxAforwhenlistisnotempty%
  \fi%
}

Console:

This is pdfTeX, Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6400)
entering extended mode
(test3.tex
LaTeX2e <2017-04-15>
Babel <3.12> and hyphenation patterns for 76 language(s) loaded.
(D:\User\Documents\latex\texmfs\install\tex\latex\memoir\memoir.cls
Document Class: memoir 2016/05/16 v3.7f configurable book, report, article docu
ment class
(D:\User\Documents\latex\texmfs\install\tex\generic\oberdiek\ifpdf.sty)
(D:\User\Documents\latex\texmfs\install\tex\latex\ifetex\ifetex.sty
(D:\User\Documents\latex\texmfs\install\tex\plain\ifetex\ifetex.tex))
(D:\User\Documents\latex\texmfs\install\tex\generic\ifxetex\ifxetex.sty)
(D:\User\Documents\latex\texmfs\install\tex\generic\oberdiek\ifluatex.sty)
(D:\User\Documents\latex\texmfs\install\tex\latex\memoir\mem10.clo))
(D:\User\Documents\latex\texmfs\install\tex\latex\oberdiek\bookmark.sty
(D:\User\Documents\latex\texmfs\install\tex\generic\oberdiek\pdfescape.sty
(D:\User\Documents\latex\texmfs\install\tex\generic\oberdiek\ltxcmds.sty)
(D:\User\Documents\latex\texmfs\install\tex\generic\oberdiek\pdftexcmds.sty
(D:\User\Documents\latex\texmfs\install\tex\generic\oberdiek\infwarerr.sty)))
(D:\User\Documents\latex\texmfs\install\tex\generic\oberdiek\ifvtex.sty)
(D:\User\Documents\latex\texmfs\install\tex\latex\hyperref\hyperref.sty
(D:\User\Documents\latex\texmfs\install\tex\generic\oberdiek\hobsub-hyperref.st
y
(D:\User\Documents\latex\texmfs\install\tex\generic\oberdiek\hobsub-generic.sty
)) (D:\User\Documents\latex\texmfs\install\tex\latex\graphics\keyval.sty)
(D:\User\Documents\latex\texmfs\install\tex\latex\oberdiek\auxhook.sty)
(D:\User\Documents\latex\texmfs\install\tex\latex\oberdiek\kvoptions.sty)
(D:\User\Documents\latex\texmfs\install\tex\latex\hyperref\pd1enc.def)
(D:\User\Documents\latex\texmfs\install\tex\latex\00miktex\hyperref.cfg)
(D:\User\Documents\latex\texmfs\install\tex\latex\url\url.sty))
(D:\User\Documents\latex\texmfs\install\tex\latex\memoir\memhfixc.sty)

Package hyperref Message: Driver (autodetected): hpdftex.

(D:\User\Documents\latex\texmfs\install\tex\latex\hyperref\hpdftex.def
(D:\User\Documents\latex\texmfs\install\tex\latex\oberdiek\rerunfilecheck.sty))
 (D:\User\Documents\latex\texmfs\install\tex\latex\oberdiek\bkm-pdftex.def))
(test3.aux)
(D:\User\Documents\latex\texmfs\install\tex\latex\hyperref\nameref.sty
(D:\User\Documents\latex\texmfs\install\tex\generic\oberdiek\gettitlestring.sty
))
Redoing nameref's sectioning
Redoing nameref's label
(test3.tpc)
The height of the list 'Short Table of Contents' is '151.0pt' from '34.6825pt'
[1{D:/User/Documents/latex/texmfs/data/pdftex/config/pdftex.map}] (test3.lof)
The height of the list 'List of Figures' is '149.05556pt' from '34.6825pt'
[2] (test3.lot)
The height of the list 'List of Tables' is '127.0pt' from '34.6825pt'
[3] [4] [5] (test3.aux) )<D:/User/Documents/latex/texmfs/install/fonts/type1/pu
blic/amsfonts/cm/cmbx12.pfb><D:/User/Documents/latex/texmfs/install/fonts/type1
/public/amsfonts/cm/cmr10.pfb>
Output written on test3.pdf (5 pages, 32811 bytes).
Transcript written on test3.log.

References:

  1. What is the difference between \vskip and \vspace?
  2. \setbox vs. \sbox and \savebox - What are the differences I need to know about?
  3. Problem with \setbox
Related Question