# Add list of figures / tables only when content

How can I add the list of figures, tables or listings to the frontmatter, ONLY when there is content in each respectively?

This is for the article class; adapt the redefinition of \listoftables in a similar way by copying the definition in the class you're using.

\documentclass{article}

\makeatletter
\renewcommand\tableofcontents{%
\section*{\contentsname
\@mkboth{%
\MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
\global\@printlisttrue
\@starttoc{toc}%
}

% redefine \listoftables to check for contents
\renewcommand\listoftables{%
\check@list{lot}
\if@printlist
\section*{\listtablename}%
\@mkboth{%
\MakeUppercase\listtablename}%
{\MakeUppercase\listtablename}%
\fi
\@starttoc{lot}%
}
\def\@starttoc#1{%
\begingroup
\makeatletter
\if@printlist
\@input{\jobname.#1}%
\fi
\if@filesw
\expandafter\newwrite\csname tf@#1\endcsname
\immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
\fi
\@nobreakfalse
\endgroup}
\newif\if@printlist

% We typeset the file in a box; if the box is empty, we don't input the file for real
\def\check@list#1{%
\global\@printlistfalse
\setbox\z@=\vbox{\makeatletter\@input{\jobname.#1}}%
\ifdim\ht\z@>\z@\global\@printlisttrue\fi}
\makeatother

\begin{document}
\listoftables

%%% uncomment the table for seeing the effect
%\begin{table}[htp]x\caption{y}\end{table}

abc

\end{document}

• This is excellent, however, seems to be a clash when babel is being used. Do you get an error when \usepackage[english]{babel} or \usepackage[australian]{babel} is put in the preamble? – Nicholas Hamilton May 11 '13 at 20:03
• @ADP Yes, you're right. Fixed. – egreg May 11 '13 at 20:07
• Great. Cheers.. – Nicholas Hamilton May 11 '13 at 20:11
• I just realized it also killed the table of contents. – Nicholas Hamilton May 11 '13 at 20:42
• @ADP Yes; you have to redefine also \tableofcontents accordingly. I'll add it. – egreg May 11 '13 at 20:45

You could use the/my totalcount package for that purpose:

\documentclass{article}
\usepackage{listings}
\usepackage[figure,table,lstlisting]{totalcount}

\begin{document}
\iftotalfigures\listoffigures\fi
\iftotaltables\listoftables\fi
\iftotallstlistings\lstlistoflistings\fi

\begin{figure}
\caption{A figure}
\end{figure}

%\begin{table}
%\caption{A table}
%\end{table}

\begin{lstlisting}[caption={Useless code},label=useless,captionpos=t]
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{lstlisting}

\end{document}


Please note that older versions of the totalcount package does not support the lstlisting counter, one need at least v1.0-92 for that.

Version v1.0-92 of the totalcount package can be found here: https://sourceforge.net/p/latex-caption/code/HEAD/tree/trunk/tex/ (will upload it to CTAN today)

• Excellent! Havn't tested yet but looks like a clean solution. – Nicholas Hamilton May 12 '13 at 9:32
• Will it work with newly defined lstlisting environments? – Nicholas Hamilton May 12 '13 at 9:33
• @ADP How do you actually define new listing environments? At least the one defined with \lstnewenvironment do use the same counter (lstlistings), so there should be no problem. – user2574 May 12 '13 at 9:45
• @ADP ...I just have found tex.stackexchange.com/a/113679/2574 However, this solution (from Werner) does use own lists but not own counters for the newly defined environments, so in this case you need to use the solution offered by egreg since it checks for the content of the lists while the totalcount package is dependent on (separate) counters. – user2574 May 12 '13 at 9:53

A sketch of a simple solution, with additional restrictions: \caption should be used, and listof... is at the end of a document.

\documentclass{article}

\begin{document}

\begin{figure}[!ht]
\centering
\caption{ }
\end{figure}

\begin{table}[!ht]
\caption{ }
\end{table}

\section{First}

\begin{figure}[!ht]
\centering
\caption{ }
\end{figure}

\begin{table}[!ht]
\caption{ }
\end{table}


• The problem with this approach is that the counters can get reset, say if the number format is chapter or section based. – Nicholas Hamilton May 11 '13 at 20:09