我正在开发标准化测试的格式化系统。在测试的每个部分中,都有一组连续的问题,我可以选择将它们包含在环境中。
\begin{passagequestions}
\begin{question} % question 1, or whichever this is.
\end{question}
% and so on
\end{passagequestions}
无论何时使用,questioncount
外部都会提供计数器。假设所做的只是增加问题编号,即通过。passagequestions
question
\stepcounter{questioncount}
在每个 实例的开头passagequestions
,我想命名属于该问题组的所有问题。 每个 实例passagequestions
都应以 开头Questions {0}--{1} are based on the following passage.
,其中{0}
是第一个问题的索引(可能是 1,也可能不是),{1}
是最后一个问题的索引。
我以前的解决方案是使用totcount
来跟踪问题组的长度,但我的印象是那不是一个好主意。
答案1
编辑请参阅最后的更好版本。
与 Werner 类似,但我使用的是假标签,一旦hyperref
使用就会破裂。
\documentclass{article}
\usepackage{refcount}
\newcounter{passage}%
\newcounter{question}
\newenvironment{question}{\refstepcounter{question}}{}
\makeatletter
\newenvironment{passagequestions}{%
\refstepcounter{passage}%
% Store the fake label for the beginning
\immediate\write\@auxout{%
\string\newlabel{passagestart::\number\value{passage}}{{\number\value{question}}{\thepage}}%
}%
Questions \getrefnumber{passagestart::\number\value{passage}} -- \getrefnumber{passageend::\number\value{passage}} are based on the following passage.
}{%Store the fake label for the end
\immediate\write\@auxout{%
\string\newlabel{passageend::\number\value{passage}}{{\number\value{question}}{\thepage}}%
}%
}
\makeatother
\begin{document}
\begin{passagequestions}
\begin{question}
\end{question}
\begin{question}
\end{question}
\begin{question}
\end{question}
\begin{question}
\end{question}
\end{passagequestions}
\begin{passagequestions}
\begin{question}
\end{question}
\begin{question}
\end{question}
\begin{question}
\end{question}
\begin{question}
\end{question}
\end{passagequestions}
\end{document}
编辑更好的版本
\documentclass{article}
\usepackage{xpatch}
\usepackage{refcount}
\usepackage{xcolor}
\newif\ifnewpassage
\newpassagefalse
\newcounter{passage}%
\newcounter{question}
%Define some dummy question environment
\newenvironment{question}{%
\vskip0.3\abovedisplayskip
\refstepcounter{question}%
\colorbox{yellow}{\bfseries \textcolor{blue}{Question \thequestion}}%
\addvspace{0.5\baselineskip}
}{\vskip0.5\belowdisplayskip}
% Add some code to the question environment
\makeatletter
\newcommand{\storefakelabel}[3][\number\value{passage}]{%
\immediate\write\@auxout{%
\string\newlabel{#2#1}{{#3}{\thepage}}%
}%
}
\newcommand{\passagestartprefix}{passagestart:}
\newcommand{\passageendprefix}{passageend:}
\newcommand{\getstartquestion}{%
\getrefnumber{\passagestartprefix\number\value{passage}}
}
\newcommand{\getendquestion}{%
\getrefnumber{\passageendprefix\number\value{passage}}%
}
\xapptocmd{\question}{% Append the stuff after(!!!) the question startup code has been done!
\ifnewpassage%
\storefakelabel{\passagestartprefix}{\number\value{question}}%
\global\newpassagefalse%
\fi%
}{}{}
\newcommand{\passagequestionsheading}{%
\colorbox{green}{Questions \getstartquestion -- \getendquestion\ are based on the following passage.}
}
\newenvironment{passagequestions}{%
\vskip\abovedisplayskip%
\newpassagetrue% Started a new passage
\refstepcounter{passage}%
\colorbox{red}{\large \bfseries Questions passage \thepassage}%
\passagequestionsheading
}{%
% Store the fake label for the end
\storefakelabel{\passageendprefix}{\number\value{question}}%
\vskip\belowdisplayskip%
}
\makeatother
\begin{document}
\begin{passagequestions}
\begin{question}
Why does \LaTeXe\ provide that much fun?
\end{question}
\begin{question}
\end{question}
\begin{question}
\end{question}
\begin{question}
\end{question}
\end{passagequestions}
\begin{passagequestions}
\begin{question}
\end{question}
\begin{question}
\end{question}
\begin{question}
\end{question}
\begin{question}
\end{question}
\end{passagequestions}
\end{document}
答案2
以下是对您的问题的非正式解决方案:
用
passagequestions
\label
回忆一下环境开始时的这些
\label
s 。\ref
passagequestions
您必须确保计数器步进能够在其被调用的环境中生存。虽然这是自然情况,但在系统中使用的步进计数器值的重新定义却\@currentlabel
不能。\label
\ref
\documentclass{article}
\newcounter{question}
\newcounter{passagequestions}
\newenvironment{passagequestions}
{\stepcounter{passagequestions}%
\par\addvspace{\bigskipamount}\noindent
Questions \ref{pq-\thepassagequestions-start}--\ref{pq-\thepassagequestions-end} are based on the following passage\ldots
\par
\renewcommand{\question}{%
\oldquestion%
\label{pq-\thepassagequestions-start}%
\global\let\question\oldquestion}}%
{\label{pq-\thepassagequestions-end}}
\makeatletter
\newenvironment{question}
{\stepcounter{question}%
\xdef\@currentlabel{\thequestion}%
\par\addvspace{\baselineskip}%
\textbf{Question~\thequestion}:}
{}
\makeatother
\let\oldquestion\question
\begin{document}
\begin{passagequestions}
This is the first passage\ldots
\begin{question}
First question
\end{question}
\begin{question}
Second question
\end{question}
\begin{question}
Third question
\end{question}
\end{passagequestions}
\begin{passagequestions}
This is the second passage \ldots
\begin{question}
First question
\end{question}
\begin{question}
Second question
\end{question}
\begin{question}
Third question
\end{question}
\end{passagequestions}
\end{document}