1

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}

1 Answer 1

2

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}
4
  • Thanks! That's a neat way of working around it. It solves my problem for now, but ultimately I should get this into the cls file so my colleagues can simply insert \resetpagecounts to get the correct result. I got things working more or less based on the zref and atenddvi trick from the other question, but it still feels overly complicated. If you have a more concise solution that could fit in a cls, feel free to add it.
    – Joris Meys
    Commented Jan 10, 2019 at 16:21
  • Thanks again, exactly what I was looking for.
    – Joris Meys
    Commented Jan 10, 2019 at 17:50
  • @john Kormylo I did that and I lose the total number of pages of the document, that I also need. could you please help me? I need to have something like A1/50, so I need the page inside sections and the total number. I have this question open, can you have a look please?
    – JCV
    Commented Nov 15, 2022 at 16:28
  • @JenifferBarreto - I would be very surprised if something intended for exam class worked for KOMA. Commented Nov 15, 2022 at 20:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .