Consider the following MWE:
\documentclass[12pt,twoside,letterpaper]{report}
\usepackage{etoolbox}
\newcounter{totchapters}
\providecommand\totchap{}
\makeatletter
\AtEndDocument{%
\addtocounter{totchapters}{\value{chapter}}%
\immediate\write\@mainaux{%
\string\gdef\string\totchap{\number\value{totchapters}}%
}%
}
\makeatother
\pretocmd{\chapter}{\addtocounter{totchapters}{\value{chapter}}\setcounter{chapter}{0}}{}{}
\begin{document}
\totchap \chapter{one}
\chapter{two}
\chapter{three}
\chapter{four}
\end{document}
I am trying to gain access to the total number of chapters in a document. I got inspiration from https://tex.stackexchange.com/a/55583/10898. But for some reason or the other the chapter counter is affected and no longer increasing. Why is that the case and how can it be fixed? Note that I would like to use this total to make calculations.
\total{chapter}to make arithmetic calculations? – azetina May 08 '13 at 21:47\newcommand{\numtotal}[1]{\value{#1@totc}}you can use\numtotal{chapter}(or any other registered counter) in the context of a<number>. The returned value is-1at the first run (totcountuses the .aux file for its workings). – egreg May 08 '13 at 22:04