末尾具有给定数量 n 个空白页的环境在 n > 0 时有效,但在 n = 0 时失败

末尾具有给定数量 n 个空白页的环境在 n > 0 时有效,但在 n = 0 时失败

我想构建一个考试环境,允许在每场考试的末尾添加一定数量的空白页。每场考试由 a) 封面、b) 带有问题的主要页面和 c) 末尾的空白页组成(它们的数量作为考试环境的参数指定)。封面应显示考试的页数(包括末尾的空白页)。因此,我在每个考试环境的封面和末尾都贴上标签(差值加 1 就是页数)。只要每场考试末尾的空白页数为正数,这一切都可以正常工作(见下面的考试 1)。但是,如果我选择 0 个这样的页面(见下面的考试 2),则考试的页数为 0(尽管它应该至少为 0,包括封面和至少一页的考试问题)。为什么?

\documentclass{scrartcl}
\usepackage{calc}
\usepackage{refcount}

\newcommand\numberOfPages[1]{\edef\tmp{\number\numexpr\getpagerefnumber{end:page:#1}-\getpagerefnumber{start:page:#1}+1\relax}\tmp}% command for counting the number of pages of an exam

% Counters for the exam environment
\newcounter{examEnvCounter}% counts how often the environment 'exam' was called
\setcounter{examEnvCounter}{0}% initialize
\newcounter{numEmptyPages}% counter for the number of empty pages at the end of each exam

% Environment exam
\newenvironment{exam}[1]{% the argument is the number of empty pages at the end of the exam
  % Counters
  \setcounter{page}{1}% set page number to 1
  \stepcounter{examEnvCounter}% increase counter which counts how many times the exam environment was called

  % Cover/first page
  \thispagestyle{empty}
  \label{start:page:\number\value{examEnvCounter}}% define label for computing number or pages
  {\bfseries Exam~\number\value{examEnvCounter}}
  \par\bigskip
  Number of pages this exam covers (including this cover page): \numberOfPages{\number\value{examEnvCounter}}
  \clearpage

  % Subsequent exam pages should show "Page ... of <number of pages>"
  \renewcommand\pagemark{Page~\thepage~of~\numberOfPages{\number\value{examEnvCounter}}}
  \setcounter{numEmptyPages}{#1}% number of empty pages at the end
}{% Inserting the provided number of empty pages
  \ifnum\value{numEmptyPages} > 0% only enter the loop if we have at least one empty page
  \loop
  \clearpage% create empty page ...
  Extra page at the end of this exam for scratch work, additional answers, etc. % ... with some text
  \addtocounter{numEmptyPages}{-1}% decrease required (remaining) number of empty pages
  \ifnum\value{numEmptyPages} > 0
  \repeat
  \fi% \ifnum

  % Insert label at the end (to determine the number of pages of the exam)
  \label{end:page:\number\value{examEnvCounter}}\clearpage
}

\begin{document}
% Exam 1
\begin{exam}{1}% first call (with one empty page at the end => fine!)
  Some text as placeholder for an exam question.
  \clearpage
\end{exam}

% Exam 2
\begin{exam}{0}% second call (with no empty pages at the end => doesn't determine the number of pages correctly)
  Some text as placeholder for an exam question.
  \clearpage
\end{exam}
\end{document}

答案1

由于您知道要发放的空白页数,因此您只需在考试文本末尾设置一个标签并添加空白页数即可。

\documentclass{scrartcl}
\usepackage{refcount}

\newcommand\numberOfPages[2]{%
  \the\numexpr\getpagerefnumber{end:page:#1}+#2\relax
}

% Counters for the exam environment
\newcounter{examEnvCounter}% counts how often the environment 'exam' was called
\newcounter{numEmptyPages}% counter for the number of empty pages at the end of each exam

% Environment exam
\newenvironment{exam}[1]{% the argument is the number of empty pages at the end of the exam
  \clearpage
  % Counters
  % set page number to 1
  \setcounter{page}{1}%
  % increase counter which counts how many times the exam environment was called
  \stepcounter{examEnvCounter}
  \setcounter{numEmptyPages}{#1}% number of empty pages at the end
  %
  % Cover/first page
  \thispagestyle{empty}%
  \noindent\textbf{Exam~\theexamEnvCounter}%
  \par\bigskip
  Number of pages this exam covers (including this cover page):
  \numberOfPages{\theexamEnvCounter}{#1}%
  \clearpage
  % Subsequent exam pages should show "Page ... of <number of pages>"
  \renewcommand\pagemark{Page~\thepage~of~\numberOfPages{\theexamEnvCounter}{#1}}%
}{%
  % label
  \label{end:page:\theexamEnvCounter}%
  \clearpage
% Inserting the provided number of empty pages
  \ifnum\value{numEmptyPages} > 0% only enter the loop if we have at least one empty page
    \loop
    Extra page at the end of this exam for scratch work, additional answers, etc.
    \clearpage% create empty page ...
    \addtocounter{numEmptyPages}{-1}% decrease required (remaining) number of empty pages
    \ifnum\value{numEmptyPages} > 0
    \repeat
  \fi
}

\begin{document}

% Exam 1
\begin{exam}{1}
  Some text as placeholder for an exam question.
\end{exam}

% Exam 2
\begin{exam}{0}
  Some text as placeholder for an exam question.
  \clearpage
  This exam has two pages, but no additional blank page
\end{exam}

\end{document}

在此处输入图片描述

为了制作这张图片,我使用了 A6 纸。

相关内容