我该如何纠正这个文件中的 \pageref{LastPage}?

我该如何纠正这个文件中的 \pageref{LastPage}?

我使用包lastpageanswer进行多项选择。这是我的代码

\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} 

这里,file1.tex 是

\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}

并且 file2.tex 是

\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}

我不明白 \pageref{LastPage} 不正确。 在此处输入图片描述

我该如何解决这个问题?

答案1

您必须在解决方案表开始之前设置不同的标签。

在这里我介绍了解决方案的大部分内容,您可以添加其余部分:这突出了解决方案,清楚地说明了真正需要的内容,因此它对其他读者也有用。

最好避免在文档正文中使用“明确的”文件名;因此我使用\AtBeginDocument\solutionsheet在其旁边创建来定义答案文件:编辑会更容易。

这个想法是在执行之前\solutionsheet发出一个并从 1 重新开始编号。我没有使用页脚中的明确命令,而是使用可以通过重新定义的宏。\label\clearpage\pageref\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}

附有解答页的几张试题表如下:

\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}

这将打印

  • 第 1 页,共 2 页
  • 第 2 页,共 2 页
  • 第 1 页,共 1 页
  • 第 1 页,共 2 页
  • 第 2 页,共 2 页
  • 第 1 页,共 1 页

或者,对问题表进行连续编号

\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}

这将打印

  • 第 1 页,共 4 页
  • 第 2 页,共 4 页
  • 第 1 页,共 1 页
  • 第 3 页,共 4 页
  • 第 4 页,共 4 页
  • 第 1 页,共 1 页

相关内容