How can one count the total number of sections within each chapter?
I am trying to use the totcount
package, but it returns the total number of sections of the last chapter, not total number of sections of the current chapter.
In the following MWE, the desired behaviour would be to report 3 sections for the first Chapter, and 1 section for the second and last chapter.
totcount
reports only 1 section (the counter value for the last chapter).
\documentclass[12pt]{book}
\usepackage{totcount}
\regtotcounter{section}
\begin{document}
\chapter*{First Chapter}
The total number of sections in this chapter should be 3.
The totcount package reports: \total{section}
\section{First Section}
First text
\section{Second Section}
Second text
\section{Third Section}
Third text
\chapter*{Last Chapter}
The total number of sections in this chapter should be 1.
The totcount package reports: \total{section}
\section{First and only section}
Section text
\end{document}
How can one count the total number of sections within each chapter?
Note: totcount
reports 0 sections if using \section*
, see Sigur's comment.
Best Answer
This question lead to a new package
cntperchap
which is available in its version 0.2 on CTAN since 2015/9/5This uses the
assoccnt
package (whose author I accidentally know quite well ;-))It associates a counter
totalsections
to thesection
counter -- each time thesection
counter is increased, thetotalsections
counter is stepped as well.However, there is no automatic resetting for
\chapter*
usage. In this case, it can be done automatically by prepending some code to\chapter
using\xpretocmd
fromxpatch
packageNote The author of
assoccnt
should really incorporate this into his package ;-)Edit Some new version, which uses a per chapter count of sections (requires two runs to be successful).
Explanation: Each time a new chapter is used, the accumulated number of sections is written to external file,
\jobname.seccnt
, sayfoo.seccnt
for short. This file is read again at the next latex compile run and the values are stored to aetoolbox
list. The macro\GetTotalSectionCounter
will advance through this list until it's at the right position and will then print out the number of sections in this chapter, even ahead. (The macro should be expandable, I think this is the case)At the moment, it's necessary to manually remove the
foo.seccnt
file if there have been changes to number of chapters/sections.I'll try to get around this drawback.
Edit This is the version without explicit deleting the
foo.seccnt
fileI used the
addtocontents
approach to letLaTeX
write the section numbers to a separate file just as it is done with thetoc
related stuff. Thefoo.seccnt
is then treated as a faked toc, read in before (and the values temporarily stored) rewritten in the run.Edit The OP gsl noted some error in this code. I could track it down to the fact that
\@startfaketoc
tries to read in the externalfoo.seccnt
file in the first run already. This fails of course, since there is no such file if it was deleted or the documentat is compiled for the very first time.