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):
The idea is to place some
\label
s 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.