1

I am typesetting an exam question like

\documentclass{article}
\newenvironment{answer}{\par[}{]}
\newenvironment{answersonly}{}{}
\begin{document}
\begin{answersonly}
\begin{enumerate}
\item Question 1
  \begin{answer}Answer 1\end{answer}
\item
  \begin{enumerate}
  \item Question 2a
    \begin{answer}Answer 2a\end{answer}
  \item Question 2b\par more details
    \begin{answer}Answer 2b\end{answer}
  \end{enumerate}
\end{enumerate}
\end{answersonly}
\end{document}

resulting in typeset output like

1. Question 1
   [Answer 1]
2. (a) Question 2a
       [Answer 2a]
   (b) Question 2b
       more details
       [Answer 2b]

I would now like to switch the typesetting of this document into a mode where it prints only the answers, but not the questions, while keeping the nested enumeration intact, such that the document then looks like

1. [Answer 1]
2. (a) [Answer 2a]
   (b) [Answer 2b]

Concretely, I would like to do this by redefining the answersonly environment such that its content is typeset as if the strings Question 1, Question 2a and Question 2b\par more details had been deleted, and then modifying the answer environment to neutralize that effect for its content.

In other words, I would like to define an answersonly environment that suppresses the output of any text in its body, except for

  • the item labels of the enumerate environment
  • the contents of the answer environment

Important: I can't modify the content of answersonly, i.e. I can't put any conditional-compilation environments like \begin{question}Question 2a\end{question} around the text to be suppressed.

Is there some “swallow text” mode that one can change LaTeX into such that it does not output any text that it encounters, and then modify the enumerate and answer environments, such that the item labels and vertical inter-item skips of enumerate and the content of answers still get through?

Any ideas on how one could do this?

2 Answers 2

1

I would edit the source to be more like

 \item 
    \begin{question}Question 2b\par more details\end{question}
    \begin{answer}Answer 2b\end{answer}

Then the structure is much clearer and you can simply define question to do nothing (so the content typesets) or to discard its body, but as you have it...

enter image description here




\documentclass{article}
\newenvironment{answer}{\par[}{]}
\newenvironment{answersonly}{
\let\xitem\item\long\def\item##1\begin{\xitem\begin}}{}
\begin{document}
\begin{answersonly}
\begin{enumerate}
\item Question 1
  \begin{answer}Answer 1\end{answer}
\item
  \begin{enumerate}
  \item Question 2a
    \begin{answer}Answer 2a\end{answer}
  \item Question 2b\par more details
    \begin{answer}Answer 2b\end{answer}
  \end{enumerate}
\end{enumerate}
\end{answersonly}
\end{document}
2
  • Could I realistically perform such a text transform in LaTeX, inside the answersonly environment, i.e. without editing the source file? I appreciate that this would probably be close to writing a LaTeX parser, but I've seen all sorts of clever things being done. (This exam-class format has been in use for over a decade in my department, and our archive has thousands of such questions, and the use case of having optionally an answers-only output format produced is not strong enough to ask all authors to clutter their questions with an additional \begin{question}...\end{question} environment.) Dec 12, 2023 at 21:14
  • isn't that exactly what this shows? you don't need to make any changes in the body of the document. But I wouldn't call it an extra equation environment "clutter": as an author I'd appreciate the extra structure. But the answer above only says that's what i do the actual code does not use the extra environment. Dec 12, 2023 at 21:22
0

I came across an article a while ago where someone did a decent job of articulating something similar, using the 'exam' document class and the command '\printanswers'. I'll just link to the article, they did a much better job of explaining it (section 3 should be what you want in particular).

Typesetting multiple choice questions in Latex

You must log in to answer this question.

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