[Tex/LaTex] Counting pages of sections

countersnumberingsectioning

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[automark,headsepline,footsepline]{scrpage2}
\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

% header stuff
\ihead{\rightmark}
\chead{}
\ohead{Sectionpage \arabic{sectionpagecounter}}
\ifoot{}
\cfoot{}
\ofoot{\pagemark}
\pagestyle{scrheadings}


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

Best Answer

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

\documentclass{scrartcl}
\usepackage[automark,headsepline,footsepline]{scrpage2}
\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

\ihead{\rightmark}
\chead{}
\ohead{Sectionpage \arabic{sectionpagecounter} of \number\value{section\romannumeral\value{section}}}
\ifoot{}
\cfoot{}
\ofoot{\pagemark}
\pagestyle{scrheadings}

\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.

Related Question