# Total count of Theorem like environments - In all chapters

I am using the following structure to define my custom theorem-like Prob environment using the thmtools package.

I want to include the total number of problems in the introduction. For this I am using totcount package. This MWE shows \total{Prob} as 3, but it should be 5 (2+3).

Please guide me how to define \theProb so that \total{Prob} can give total number of problems from Chapter 1 and Chapter 2.

\documentclass{memoir}

\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{totcount}

\declaretheoremstyle[
notefont=\mdseries, notebraces={(}{)},
bodyfont=\normalfont,
]{probStyle}

\declaretheorem[style=probStyle,name={},numberwithin=chapter]{Prob}
\renewcommand{\theProb}{\thesection.\arabic{Prob}}

\regtotcounter{Prob}

\begin{document}

\frontmatter

\chapter{Introduction}

This book contains \total{Prob} Problems

\mainmatter

\chapter{First}

\section{Exercises}

\begin{Prob}
Problem 1 Chap 1

\end{Prob}

\begin{Prob}
Problem 1 Chap 1

\end{Prob}

\chapter{Second}

\section{Another Exercise}

\begin{Prob}
Problem 1 Chap 2

\end{Prob}

\begin{Prob}
Problem 2 Chap 2

\end{Prob}

\begin{Prob}
Problem 3 Chap 2

\end{Prob}

\end{document}

• Technically it's not really about \theProb (which only displays the count); but \c@Prob, the LaTeX-level macro that handles the count. Commented Dec 29, 2016 at 15:40
• Here \theProb resets with each chapter. \total{Prob} is reporting the last one. How to count such counter across all chapters, to get total count finally. Commented Dec 29, 2016 at 15:44
• thats exactly the problem. A possible algorithm would be to store all the counter values before each reset, in this case the \cahpters, and add them together in the end. I wonder if this has been implemented alredy or if there is a clever way that is not so brute force style. Commented Dec 29, 2016 at 15:49

The resetting feature of counters is a point where totcount may fail.

The xassoccnt package associates a second counter to the Prob counter and increases this totalprobs counter every time Prob is increased, but the totalprobs counter is not reset automatically.

Declaring it as \NewTotalDocumentCounter gives the value via \TotalValue right at any place in the document body, since the last value is stored at the end of the previous run.

\documentclass{memoir}

\usepackage{amsthm}
\usepackage{thmtools}

\usepackage{xassoccnt}

\declaretheoremstyle[
notefont=\mdseries, notebraces={(}{)},
bodyfont=\normalfont,
]{probStyle}

\declaretheorem[style=probStyle,name={},numberwithin=chapter]{Prob}

\NewTotalDocumentCounter{totalprobs}
\DeclareAssociatedCounters{Prob}{totalprobs}
\renewcommand{\theProb}{\thesection.\arabic{Prob}}

\begin{document}

\frontmatter

\chapter{Introduction}

This book contains \TotalValue{totalprobs} Problems

\mainmatter

\chapter{First}

\section{Exercises}

\begin{Prob}
Problem 1 Chap 1

\end{Prob}

\begin{Prob}
Problem 1 Chap 1

\end{Prob}

\chapter{Second}

\section{Another Exercise}

\begin{Prob}
Problem 1 Chap 2

\end{Prob}

\begin{Prob}
Problem 2 Chap 2

\end{Prob}

\begin{Prob}
Problem 3 Chap 2

\end{Prob}

\end{document}

• @RamaKrishnaMajety: You're welcome. Happy TeXing!
– user31729
Commented Dec 29, 2016 at 15:52
• Nice! "Look son, I got a package fot this..." :) +1 anyways and welcome back Commented Dec 29, 2016 at 15:53
• @Ruben: Thanks ;-) xassoccnt has some nice features. And I know the author personally (at least I think so ;-))
– user31729
Commented Dec 29, 2016 at 15:54
• Haha, I hope so... I'll check the code briefly to see the features Commented Dec 29, 2016 at 15:55
• @Ruben: Oh my, don't check the code , rather read the manual. The code is very 'hidden'
– user31729
Commented Dec 29, 2016 at 15:57

Declare a new total counter allprobs

\newtotcounter{allprobs}


and increment it each time you use the theorem by adding the following option to \declaretheorem:

\declaretheorem[...,postheadhook=\stepcounter{allprobs}]{Prob}


Then you can access the total number of these theorem-like environments by \total{allprobs}.