…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.

Ruslan Kiianchuk (@zoresvit)

The first trick with references almost never works since there may be several \cite to the same reference. Is there a way to take that into account?

tom

True, haven’t thought of that. Thanks for pointing it out. I wonder if there is a workaround.

q

check this out:

tom

Awesome, thanks!

Here is a minimum working example using the solution provided by q:

Preetham Aghalaya Manjunatha

Dear Tom,

Thanks a lot for your efforts. I am badly looking for a way to have a chapter counter from high to low i.e.

Chapter n

Chapter n-1

Chapter n-2 and so on. Please let me know if any work around exists for that. I really appreciate your help.

Thank you,

tom

Hi!

Took me a moment to figure this out :-). Here is the code:

Preetham Aghalaya Manjunatha

Dear Tom,

You are my saviour! It took hours and days to work around this, I no where found it on internet.

Thank you again.

tom

glad i could help. i might write a short post about it as a similar solution does not seem to exist online yet.

Preetham Aghalaya Manjunatha

Dear Tom,

But one thing I tested is by using,

%\setcounter{chapter}{10} or to start with some given number. Suppose there are 40 chapters in total.

When I compile this, it never starts from the chapter 10 to chapter n (say n=50). Instead it starts from chapter 1 to 40 (descending). Is there any workaround for this?

Thanks and I appreciate your time.

tom

Hmmm, not sure I understand what you are trying to do. You want chapter numbering to start from 50 – 10 instead of 40 – 1?

If so, try: