Estou trabalhando em um sistema de formatação para testes padronizados. Dentro de cada seção de um teste, há grupos de questões consecutivas que posso optar por conter dentro de um ambiente.
\begin{passagequestions}
\begin{question} % question 1, or whichever this is.
\end{question}
% and so on
\end{passagequestions}
O balcão questioncount
é disponibilizado no exterior sempre que passagequestions
é utilizado. Suponha que isso question
faça pouco mais do que incrementar o número da pergunta, ou seja, via \stepcounter{questioncount}
.
No início de cada instância de passagequestions
, gostaria de nomear todas as questões que fazem parte do grupo de questões. Cada instância de passagequestions
deve começar com Questions {0}--{1} are based on the following passage.
, onde {0}
é o índice da primeira questão (que pode ou não ser 1) e {1}
é o índice da última questão.
Minha solução anterior começaria usando um totcount
para controlar o comprimento de um grupo de perguntas, mas tive a impressão de queisso não foi uma boa ideia.
Responder1
EditarVeja a melhor versão no final.
Semelhante ao Werner, mas estou usando etiquetas falsas, que podem quebrar assim que hyperref
usadas.
\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}
EditarVersão melhor
\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}
Responder2
Aqui está uma solução informal para sua pergunta:
Marque o início e o fim do
passagequestions
ambiente com um\label
Lembre-se desses
\label
s como\ref
s no início dopassagequestions
ambiente.
Você terá que garantir que a etapa do contador sobreviva ao ambiente em que é chamada. Embora esse seja naturalmente o caso, a redefinição do valor do contador escalonado em \@currentlabel
- usado com o \label
- \ref
sistema - não sobrevive.
\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}