2

I'm working on a formatting system for standardized tests. Within each section of a test, there are groups of consecutive questions that I might choose to contain inside an environment.

\begin{passagequestions}
    \begin{question} % question 1, or whichever this is.
    \end{question}
    % and so on
\end{passagequestions}

The counter questioncount is provided outside whenever passagequestions is used. Assume that question does little more than increment the question number, i.e. via \stepcounter{questioncount}.

At the beginning of each instance of passagequestions, I would like to name all of the questions that are part of the question group. Each instance of passagequestions should begin with Questions {0}--{1} are based on the following passage., where {0} is the index of the first question (which may or may not be 1) and {1} is the index of the last question.

My previous solution was going to begin by using a totcount to keep track of the length of a question group, but I got the impression that that was not a good idea.

1
  • How about a \label approach?
    – user31729
    Jul 29, 2015 at 21:58

2 Answers 2

1

Edit See the better version at the end.

Similar to Werner, but I am using fake labels, which can break as soon as hyperref is used.

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

Edit Better version

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

enter image description here

2
  • What makes the better version better? Jul 30, 2015 at 16:12
  • @SimonKuang: It uses the correct question counter ranges for each passage, the first version just used the old one from a previous passage, but did not increment it. That was wrong
    – user31729
    Jul 30, 2015 at 16:15
1

Here is an informal solution to your question:

  1. Mark the beginning and end of the passagequestions environment with a \label

  2. Recall these \labels as \refs in the start of the passagequestions environment.

You'll have to make sure that counter stepping survives the environment it's called in. While this is naturally the case, redefinition of the stepped counter's value in \@currentlabel - used with the \label-\ref system - does not.

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .