8

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

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}
  • 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
15

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

enter image description here

  • 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
  • @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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.