15

I'm using report documentclass and trying to use totcount for counting figures, tables and references, but there are two problems:

  1. Figures and tables counters are reset for each chapter, so \total{figure} and \total{table} is displaying 0. How to find the total count of figures and tables throughout the document?
  2. A solution for counting references given in totcount documentation

    \newtotcounter{citesnum}
    \def\oldcite{} \let\oldcite=\cite
    \def\cite{\stepcounter{citesnum}\oldcite}
    

    isn't working for me since I have several \cite{} commands for single reverence. Is there a way to work around this one as well?

lockstep
  • 250,273
rkiyanchuk
  • 263
  • 2
  • 7

3 Answers3

14

You can use auxiliary counters for figures and tables and patch \chapter (using the etoolbox package, for example) to add the value \value{figure} (resp. \value{table}) to the respective auxiliary counter; a similar procedure can be applied for tables; then, at the end of the document this operation is once again performed and the result is written to the .aux file in two macros that hold the total values:

\documentclass{report}
\usepackage{etoolbox}

\newcounter{totfigures}
\newcounter{tottables}

\providecommand\totfig{} 
\providecommand\tottab{}

\makeatletter
\AtEndDocument{%
  \addtocounter{totfigures}{\value{figure}}%
  \addtocounter{tottables}{\value{table}}%
  \immediate\write\@mainaux{%
    \string\gdef\string\totfig{\number\value{totfigures}}%
    \string\gdef\string\tottab{\number\value{tottables}}%
  }%
}
\makeatother

\pretocmd{\chapter}{\addtocounter{totfigures}{\value{figure}}\setcounter{figure}{0}}{}{}
\pretocmd{\chapter}{\addtocounter{tottables}{\value{table}}\setcounter{table}{0}}{}{}

\begin{document}

\chapter{Test Chapter One}
There are \totfig\ figures and \tottab\ tables in this document.
\begin{figure}FA\caption{test FA}\end{figure}
\begin{table}TA\caption{test TA}\end{table}
\begin{figure}FB\caption{test FB}\end{figure}

\chapter{Test Chapter Two}
\begin{figure}FC\caption{test FC}\end{figure}
\begin{figure}FD\caption{test FD}\end{figure}
\begin{table}TB\caption{test TB}\end{table}
\begin{figure}FE\caption{test FE}\end{figure}

\chapter{Test Chapter Three}
\begin{table}TC\caption{test TC}\end{table}
\begin{figure}FF\caption{test FF}\end{figure}
\begin{table}TD\caption{test TD}\end{table}

\end{document}

enter image description here

You need to run at least twice the code.

After testing, I noticed that, for some reason, if some chapter does not contain any figures, the counting is wrong. By appending \setcounter{figure}{0} to the \pretocmd code, I get the correct results. My guess is that the figure counter holds the value of the previous chapter, when no figures have been defined in the current chapter. I may be wrong though. In any case, resetting the counter when starting the chapter will not produce any unexpected results.

azetina
  • 28,884
Gonzalo Medina
  • 505,128
  • Thank you, that solved the problem and saved me from manual figures/tables counting throughout the thesis. I don't understand, however, why totcount developers would not impement such functionality. – rkiyanchuk May 13 '12 at 07:46
  • This fails for me unless I build at least once without it and leave the .aux files. Anyway to make it work out of the box? – Alec Jacobson Mar 24 '13 at 19:52
  • @mangledorf that is because the information is first written to the .aux file (on the first run) and then read from it (on the second run). The current implementation has this disadvantage; if I think of a work-around I'll let you know. – Gonzalo Medina Mar 25 '13 at 00:46
  • I found a solution, add \providecommand{\totfig}{??} \providecommand{\tottab}{??} below the \newcounter commands – Alec Jacobson Mar 26 '13 at 10:48
  • How to count figures that are inserted in a report before any \chapter{}? 2) Can \AtEndDocument be included in my custom class definition?
  • – Viesturs Nov 21 '18 at 22:46