# Counting pages of sections

After building a second page counter that is reset for every section, I wonder how to get the number of pages of a section. Every section starts on a new page.

How can I calculate the number of pages that every section takes and put that in the header like "Sectionpage 1 of 2"?

This is my code so far:

\documentclass[a4paper,oneside]{scrartcl}
\usepackage{etoolbox}
\usepackage{lipsum}

\setcounter{secnumdepth}{-1}

% new counter for pages of section
\newcounter{sectionpagecounter}
\newcommand{\resetsectionpagecount}{\setcounter{sectionpagecounter}{1}}

\makeatletter
\patchcmd\@outputpage{\stepcounter{page}}{\stepcounter{page}\stepcounter{sectionpagecounter}}{}{} % inc for every page
\patchcmd\@xsect{\ignorespaces}{\resetsectionpagecount\ignorespaces}{}{} % reset on new section
\makeatother

\ifoot{}
\cfoot{}
\ofoot{\pagemark}

\begin{document}
\section{My Section A}
\lipsum
\clearpage
\section{My Section B}
\lipsum
\clearpage
\section{My Section C}
\lipsum
\end{document}

• I don't know of a package that does that, but the lastpage package allows access to the number of the document's last page. I'm sure ideas from that package could be adapted (by competent people, i.e. not me) to solve problems such as yours. – jub0bs May 30 '13 at 22:34
• Thank you for that idea, I looked into it and it seems that lastpage uses the aux file to remember the number of pages in the first run and inserts the result in the second run. I'll try to adopt that to my problem. – ofenrohr Jun 2 '13 at 20:45
• Don't hesitate to post your code here if you solve your problem! :) – jub0bs Jun 2 '13 at 20:47
• I'll do that for sure. If anyone stumbles upon this, there should be an answer. Finding unanswered questions is very annoying... – ofenrohr Jun 2 '13 at 20:51

Nice problem. Here's one first solution that admits improvements (see the notes below the code):

\documentclass{scrartcl}
\usepackage{refcount}
\usepackage{etoolbox}
\usepackage{lipsum}
\usepackage{pgffor}

% new counter for pages of section
\newcounter{sectionpagecounter}
\newcommand{\resetsectionpagecount}{\setcounter{sectionpagecounter}{1}}

\makeatletter
\patchcmd\@outputpage{\stepcounter{page}}{\stepcounter{page}\stepcounter{sectionpagecounter}}{}{} % inc for every page
\patchcmd{\@xsect}{\ignorespaces}{\resetsectionpagecount\ignorespaces}{}{} % reset on new section
\pretocmd{\section}{\SectionPage}{}{}
\makeatother

\ifoot{}
\cfoot{}
\ofoot{\pagemark}

\newcounter{tmp}
\stepcounter{tmp}
% We create 100 new counters: sectioni, sectionii,...,sectionic, sectionc
\loop
\ifnum\value{tmp}<101
\newcounter{section\romannumeral\value{tmp}}
\stepcounter{tmp}
\repeat

% command to label each section using its counter
\newcommand\SectionLab{\label{\thesection}}
% the main command: gets the number of pages by subtracting the number of pages
% between two consecutive labels (i.e., between two \section commands)
\newcommand\SectionPage{%
\setcounter{section\romannumeral\number\numexpr\value{section}+1\relax}{%
\number\numexpr\getpagerefnumber{\number\numexpr\thesection+2\relax}
-\getpagerefnumber{\number\numexpr\thesection+1\relax}\relax}%
}

% We need a last label at the end of the document
\newcounter{Lastpg}
\AtBeginDocument{%
\AtEndDocument{%
\setcounter{Lastpg}{\value{page}}
\stepcounter{page}\label{\number\numexpr\thesection+1\relax}}%
\setcounter{page}{\value{Lastpg}}
}%

\begin{document}

\section{Test Section One}
\SectionLab
\lipsum[1-72]

\clearpage
\section{Test Section Two}
\SectionLab
\lipsum[1-2]

\clearpage
\section{Test Section Three}
\SectionLab
\lipsum[1-40]

\clearpage
\section{Test Section Four}
\SectionLab
\lipsum[1-60]

\end{document}


The idea is to place some \labels at each \section command and use this labels to calculate the number of pages; this number is stored in a separate counter (in its present form, the code allows for 100 sections). There are still some things that can be improved and I will try to do so over the next days:

• Each time a \section is issued, one has to add \SectionLab immediately after the \section command (this can be easily automated).

• The current method used in the code posted in the question to reset the counter for pages is not optimal, since it hooks into \@xsect without enough care, and lower sectioning commands \subsection, for example) will also reset the counter.

Last night I solved the problem by writing labels to the aux file:

\documentclass[a4paper,oneside]{scrartcl}
\usepackage{etoolbox}
\usepackage{lipsum}

\setcounter{secnumdepth}{-1}

% new counter for sections (why is there no section counter?)
\newcounter{sectioncounter}
% new counter for pages of section
\newcounter{sectionpagecounter}
\newcommand{\resetsectionpagecount}{\setcounter{sectionpagecounter}{1}}
\newcommand{\sectionpagescount}{\pageref{test\arabic{sectioncounter}}}

\makeatletter
\patchcmd\@outputpage{\stepcounter{page}}{\stepcounter{page}\stepcounter{sectionpagecounter}}{}{} % inc for every page
\patchcmd\@xsect{\ignorespaces}{\addtocounter{sectionpagecounter}{-1}\immediate\write\@auxout{\string\newlabel{test\arabic{sectioncounter}}{{}{\arabic{sectionpagecounter}}}}\resetsectionpagecount\stepcounter{sectioncounter}\ignorespaces}{}{} % reset on new section
\makeatother

\ifoot{}
\cfoot{}
\ofoot{\pagemark}

\begin{document}

\section{Test Section One}
\lipsum[1-72]

\clearpage
\section{Test Section Two}
\lipsum[1-2]

\clearpage
\section{Test Section Three}
\lipsum[1-40]

\clearpage
\section{Test Section Four}
\lipsum[1-60]

\end{document}


The labels are named 'testSECTIONNUMBER'. pdflatex has to be run at least twice.

• Of course there is a section counter: section`. – mafp Jun 3 '13 at 9:17
• I keep getting errors of the kind Extra \else ... whenever an environment crosses a page boundary. It compiles to a correct document, but I'd like a way of solving this. The problem seems to be using the \rhead command. – fabikw Nov 8 '16 at 18:45