How can I add the list of figures, tables or listings to the frontmatter, ONLY when there is content in each respectively?
Asked
Active
Viewed 1,439 times
3 Answers
16
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)
8
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
% We don't want to kill the table of contents
\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}
egreg
- 1,121,712
-
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 have to redefine also
\tableofcontentsaccordingly. I'll add it. – egreg May 11 '13 at 20:45 -
@egreg Why not offering something like
\ifchecklist{<counter>}{<code>}so one could use\ifchecklist{figure}{\listoffigures}. (The file extension could be found in\ext@<counter>, e.g.\ext@figureis defined aslof.) As opposite to your solution this approach would not be dependent on the document class used anymore since no patching of\listoffiguresetc. is needed here. – May 12 '13 at 10:01 -
@AxelSommerfeldt The counter might reset at new sections. However your idea is good. – egreg May 12 '13 at 10:14
-
@egreg I do not see how counters, reset at new sections, do affect my idea about your solution, since (still) no counters are used at all? I'm confused... – May 12 '13 at 10:48
-
@egreg
\newcommand\check@list[2]{\setbox\z@=\vbox{\makeatletter\@input{\jobname.#1}}\ifdim\ht\z@>\z@\relax#2\else\if@filesw\expandafter\newwrite\csname tf@#1\endcsname\immediate\openout \csname tf@#1\endcsname \jobname.#1\relax\fi\fi}\newcommand\ifchecklist[1]{\check@list{\@nameuse{ext@#1}}}- hopefully this makes it more clear what I had in mind. – May 12 '13 at 11:39
1
A sketch of a simple solution, with additional restrictions: \caption should be used, and listof... is at the end of a document.
\documentclass{article}
\def\ADPlistoftables{\ifnum\value{table}>0 \listoftables \else \fi}
\def\ADPlistoffigures{\ifnum\value{figure}>0 \listoffigures \else \fi}
\begin{document}
%\ADPlistoftables
%\ADPlistoffigures
\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}
\ADPlistoftables
\ADPlistoffigures
\end{document}

Przemysław Scherwentke
- 37,268
-
The problem with this approach is that the counters can get reset, say if the number format is
chapterorsectionbased. – Nicholas Hamilton May 11 '13 at 20:09 -
@ADP You are right. It is a simplification, because I cannot imagine a real usage, when percent sign is easy to use. – Przemysław Scherwentke May 11 '13 at 20:19
-
Was waiting for a comment like that. But if you have a look at my recent post about different listings environments, and having a list of C code, list of R code and list of Pseudo code, now extend it to many other code variants, it is a safety net. – Nicholas Hamilton May 11 '13 at 20:27
-
@ADP I hope that informing about it is not against the rules. I upvoted your question when it had only downvote, because i saw it's potential. However, a background of your question might be helpful in finding a good answer. And now I am going to study your other posts. :-) – Przemysław Scherwentke May 11 '13 at 20:49
\lstnewenvironmentdo use the same counter (lstlistings), so there should be no problem. – May 12 '13 at 09:45totalcountpackage is dependent on (separate) counters. – May 12 '13 at 09:53