[Tex/LaTex] How to correct the \pageref{LastPage} in this file

lastpage

I use packages lastpage and answer to make multiple choice. This is my code

\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}
\usepackage{fouriernc}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{xparse}
\usepackage[paperwidth=21cm, paperheight=29cm, left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{multicol}
\theoremstyle{definition}
\usepackage{fancyhdr}
\usepackage{lastpage}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}

\fancyhf{}
\fancyfoot[R]{%
Page \thepage\
 of \pageref{LastPage}}
\input{random}
\ExplSyntaxOn
\cs_new_eq:NN \knuthshuffle_get_random:Nnn \setrannum

\tl_new:N \l_knuthshuffle_tempa_tl
\tl_new:N \l_knuthshuffle_tempb_tl
\int_new:N \l_knuthshuffle_random_int
\seq_new:N \l_knuthshuffle_permutation_seq

\NewDocumentCommand{\inputpermutation}{mm}
 {% #1 is the number of files, #2 is the common prefix
  \knuthshuffle_generate:n { #1 }
  \ferrard_input:n { #2 }
 }

\cs_new_protected:Nn \knuthshuffle_generate:n
 {
  \int_step_inline:nnnn { 1 } { 1 } { #1 }
   {
    \tl_clear_new:c { l_knuthshuffle_##1_element_tl }
    \tl_set:cn { l_knuthshuffle_##1_element_tl } { ##1 }
   }
  \prop_set_eq:NN \l_knuthshuffle_newperm_prop \g_knuthshuffle_identity_prop
  \int_step_inline:nnnn { #1 } { -1 } { 2 }
   {
    \knuthshuffle_get_random:Nnn \l_knuthshuffle_random_int { 1 } { ##1 }
    \tl_set_eq:Nc \l_knuthshuffle_tempa_tl
     { l_knuthshuffle_##1_element_tl }
    \tl_set_eq:Nc \l_knuthshuffle_tempb_tl
     { l_knuthshuffle_ \int_to_arabic:n \l_knuthshuffle_random_int _element_tl }
    \tl_set_eq:cN { l_knuthshuffle_##1_element_tl }
     \l_knuthshuffle_tempb_tl
    \tl_set_eq:cN { l_knuthshuffle_ \int_to_arabic:n \l_knuthshuffle_random_int _element_tl }
     \l_knuthshuffle_tempa_tl
   }
  \seq_clear:N \l_knuthshuffle_permutation_seq
  \int_step_inline:nnnn { 1 } { 1 } { #1 }
   {
    \seq_put_right:Nv \l_knuthshuffle_permutation_seq { l_knuthshuffle_##1_element_tl }
   }
%  \seq_show:N \l_knuthshuffle_permutation_seq % for debugging
 }

\cs_new_protected:Npn \ferrard_input:n #1
 {
  \seq_map_inline:Nn \l_knuthshuffle_permutation_seq
   {
    \input{#1##1}\par
   }
 }
\ExplSyntaxOff
\usepackage{environ}
\usepackage[normalem]{ulem}
\makeatletter
\newlength\choiceitemwidth
\newif\ifshowsolution \showsolutiontrue
\newcounter{choiceitem}
\def\thechoiceitem{\Alph{choiceitem}}
\def\makechoicelabel#1{#1\fbox{\thechoiceitem.}\else\thechoiceitem.\fi\space}
\def\choice@mesureitem#1{\cr\stepcounter{choiceitem}\makechoicelabel#1}
\def\choicemesureitem{\@ifstar
  {\choice@mesureitem\ifshowsolution}%
  {\choice@mesureitem\iffalse}}%
\def\choice@blockitem#1{%
  \ifnum\value{choiceitem}>0\hfil\fi
  \egroup\hskip0pt
  \hbox to \choiceitemwidth\bgroup\refstepcounter{choiceitem}\makechoicelabel#1}
\def\choiceblockitem{\@ifstar
  {\choice@blockitem\ifshowsolution}%
  {\choice@blockitem\iffalse}}
\def\choice@paraitem#1{%
  \par\refstepcounter{choiceitem}\makechoicelabel#1}
\def\choiceparaitem{\@ifstar
  {\choice@paraitem\ifshowsolution}%
  {\choice@paraitem\iffalse}}
\NewEnviron{choices}{%
  \begingroup
    \let\item\choicemesureitem
    \setcounter{choiceitem}{0}%
    \settowidth{\global\choiceitemwidth}{\vbox{\halign{##\hfil\cr\BODY\crcr}}}%
  \endgroup
  \trivlist\item\relax
  \parindent0pt
  \setcounter{choiceitem}{0}%
  \ifdim\choiceitemwidth<0.25\columnwidth
    \choiceitemwidth=0.25\columnwidth
    \let\item\choiceblockitem
    \bgroup\BODY\hfil\egroup
  \else\ifdim\choiceitemwidth<0.5\columnwidth
    \choiceitemwidth=0.5\columnwidth
    \let\item\choiceblockitem
    \bgroup\BODY\hfil\egroup
  \else % \choiceitemwidth > 0.5\columnwidth
    \let\item\choiceparaitem
    \BODY
  \fi\fi
  \endtrivlist
}
\makeatother

\usepackage{answers}
\theoremstyle{definition}
\newtheorem{ex}{Exame}
\Newassociation{sol}{Solution}{ans}
\renewcommand{\Solutionlabel}[1]{\textbf{Answer #1.}}

\begin{document}
\Opensolutionfile{ans}[ans1]
\setcounter{ex}{0}
\inputpermutation{2}{file}
\inputpermutation{2}{file}
\inputpermutation{2}{file}
\inputpermutation{2}{file}
\inputpermutation{2}{file}
\inputpermutation{2}{file}
\inputpermutation{2}{file}
\inputpermutation{2}{file}
\inputpermutation{2}{file}
\inputpermutation{2}{file}
\newpage
\setcounter{page}{1}
\Closesolutionfile{ans}
\section{Solutions}
\input{ans1}
\end{document} 

Here, file1.tex is

\begin{ex}
Find the correct answer
\begin{choices}
\item The first line.
\item The first line.
\item The first line.
\item* This is the answer.
\end{choices}
\begin{sol}
D
\end{sol}
\end{ex}

and file2.tex is

\begin{ex}
Find the correct answer
\begin{choices}
\item* This is the answer.
\item This is a quetion.
\item This is a quetion.
\item TThis is a quetion.
\end{choices}
\begin{sol}
A
\end{sol}
\end{ex}

I don't understand the \pageref{LastPage} dosn't correct.
enter image description here

How can I fix this case?

Best Answer

You have to set a different label just before the solution sheet begins.

Here I present the bulk of the solution, you can add the rest: this highlights the solution, making it clear what's really necessary, so it can be useful also to other readers.

It's better to avoid “explicit” file names in the body of the document; so I define the answer file with \AtBeginDocument and create \solutionsheet next to it: editing will be easier.

The idea is that \solutionsheet emits a \label before doing the \clearpage and restarting numbering from 1. Instead of an explicit \pageref command in the footer, I use a macro that can be redefined by \solutionsheet.

\documentclass{article}

\usepackage{answers}
\usepackage{fancyhdr}
\usepackage{lastpage}

\pagestyle{fancy}
\fancyhf{}
\fancyfoot[R]{Page \thepage\ of \lastpageref}
\renewcommand{\headrulewidth}{0pt}

% normal value to be changed later
\newcommand{\lastpageref}{\pageref{LastPageOfQuestions}}

% for the solution sheet
\AtBeginDocument{\Opensolutionfile{ans}[ans1]}
\newcommand{\solutionsheet}{%
  \label{LastPageOfQuestions}%
  \clearpage
  \renewcommand{\lastpageref}{\pageref{LastPage}}%
  \setcounter{page}{1}%
  \Closesolutionfile{ans}%
  \section{Solutions}%
  \input{ans1}%
}

\begin{document}

Page of questions 1

\clearpage

Page of questions 2

\solutionsheet

\end{document}

For several question sheets with the attached solution pages:

\documentclass{article}

\usepackage{answers}
\usepackage{fancyhdr}
\usepackage{totcount}
\usepackage{lastpage}

\pagestyle{fancy}
\fancyhf{}
\fancyfoot[R]{Page \thepage\ of \lastpageref}
\renewcommand{\headrulewidth}{0pt}

% normal value to be changed later
\newcommand{\lastpageref}{\pageref{LastPageOfQuestions\totalquestionsheets}}
\let\questionpageref\lastpageref

\newcounter{questionsheet}
\regtotcounter{questionsheet}
\makeatletter
\AtBeginDocument{%
  \edef\totalquestionsheets{%
    \@ifundefined{c@questionsheet@totc}{0}{\the\totvalue{questionsheet}}%
  }%
}
\makeatother

\newcommand{\questionsheet}{%
  \setcounter{page}{1}%
  \let\lastpageref\questionpageref
  \stepcounter{questionsheet}%
  \Opensolutionfile{ans}[ans\thequestionsheet]%
}
\newcommand{\solutionsheet}{%
  \label{LastPageOfQuestions\thequestionsheet}%
  \clearpage
  \ifnum\totalquestionsheets=\value{questionsheet}
    \renewcommand{\lastpageref}{\pageref{LastPage}}%
  \else
    \renewcommand{\lastpageref}{\pageref{LastPageOfAnswers\thequestionsheet}}%
  \fi
  \setcounter{page}{1}%
  \Closesolutionfile{ans}%
  \section{Solutions}%
  \input{ans\thequestionsheet}%
  \label{LastPageOfAnswers\thequestionsheet}
  \clearpage
}

\begin{document}

\questionsheet

Page of questions 1

\clearpage

Page of questions 2

\solutionsheet

\questionsheet

Page of questions 3

\clearpage

Page of questions 4

\solutionsheet

\end{document}

This will print

  • Page 1 of 2
  • Page 2 of 2
  • Page 1 of 1
  • Page 1 of 2
  • Page 2 of 2
  • Page 1 of 1

Alternatively, for numbering consecutively the question sheets

\documentclass{article}

\usepackage{answers}
\usepackage{fancyhdr}
\usepackage{totcount}
\usepackage{lastpage}

\pagestyle{fancy}
\fancyhf{}
\fancyfoot[R]{Page \thepage\ of \lastpageref}
\renewcommand{\headrulewidth}{0pt}

% normal value to be changed later
\newcommand{\lastpageref}{\pageref{LastPageOfQuestions\totalquestionsheets}}
\let\questionpageref\lastpageref

\newcounter{questionsheet}
\regtotcounter{questionsheet}
\makeatletter
\AtBeginDocument{%
  \edef\totalquestionsheets{%
    \@ifundefined{c@questionsheet@totc}{0}{\the\totvalue{questionsheet}}%
  }%
}
\makeatother

\newcounter{savepage}

\newcommand{\questionsheet}{%
  \let\lastpageref\questionpageref
  \stepcounter{questionsheet}%
  \Opensolutionfile{ans}[ans\thequestionsheet]%
}
\newcommand{\solutionsheet}{%
  \label{LastPageOfQuestions\thequestionsheet}%
  \clearpage
  \ifnum\totalquestionsheets=\value{questionsheet}
    \renewcommand{\lastpageref}{\pageref{LastPage}}%
  \else
    \renewcommand{\lastpageref}{\pageref{LastPageOfAnswers\thequestionsheet}}%
  \fi
  \setcounter{savepage}{\value{page}}%
  \setcounter{page}{1}%
  \Closesolutionfile{ans}%
  \section{Solutions}%
  \input{ans\thequestionsheet}%
  \label{LastPageOfAnswers\thequestionsheet}
  \clearpage
  \ifnum\totalquestionsheets=\value{questionsheet}
  \else
    \setcounter{page}{\value{savepage}}
  \fi
}

\begin{document}

\questionsheet

Page of questions 1

\clearpage

Page of questions 2

\solutionsheet

\questionsheet

Page of questions 3

\clearpage

Page of questions 4

\solutionsheet

\end{document}

This will print

  • Page 1 of 4
  • Page 2 of 4
  • Page 1 of 1
  • Page 3 of 4
  • Page 4 of 4
  • Page 1 of 1