4

I found that totcount gives the wrong chapter count when appendices are present. In the following MWE, the chapter count should be 3 but is actually 1:

\documentclass{report}
\usepackage{totcount}
\regtotcounter{chapter}
\begin{document}
There are \total{chapter} chapters.
\chapter{A}
\chapter{B}
\appendix
\chapter{C}
\end{document} 

When \appendix is commented out, the count is correct. Am I misusing the package, making a mistake, or this is a bug?

user1362373
  • 2,859

1 Answers1

4

Edit note: The assoccnt package has a successor named xassoccnt which is extended compared to assoccnt. It incorporates some features that totcount provides too.

The package assoccnt can be used to step an associated counter each time another ('master') counter is stepped, regardless if the master counter is reset in between. In conjunction with totcount the task is easily solved, but don't reset (or change) the associated counter manually of course.

The reason why totcount itself can't do this is not totcounts fault. The \appendix command resets the chapter counter and after one \chapter in the appendix, the chapter counter value is 1, this value is written to the .aux file in the end, making totcount believe, there's only one chapter in the file finally.

assoccnt 'hacks' into \addtocounter{...}{} (the basic command behind \stepcounter or \refstepcounter and increases the driven counter simultaneously. This even works if the master counters are reset with chngcntr etc. or \@addtoreset features.

Basically any counter can be used as master counters and there might be more than one of them. In the end, each master counter can have many driven (associated) counters themself. (However, the page counter is little bit tricky!)

\documentclass{report}
\usepackage{totcount}
\usepackage{assoccnt}

\newtotcounter{totalchapters}
\DeclareAssociatedCounters{chapter}{totalchapters} % Associate the driven counter `totalchapters` to the master counter `chapter`
\begin{document}
There are \total{totalchapters} chapters.
\chapter{A}
\chapter{B}
\appendix
\chapter{C}
\chapter{D}
\chapter{E}
\chapter{F}
\chapter{G}
\chapter{H}
\chapter{I}
\end{document}

enter image description here