Here's a combination of totcount and assoccnt (it's combined in xassoccnt now, at least some features of totcount)
xassoccnt defines counters that step with a master counter, say figure but are not reset if figure is set to zero, i.e. the totalfigures counter holds the total number of figures (unless manipulated directly).
Using the \NewTotalDocumentFeature, the value of a total counter is written to the .aux file at the end of the compilation run and read in in the next run, i.e. the number of figures is known at the start of the document.
Werner used the refcount approach together with assoccnt (which is the predecessor of xassoccnt, which is much more developed than assoccnt (I know about the states -- I am the author of (x)assoccnt ;-))
I redefined the \listof... commands slightly.
\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xassoccnt}
\usepackage{graphicx}
\usepackage{tabularx}
\NewTotalDocumentCounter{totalfigures}
\NewTotalDocumentCounter{totaltables}
\DeclareAssociatedCounters{figure}{totalfigures}
\DeclareAssociatedCounters{table}{totaltables}
\makeatletter
\renewcommand{\TotalValue}[1]{% A small bug fix
\value{xassoccnt@total@#1}%
}
\let\@@latex@@listoffigures\listoffigures
\let\@@latex@@listoftables\listoftables
\renewcommand{\listoffigures}{%
\ifnum\TotalValue{totalfigures} > 0
\@@latex@@listoffigures%
\fi
}
\renewcommand{\listoftables}{%
\ifnum\TotalValue{totaltables} > 0
\@@latex@@listoftables%
\fi
}
\makeatother
\begin{document}
\tableofcontents
\clearpage
\listoftables
\listoffigures
A figure:
\begin{figure}[htbp]
\centering
\includegraphics[width=0.25\linewidth]{example-image-a}
\caption{A picture}
\label{fig:label}
\end{figure}
A table:
\begin{table}[htbp]
\centering
%\caption{A table}
\label{tab:label}
\begin{tabularx}{\linewidth}{lX}
test & test test test test test test test test test test test test test test test test test test test test
\end{tabularx}
\end{table}
\end{document}
*.lof(and*.lot) file exists at all? – Thorsten Donig Oct 31 '11 at 18:08\listoffigurescalls\@starttoc{lof}which creates/writes to\jobname.lof. So\jobname.lofonly exists based the call\listoffigures. Checking for its existence is therefore "too late". – Werner Oct 31 '11 at 21:27