更新

更新

原始答案来自当没有图表时,如何列出图表?,没有给我任何坏盒警告:

\def\wheninteresting#1{%
\setbox0\vbox{#1}%
\ifdim\ht0>35pt
\unvbox0
\fi}

然而,如何动态计算空列表的高度?,它不断newsavebox给我几个坏盒子警告

\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%
}

安慰:

) (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
[][]

我想继续使用\newsavebox答案,因为它比使用 的答案更清晰易读\setbox0。是否可以更改使用 的答案\newsavebox以停止发出坏框警告,或者使用 的答案是否可以\setbox0变得更清晰易读?

使用坏盒的最小示例\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}

更新

在 @DavidCarlisle 评论之后,我尝试使用\vbox,但它不起作用并且仍然出现警告:

\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%
}

虽然现在所有警告都是Underfull不好的,而不是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'

答案1

除了@DavidCarlisle 评论之外,我还设法通过替换来完全修复\\[+\afterchapskip]它们\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%
}

安慰:

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.

参考:

  1. \vskip 和 \vspace 之间有什么区别?
  2. \setbox 与 \sbox 和 \savebox - 我需要了解哪些区别?
  3. \setbox 的问题

相关内容