0

I am trying to conditionally-print a List of Figures and List of Tables only when the relevant elements exist. (I started by modifying the solution to this question: Add list of figures/tables only when content)

The following code works for the List of Figures (the first three/last three lines exist because I am modifying an existing thesis class and attempting to make minimal changes; my contribution is the middle two):

\newif\if@figures
\@figurestrue
\def\listoffigures{
  \newbox\tmpboxA\newbox\tmpboxB%
  \setbox\tmpboxA\vbox{\makeatletter\@input{\jobname.lof}}\setbox\tmpboxB\vbox{Figure 1}%
  \ifdim\ht\tmpboxA<\ht\tmpboxB\@figuresfalse\fi
  \if@figures\chapter*{List of Figures\markboth
  {LIST OF FIGURES}{LIST OF FIGURES}}\@starttoc{lof}\fi
}

However, doing the same for the List of Tables does not work:

\newif\if@tables
\@tablestrue
\def\listoftables{
  \newbox\tmpboxA\newbox\tmpboxB%
  \setbox\tmpboxA\vbox{\makeatletter\@input{\jobname.lot}}\setbox\tmpboxB\vbox{Table 1}%
  \ifdim\ht\tmpboxA<\ht\tmpboxB\@tablesfalse\fi
  \if@tables\chapter*{List of Tables\markboth
  {LIST OF TABLES}{LIST OF TABLES}}\@starttoc{lot}\fi
}

I can see that this is because \jobname.lof is empty, while \jobname.lot contains six \addvspace{10pt} (one for each chapter). Is there a way around this/a better way to handle this?

Edit: after cleaning out the directory, the problem seems to be that the \@starttoc command is responsible for generating the files \jobname.lof and \jobname.lot. So testing the size of the list can't work until \@starttoc is called?

  • There is certainly a simpler way how to check that there's no table. Anyways: if .lof and .lot differ in the number of lines \addvspace{...} they contain, the class is corrupted since the files should be exactly the same in this manner; it's the top sectioning command (\chapter or \section) that inserts the space there (in both of them by default). Could you add a full MWE? – yo' Mar 12 '15 at 16:29
  • @yo' I'm having trouble figuring out a MWE because the .cls file involved is large, and I'm making only minor edits to it. – kyle Mar 12 '15 at 18:50
6

I suggest to use the second answer in the linked question and use the totalcount package:

\documentclass[]{article}
\RequirePackage[figure,table]{totalcount}

\begin{document}
\iftotaltables
   \listoftables
\fi

\iftotalfigures
   \listoffigures
\fi

\begin{figure}
blal
\caption{a figure}
\end{figure}
\end{document}
  • Well you could try, it is a complete minimal example. (The answer is: it needs three compilations, after the first the boolean for figure is true). – Ulrike Fischer Mar 12 '15 at 17:03
  • Works like a charm, thank you. Although, would it be possible to do this in "raw" TeX/LaTeX, without the package import? It seems like it shouldn't be that hard... famous last words when LaTeX is concerned. – kyle Mar 12 '15 at 18:55
  • Sure copy the code of the package in your class. But imho it is against the spirit of latex not to use (good and useful) packages. – Ulrike Fischer Mar 12 '15 at 19:04
  • Ha! I'm not against using packages, I was just hoping to make a decent, sharable edit that wouldn't rely on outside sources if a handful of lines could avoid it. (as is, I'm only writing one thesis, so omitting the List of Figures/Tables is as simple as not including it) – kyle Mar 12 '15 at 19:13

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.