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?

3 Answers 3

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.

5
  • 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, 2012 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? Mar 24, 2013 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. Mar 25, 2013 at 0:46
  • I found a solution, add \providecommand{\totfig}{??} \providecommand{\tottab}{??} below the \newcounter commands Mar 26, 2013 at 10:48
  • 1) 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, 2018 at 22:46
7

This appears to work for me:

enter image description here

\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}
3
  • 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 ;-))
    – user2574
    May 13, 2012 at 5:39
  • @AxelSommerfeldt I posted an answer using your wonderful package :)
    – cmhughes
    Dec 31, 2013 at 1:55
  • I especially appreciate how easy it is to count customized counters with the totcount package. Thanks for the pointer! Most helpful for me was the Example section of ctan.space-pro.be/tex-archive/macros/latex/contrib/totcount/…
    – ingli
    Jan 25, 2023 at 12:43
5

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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .