我正在開發用於標準化測試的格式化系統。在測試的每個部分中,我可能會選擇將一組連續的問題納入環境中。
\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}
是最後一個問題的索引。
我之前的解決方案是先使用 atotcount
來追蹤問題組的長度,但我的印像是這不是一個好主意。
答案1
編輯更好的版本見最後。
與維爾納類似,但我使用的是假標籤,一旦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
用 a標記環境的開始和結束\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}