# How to detect that there are tables in the file? [duplicate]

I am writing a LaTeX template for our university dissertations. A problem I face is the following:

An student asked me that his dissertation does not include any tables and he views an empty list of tables. I have put \listoftables in the .cls file and told him to comment it. However, I wonder how to detect the existence of tables and automatically disable/enable any listings such as list of figures, list of tables, list of algorithms and similar thing.

I've searched for a while, but could not find any solution other than adding options to the document class like the following:

\newif\if@alg\@algfalse
\DeclareOption{alg}{
\@algtrue
}
\ProcessOptions\relax
\if@alg
\RequirePackage{algorithmicx}
\RequirePackage{algorithm}
\RequirePackage{algpseudocode}
\renewcommand{\thealgorithm}{\arabic{chapter}.\arabic{algorithm}}
\fi

\if@alg
\listofalgorithms
\cleardoublepage
\newpage
\fi

• are you typesetting in the class file? or is the listofalgorithms in some unshown \AtbeginDocument ?? Feb 21 '18 at 19:30
• I've used it inside a command, look at this github.com/alishakiba/vruthesis/blob/master/vruthesis.cls on line 581. Feb 21 '18 at 19:34
• Parse the contents of the corresponding file. If there is no contents line in *.lof, it is unlikely that there is a figure in the \listoffigures. Feb 21 '18 at 19:42
• @Skillmon How to do this in a LaTeX way? I mean the user gets the result just by running xelatex -syntex=-1 thesis.tex and not running another program. Feb 21 '18 at 19:43
• so your posted code above is just misleading, I suggest you edit to be something that could have been used as a class. (see mico's answer) Feb 21 '18 at 20:17

Auto-detecting the presence or absence of floats might get messed up if the user is in the habit of deleting the aux files compulsively. (You'd be surprised how common this obsession is...) I would therefore like to suggest you take a different approach: provide three documentclass-level options: nofigures, notables, and noalgorithms, to be specified by the user him/herself. If these options are specified, no list of figures, no list of tables, and no list of algorithms, respectively, is generated.

The following code should illustrate this suggestion in more detail. It sets up both a document class file called myclass.cls (you're obviously free to choose a more imaginative name!), which sets up the new options and then invokes the report class, as well as a sample user .tex file that employs the myclass document class.

Addendum: On @Werner's suggestion, I've added code for three user-level macros: \nofigures, \notables, and \noalgorithms. They may be inserted by the user in the preamble, as an alternative to providing the corresponding options at the \documentclass stage

First, the code for the class file (to be saved as myclass.cls):

\NeedsTeXFormat{LaTeX2e}[2015/01/01]
\ProvidesClass{myclass}[2018/02/21]

% define three new documentclass-level options
\newif\ifnofigures\nofiguresfalse
\newif\ifnotables\notablesfalse
\newif\ifnoalgorithms\noalgorithmsfalse
\DeclareOption{nofigures}{\nofigurestrue}
\DeclareOption{notables}{\notablestrue}
\DeclareOption{noalgorithms}{\noalgorithmstrue}

\ProcessOptions\relax

% provide three user commands: \nofigures, \notables, \noalgorithms
% (to be used as an alternative to setting documentclass-level options)
\newcommand\nofigures{\let\ifnofigures\iftrue}
\newcommand\notables{\let\ifnotables\iftrue}
\newcommand\noalgorithms{\let\ifnoalgorithms\iftrue}

\usepackage{algorithmicx,algorithm,algpseudocode}
% load any and all other default packages

\AtBeginDocument{%
\pagenumbering{roman}
\maketitle % or, likely, something far more elaborate
\ifnofigures\else\listoffigures\fi
\ifnotables\else\listoftables\fi
\ifnoalgorithms\else\listofalgorithms\fi
\clearpage
\pagenumbering{arabic}
}


Second, a sample user document. Note that if all three of the new documentclass options -- nofigures, notables, and noalgorithms -- are specified, no lists of figures, tables, and algorithms are created.

\documentclass[nofigures,notables,noalgorithms]{myclass}

\title{Thoughts}
\author{A. Person}
\date{\today}

\begin{document}
xxx
\end{document}

• How about also providing document-level options, like \nofigures, \notables, ... (that sets \let\ifnofigures\iftrue, ...). Feb 21 '18 at 20:38
• @Werner - Many thanks for these suggestions! I've added the required code to the class file.
– Mico
Feb 21 '18 at 20:54
• @Werner - My very best (though several-days-late, tsk, tsk) congratulations on the passing the 400k rep count mark!!!
– Mico
Feb 21 '18 at 20:57
• @Mico, and I'm not able to get my 20k lol Feb 21 '18 at 23:09
• @Sigur — Courage! Just a few more days...
– Mico
Feb 21 '18 at 23:22

You can use the totalcount package. But be aware that it needs three compilations to get a list of tables or figures.

\documentclass{book}
\RequirePackage[figure,table]{totalcount}
\begin{document}
\iftotaltables
\listoftables %no list of tables
\fi
\iftotalfigures
\listoffigures
\fi

\chapter{A}
\begin{figure}
\caption{a figure}
\end{figure}
\chapter{B}
blblb
\end{document}