Count number of figures, tables and citations in report class

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?

-

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{%
\immediate\write\@mainaux{%
\string\gdef\string\totfig{\number\value{totfigures}}%
\string\gdef\string\tottab{\number\value{tottables}}%
}%
}
\makeatother

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


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.

-
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. –  Zoresvit May 13 '12 at 7: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? –  mangledorf 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 0:46
I found a solution, add \providecommand{\totfig}{??} \providecommand{\tottab}{??} below the \newcounter commands –  mangledorf Mar 26 '13 at 10:48
@mangledorf lines added. Thank you! –  Gonzalo Medina Mar 26 '13 at 21:43

This appears to work for me:

\documentclass{report}
% http://texblog.org/2012/04/16/counting-the-total-number-of/
\usepackage{totcount}
\newtotcounter{citenum}
\def\oldcite{}
\let\oldcite=\bibcite
\def\bibcite{\stepcounter{citenum}\oldcite}

\usepackage[figure,table]{totalcount}
% different from totcount, and not on CTAN. Found at
% http://www.mrunix.de/forums/showpost.php?p=257537&postcount=10
% Reference: http://www.latex-community.org/forum/viewtopic.php?f=5&t=9028

\begin{filecontents}{\jobname.bib}
@article {MCOSW,
author = "Hugh Glaser and Afraz Jafri and Ian Millard",
title = "Managing co-reference on the semantic web",
journal = "WWW2009 Workshop: Linked Data on the Web (LDOW2009)",
month = "April",
year = "April 2009",
}
\end{filecontents}

\begin{document}

\chapter{One}

Some citations: \cite{MCOSW}, \cite{MCOSW}, \cite{MCOSW}.
This document contains \totalfigures{} figures, \totaltables{} tables, and
\total{citenum} reference (possibly with multiple citations).

\begin{table}
\caption{Table caption}
\centering Table content
\end{table}
\begin{figure}
\centering Figure content
\caption{Figure caption}
\end{figure}

\chapter{Two}

\begin{table}
\caption{Table caption}
\centering Table content
\end{table}
\begin{figure}
\centering Figure content
\caption{Figure caption}
\end{figure}

\bibliographystyle{plain}
\bibliography{\jobname}

\end{document}

-
Uppps. This reminds me that I totally forgot to put totalcount on CTAN which was due four years ago. Will do with the next update of the caption package... (which will hopefully be done this year ;-)) –  Axel Sommerfeldt May 13 '12 at 5:39
Thank you for the advice. However I prefer using only standard packages to avoid the need of providing additional packages together with latex sources. @gonzalo-medina has already got the solution for figures and tables. I'm not sure how you got citenum counter to work though since you never did \regtotcounter{} in your code. –  Zoresvit May 13 '12 at 7:52
@AxelSommerfeldt I posted an answer using your wonderful package :) –  cmhughes Dec 31 '13 at 1:55

The totalcount package handles this easily

% arara: pdflatex
\documentclass{report}
\usepackage[figure,table]{totalcount}

\begin{document}

\chapter{Test Chapter One}
There are \totalfigures\ figures and \totaltables\ 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}

-