更新

更新

继 David Carlisle 的回答之后当没有图表时,如何列出图表?

\documentclass[10pt,twocolumn]{article}
\usepackage{lipsum}

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

\begin{document}
\tableofcontents

\wheninteresting{
\addcontentsline{toc}{section}{List of Figures}
\listoffigures
}

\wheninteresting{
\addcontentsline{toc}{section}{List of Table}
\listoftables
}

\section{First section}
...

在我的文档中,每个列表前面都有类似这样的内容\pdfbookmark[0]{\lstlistlistingname}{lol},使用他的答案时,列表在目录和文档为空时会正确隐藏。但是,它仍会添加到 PDF 书签项目列表中。

为什么\addcontentsline{toc}不将其添加到目录中就可以正常工作,但\pdfbookmark[0]仍然可以工作并将隐藏列表添加到目录中?

我设法通过改变他的回答来解决这个问题:

\newcommand{\whenlistisnotempty}[3]{
  \setbox0\vbox{#2}%
  \typeout{The height of the list '#1' is '\the\ht0'}%
  \ifdim\ht0>100pt%
    #3%
    \unvbox0%
  \fi%
}

所以,在使用的时候,我是这么使用的:

\whenlistisnotempty{\listfigurename}{%
    \addcontentsline{toc}{section}{List of Figures}
    \\listoffigures
}{\pdfbookmark[0]{\listfigurename}{lol}}

然后,\pdfbookmark[0]停止将其添加到 PDF 书签内容中。

表格列表一直显示在 PDF 书签中,但未显示在目录中。表格列表不应该显示在任何地方,因为它是空的。

在此处输入图片描述

完整示例:

\documentclass[10pt,twocolumn]{memoir}
\usepackage{bookmark}

\usepackage{chngcntr}
\counterwithout{table}{chapter}
\counterwithout{figure}{chapter}

% https://tex.stackexchange.com/questions/61086/how-to-create-my-own-list-of
\newcommand{\mytextpreliminarylistname}{Short Table of Contents}
\newlistof{textpreliminarycontents}{tpc}{\mytextpreliminarylistname}

% https://tex.stackexchange.com/questions/66604/resetting-counter
% https://tex.stackexchange.com/questions/388489/custom-list-throw-latex-error
\newlistentry{textpreliminarycounter}{tpc}{0}

% https://tex.stackexchange.com/questions/56131/continuing-page-numbering-roman
\renewcommand{\thetextpreliminarycounter}{\arabic{textpreliminarycounter}}

% https://tex.stackexchange.com/questions/71162/reset-section-numbering-between
\newcommand{\addtotextpreliminarycontent}[1]
{%
  \refstepcounter{textpreliminarycounter}%
  \addcontentsline{tpc}{textpreliminarycounter}{%
      \protect\numberline{\thetextpreliminarycounter}#1}\par%
}

\newbox\boxAforwhenlistisnotempty
\newbox\boxBforwhenlistisnotempty

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

\begin{document}
\whenlistisnotempty{\mytextpreliminarylistname}{
\pdfbookmark[0]{\mytextpreliminarylistname}{tpc}
\textpreliminarycontents*
}

\whenlistisnotempty{\listfigurename}{
\pdfbookmark[0]{\listfigurename}{lof}
\addtotextpreliminarycontent{\listfigurename}
\listoffigures*
}

\whenlistisnotempty{\listtablename}{
\pdfbookmark[0]{\listtablename}{lot}
\addtotextpreliminarycontent{\listtablename}
\listoftables*
}

\chapter{Chap Chap}

\begin{figure}[h]\centering
\caption{my figure}
\end{figure}

\begin{figure}[h]\centering
\caption{another figure}
\end{figure}

% \begin{table}[h]\centering
% \caption{my table}
% \end{table}

% \begin{table}[h]\centering
% \caption{another table}
% \end{table}

\end{document}

更新

  1. MikTex 的成功编译日志:

    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 '94.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 '94.6825pt'
    [2] (test3.lot)
    The height of the list 'List of Tables' is '127.0pt' from '94.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.
    
  2. TexLive 2016 成功编译日志

    This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016/Debian) (preloaded format=pdflatex)
     \write18 enabled.
    entering extended mode
    (./main.tex
    LaTeX2e <2017/01/01> patch level 3
    Babel <3.9r> and hyphenation patterns for 83 language(s) loaded.
    (/usr/share/texlive/texmf-dist/tex/latex/memoir/memoir.cls
    Document Class: memoir 2016/05/16 v3.7f configurable book, report, article docu
    ment class
    (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
    (/usr/share/texlive/texmf-dist/tex/latex/ifetex/ifetex.sty
    (/usr/share/texlive/texmf-dist/tex/plain/ifetex/ifetex.tex))
    (/usr/share/texlive/texmf-dist/tex/generic/ifxetex/ifxetex.sty)
    (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifluatex.sty)
    (/usr/share/texlive/texmf-dist/tex/latex/memoir/mem10.clo)
    (/usr/share/texlive/texmf-dist/tex/latex/memoir/mempatch.sty))
    (/usr/share/texlive/texmf-dist/tex/latex/oberdiek/bookmark.sty
    (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/pdfescape.sty
    (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ltxcmds.sty)
    (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/pdftexcmds.sty
    (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/infwarerr.sty)))
    (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifvtex.sty)
    (/usr/share/texlive/texmf-dist/tex/latex/hyperref/hyperref.sty
    (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/hobsub-hyperref.sty
    (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/hobsub-generic.sty))
    (/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
    (/usr/share/texlive/texmf-dist/tex/latex/oberdiek/auxhook.sty)
    (/usr/share/texlive/texmf-dist/tex/latex/oberdiek/kvoptions.sty)
    (/usr/share/texlive/texmf-dist/tex/latex/hyperref/pd1enc.def)
    (/usr/share/texlive/texmf-dist/tex/latex/latexconfig/hyperref.cfg)
    (/usr/share/texlive/texmf-dist/tex/latex/url/url.sty))
    (/usr/share/texlive/texmf-dist/tex/latex/memoir/memhfixc.sty)
    
    Package hyperref Message: Driver (autodetected): hpdftex.
    
    (/usr/share/texlive/texmf-dist/tex/latex/hyperref/hpdftex.def
    (/usr/share/texlive/texmf-dist/tex/latex/oberdiek/rerunfilecheck.sty))
    (/usr/share/texlive/texmf-dist/tex/latex/oberdiek/bkm-pdftex.def))
    (latexcache/main.aux)
    (/usr/share/texlive/texmf-dist/tex/latex/hyperref/nameref.sty
    (/usr/share/texlive/texmf-dist/tex/generic/oberdiek/gettitlestring.sty))
    Redoing nameref's sectioning
    Redoing nameref's label
    (latexcache/main.tpc)
    The height of the list 'Short Table of Contents' is '151.0pt' from '94.6825pt'
    [1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] (latexcache/main.lof)
    The height of the list 'List of Figures' is '151.0pt' from '94.6825pt'
    [2] (latexcache/main.lot)
    The height of the list 'List of Tables' is '127.0pt' from '94.6825pt'
    [3] [4] [5] (latexcache/main.aux) )</usr/share/texlive/texmf-dist/fonts/type1/p
    ublic/amsfonts/cm/cmbx12.pfb></usr/share/texlive/texmf-dist/fonts/type1/public/
    amsfonts/cm/cmr10.pfb>
    Output written on latexcache/main.pdf (5 pages, 27841 bytes).
    SyncTeX written on latexcache/main.synctex.gz.
    Transcript written on latexcache/main.log.
    

参考:

  1. 如何停止 \newsavebox 向我提供坏盒子警告或如何使用 \setbox 来获得更好的名称?

相关内容