1

I am using the exam document class for creating some tests for my students. Usually in every test the last page is for student's notes so I don't want to include this page in total test pages number. For this I include in the preamble this piece of code just for the last page not to be numbered:

\makeatletter
\def\numpages{\@ifundefined{exam@lastpage}  
  {\mbox{\normalfont\bf ??}}  
  \the\numexpr\exam@lastpage-1\relax}  
\makeatother

Although it actually works, when I compile the whole code for the first time I get the following errors:

You can't use \numexpr' in restricted horizontal mode. ^^I\part { Undefined control sequence. ^^I\part { You can't use \numexpr' in restricted horizontal mode. \end{document} Undefined control sequence. \end{document}

When I compile again the error goes away and I get a Process exited normally message and the code does what it is supposed to do: the last page is not numbered.

There is another detail: When I compile the code for the first time with out the small code above I don't get the error of course, if I paste it after the first compilation the error never appears. The same happens in pdflatex, lualatex and xelatex. Is there a way to avoid this error?

I include a minimal (as minimal as I could do it) example:

\documentclass[12pt]{exam}  
\usepackage{calc}  
\usepackage[a4paper, total={180mm,247mm},left=15mm,top=20mm]{geometry}  
\usepackage{fontenc}  
\pagestyle{headandfoot}  
\firstpageheader{}{}{}  
\runningheader{}{}{}  
\runningheadrule  
\lfoot{}  
\cfoot{}  
\rfoot{Page \thepage\ of \numpages}  
\setlength\dottedlinefillheight{9mm}  

\makeatletter  
\def\numpages{\@ifundefined{exam@lastpage}%  
    {\mbox{\normalfont\bf ??}}%  
    \the\numexpr\exam@lastpage-1\relax}%  
\makeatother  

\begin{document}  
    
    This is the fist page of the exam  
    
    \newpage  
    
    this is the second page of the exam  
    
    \newgeometry{body={185mm,257mm},centering}  
    
    \raggedright{\textbf{\underline{Student Notes:}}  
        
        \fillwithdottedlines{\stretch{2}}    
        
        \thispagestyle{empty}   
        
    \end{document}  

I noticed that also has something to do and with the command \newgeometry{body={185mm,257mm},centering} I have in my code, or the interaction of this command and the small code.

1
  • 1
    Please write a minimal working example. See link for more details what it means.
    – user202729
    Jul 14, 2022 at 12:53

1 Answer 1

1

The code for \numpages is

\def\numpages{\@ifundefined{exam@lastpage}%
  {\mbox{\normalfont\bfseries ??}}%
  \exam@lastpage
}% numpages

Since \exam@lastpage is a single token, braces around it can be omitted (I consider it bad style, though).

Here's the fix:

\makeatletter  
\renewcommand\numpages{\@ifundefined{exam@lastpage}%
    {\mbox{\normalfont\bfseries ??}}%
    {\the\numexpr\exam@lastpage-1\relax}%
}
\makeatother  

Avoid \bf that has been deprecated for about 30 years.

2
  • Thank you egreg! Now it works without the error message. So the problem was mainly the \def command instead of \renewcommand and the braces at the last line?
    – miltos
    Jul 14, 2022 at 16:14
  • @MiltosP. I prefer \renewcommand, but it's not a problem if you use \def. The issue were the missing braces.
    – egreg
    Jul 14, 2022 at 16:21

You must log in to answer this question.

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