Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is there some counter that holds the number of \chapter that are in a \part? Or do I need to define and run my own counter?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

No such counter exists, as far as I know. However, a running count of the number of chapters within a part can easily be done (see below).

Accessing the total number of chapters within a part at any point of the document (e.g. at the very beginning of a the part in question) is a little trickier. That would probably require running a first pass, retrieving information on the number of chapters from the auxiliary files, processing it, and running a second pass. I don't know how to do that.

\documentclass{book}

\newcounter{totalch}

\let\oldpart\part
\renewcommand{\part}{\setcounter{totalch}{0}\oldpart}

\let\oldchapter\chapter
\renewcommand{\chapter}{\addtocounter{totalch}{1}\oldchapter}

\begin{document}

\part{foo}
\chapter{one}
\chapter{two}
\chapter{three}
\thetotalch    %returns 3, here

\part{bar}
\chapter{four}
\chapter{five}
\thetotalch    %returns 2, here

\end{document}
share|improve this answer
    
Thank you very much. :) –  Foo Bar Apr 22 '13 at 16:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.