1

Following the answer of David Carlisle on list of figures and tables when there are no figures or tables?

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

In my document, I had to set the line \ifdim\ht0>35pt to \ifdim\ht0>100pt to make this work. Because I do not know why but my page headers are bigger than just 35pt. More precisely, I added \typeout{The height of the list is '\the\ht0'} before \ifdim\ht0>35pt, and I got The height of the list is '51.49147pt' for an empty list.

The question would be, can the hard coded value of 35pt to be dynamically calculated, so his solution can work with any latex document class and styling?

user
  • 4,745
  • Imho it is better to write some counter or boolean to the aux instead of this typesetting. As you already discovered it can have side effects if the code does more than simple typesetting. – Ulrike Fischer Nov 10 '19 at 08:31
  • @UlrikeFischer ... it will be written possibly also in case that the list will not be empty and will fail in third compilation by hiding the list. Am I wrong? Sure an answer of this kind would help in several problems (I use label instead) – koleygr Nov 10 '19 at 09:30
  • 1
    @koleygr, If we create a dummy list of things, create a box around it and get its size? This would definitely tell for sure the actual height of a list. Of course, this dummy list of things would never be drawn/outputted to the actual document. – user Nov 11 '19 at 01:24

1 Answers1

1

This is an approximation but should work in many cases and does such an automation (but not perfectly):

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

\newsavebox{\testbox}
\newsavebox{\testboxB}
\def\wheninteresting#1{%
\savebox{\testbox}{\vbox{#1}}%
\savebox{\testboxB}{\vbox{\large\contentsname\\[+\baselineskip]}}%
\ifdim\ht\testbox>\ht\testboxB
\usebox{\testbox}
\fi}


\begin{document}
\tableofcontents

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

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

\section{First section}


\begin{figure}
    \centering
    Figure
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\end{document} 

enter image description here

PS: You may redefine the \testboxB content for your purposes according to the way that your document class prints the title of the contents... I just suppose this will serve many cases.

koleygr
  • 20,105