[Tex/LaTex] Reset page numbering and total number of pages per section

numberingpage-numbering

This question is similar to: How do I have two sets of page numbers in a document?

I have an exam class document (built on article) that contains the exam questions in a first section* and extra output/figures in a second section*. Both are in the same document in order to be able to refer to the correct labels using ref{}. Yet, both sections will be printed separately and should contain page numbers like page 3 of 12.

I use fancyhdr to get that footer correctly, but I have a problem getting the correct numpages. Now it counts only the number of pages for the last section (in this case 2). I've been playing with the solution given in the question I link to, but I was hoping there was a simpler solution.

Example :

\documentclass{exam}
\renewcommand{\maketitle}{
    % Get rid of verticalmode errors from exam class
}
% From the cls file we use for the exams
\AtBeginDocument{
    \pagestyle{foot}
    \footer{}{Page \thepage\ / \numpages}{}
}
\usepackage{kantlipsum} % get some text

\begin{document}

You have \numpages~pages

\kant[1-14]

\clearpage
\setcounter{page}{1}

\section*{Another section}

You have \numpages~pages

\kant[1-7]  

\end{document}

Best Answer

Easier to show then explain. \label stores the counter and page from the last \refstepcounter, hence the dummy counter. There are other ways to do this, but they involve \makeatletter ... \makeatother.

\documentclass{exam}
\newcounter{dummy}
\renewcommand{\maketitle}{
    % Get rid of verticalmode errors from exam class
}
% From the cls file we use for the exams
\AtBeginDocument{
    \pagestyle{foot}
    \footer{}{Page \thepage\ / \pageref{first}}{}
}
\usepackage{kantlipsum} % get some text

\begin{document}

You have \pageref{first}~pages

\kant[1-14]

\refstepcounter{dummy}\label{first}% store page number
\clearpage
\footer{}{Page \thepage\ / \numpages}{}
\setcounter{page}{1}

\section*{Another section}

You have \numpages~pages

\kant[1-7]  

\end{document}

You can't mess with \numpages, but you can create a new macro (\secpages) to replace it.

\documentclass{exam}
\newcounter{dummy}
\newcommand{\secpages}{\pageref{first}}
\newcommand{\resetpagecounts}{%
  \refstepcounter{dummy}\label{first}% store page number
  \clearpage
  \let\secpages=\numpages
  \setcounter{page}{1}}


\renewcommand{\maketitle}{
    % Get rid of verticalmode errors from exam class
}
% From the cls file we use for the exams
\AtBeginDocument{
    \pagestyle{foot}
    \footer{}{Page \thepage\ / \secpages}{}
}
\usepackage{kantlipsum} % get some text

\begin{document}

You have \secpages~pages

\kant[1-14]

\resetpagecounts
\section*{Another section}

You have \secpages~pages

\kant[1-7]  

\end{document}
Related Question