Site icon texblog

Counting the total number of…

…sections, chapters, pages, theorems, equations, references, etc. There are numerous potential commands in LaTeX one may consider counting in order to automatically output their total number of appearances in a document. The totcount package provides a simple way to do that.

We will to consider two different cases, single commands (cite, section, chapter, etc.) and environments (equation, align, theorem) which are treated slightly differently. In fact, many environments have their particularities and may therefore need special manipulation. Let’s start with the more consistent case for which the author provided a minimal working example in the package documentation.

 
References (cite)

First, we define a new counter, in this case citenum. Next, we redefine the command cite, increasing the newly defined counter every time the command is called. Finally, we redefine the bibliography title in order to display the total number of references.

Below you will find a code example along with the pdf output:

\documentclass[11pt]{article}
\usepackage{totcount}
\newtotcounter{citenum}
\def\oldcite{}
\let\oldcite=\bibcite
\def\bibcite{\stepcounter{citenum}\oldcite}
\renewcommand\refname{References (\total{citenum})} %For article
%\renewcommand\bibname{} %For book and report
\begin{document}
A few \TeX\ references by Lamport \cite{lamport1986, lamport1987, lamport1994}
\bibliographystyle{plain}
\bibliography{references}
\tiny\hfill Created by http://texblog.org
\end{document}

For simple commands, displaying their total number is relatively straight forward. Let’s have a look at an example where the command defines an environment.

 
Equation

As mentioned before, different environments behave differently and therefore need special handling. As an example, we will now show how to display the total number of equations along with the equation label on the right-hand side of every equation.

There are several ways to do it. In the example below, we use the mathtools package which provides the tag command, allowing to change the equation label. The label change will automatically be reflected when referencing the equations (see pdf output below). For practical reasons and to reduce typing, we implement the command eqn in the preamble. The command takes care of the counters and uses the standard equation environment to display the equation.

\documentclass[11pt]{article}
\usepackage{totcount, mathtools}
\newtotcounter{eqnnum}
\newcommand*{\eqn}[2]{%
	\stepcounter{eqnnum}%
	\stepcounter{equation}%
	\begin{equation}\label{#2}%
		#1\tag{\arabic{equation}/\protect\total{eqnnum}}%
	\end{equation}%
}
\begin{document}
\eqn{f_1(x)=a_1x^2+b_1x+c_1}{eqn:first}
\eqn{f_2(x)=a_2x^2+b_2x+c_2}{eqn:second}
See equation (\ref{eqn:first}) and (\ref{eqn:second}).
\tiny\hfill Created by http://texblog.org
\end{document}

 
Page numbers

See this posts for an example on how to display to the total number of pages in a document, e.g. Page 4 of 65.

Exit mobile version