I have a rather large project, from which I select portions based on needs. This selection is done using a complex system of \include
and \input
commands. I would like to generate a list of figures and a list of tables for the output, but in the case there are no figures the list of figures should not appear. The same should happen with tables.
Here is a MWE:
\documentclass[10pt,twocolumn]{article} \usepackage{lipsum} \usepackage[margin=0.75in]{geometry} \usepackage{float} \floatstyle{boxed} \restylefloat{figure} \restylefloat{table} \def\ignoreTable#1\end{table}{} \def\ignoreFigure#1\end{figure}{} \begin{document} \tableofcontents \addcontentsline{toc}{section}{List of Figures} \listoffigures \addcontentsline{toc}{section}{List of Table} \listoftables \section{First section} \lipsum[\inputlineno] \ignoreFigure \begin{figure}[H]\centering \LaTeX\LaTeX \caption{my figure} \end{figure} \lipsum[\inputlineno] \begin{figure}[H]\centering \LaTeX\TeX \caption{another figure} \end{figure} \section{Second section} \lipsum[\inputlineno] \ignoreTable \begin{table}[H]\centering \TeX\LaTeX \caption{my table} \end{table} \lipsum[\inputlineno] \ignoreTable \begin{table}[H]\centering \TeX\TeX \caption{another table} \end{table} \lipsum[\inputlineno] \end{document}
Where if I write
\def\ignoreTable{\relax} \def\ignoreFigure{\relax}
just after \begin{document}
the output is:
Ideally, there would be an "elegant" macro or package, that would eliminate the list of tables, including its entry in the TOC, if there are not tables.
Best Answer
Here is another approach. What it does is to check at the end of the document the values of the
figure
andtable
counters, and add to the.aux
file a boolean flag setting depending on whether these values are zero or positive.Then you have on next run
\iffigures
and\iftables
which allow you to act conditionally.In the code below, perhaps the
\global
are not really needed, I didn't check.See also a variant at bottom which does not use the
figure
/table
counters because they might well be reset to zero due to document sectioning (as pointed out byegreg
in a comment).Here is for a document with figures but no tables:
And if tables are present:
Three compilations needed.
Variant. I don't know to what extent patching
\figure
and\table
as here is OK, depends perhaps on float related packages. At the worst I guess doing the patches immediately after\begin{document}
should be enough (at the end of preamble might not be enough if some package does stuff during At Begin Document).