3

While using the exam class, is there a way of compiling all the questions and putting them together at the beginning of the document? I would want the end result to look something like the result of the following code. First all the questions and then each question with a solution box.

\documentclass[answers]{exam}

\begin{document}
\begin{questions}
 \question First Question
 \question Second Question
\end{questions}

\newpage

 \begin{questions}
  \question First question
  \begin{EnvFullwidth}
   \begin{solutionbox}{2in}
    This looks good,
   \end{solutionbox}

  \end{EnvFullwidth}

  \question Second question
  \begin{EnvFullwidth}
   \begin{solutionbox}{2in}
    Not so good.
   \end{solutionbox}
  \end{EnvFullwidth}

 \end{questions}

\end{document}

Is there a way to automate this, so I only have the questions typed in one place and on compilation, it will extract them and create the first part? Ideally, something that looks like:

\documentclass[answers]{exam}

\begin{document}
\printallquestions

\newpage

 \begin{questions}
  \question First question
  \begin{EnvFullwidth}
   \begin{solutionbox}{2in}
    This looks good,
   \end{solutionbox}

  \end{EnvFullwidth}

  \question Second question
  \begin{EnvFullwidth}
   \begin{solutionbox}{2in}
    Not so good.
   \end{solutionbox}
  \end{EnvFullwidth}

 \end{questions}

\end{document}
4
  • Probably, Answers at the end of the exam document asks the reverse (print all answers at end) and has a number of answers.
    – Dai Bowen
    Nov 2, 2016 at 20:05
  • The difference is I want the questions to appear twice. Once at the beginning with no space for answers, and then again with space for each answer.
    – fabikw
    Nov 2, 2016 at 20:11
  • I appreciate they're different questions, though I think the biggest complication is here you want the questions to be printed before they've been defined. How do you want \printallquestions to appear - with space for answers or not?
    – Dai Bowen
    Nov 2, 2016 at 21:37
  • No space for answers. Basically the exam should contain all the questions and then a copy of each part with the corresponding space for filling in.
    – fabikw
    Nov 2, 2016 at 21:39

3 Answers 3

2

The following seems to work, although I expect there may be a more robust solution.

Include all your questions in a command, then execute that command twice after \begin{document} (once with \noprintanswers and again after \printanswers).

I also added a custom environment that encloses every solution in an EnvFullwidth environment so you don't have to do that manually every time.

\documentclass{exam}
\newenvironment{mysolution}
{\begin{EnvFullwidth}
\begin{solution}
}
{
\end{solution}
\end{EnvFullwidth}
}

\newcommand{\myquestions}{%
    \begin{questions}
    \question First question

    \begin{mysolution}
    This looks good,
    \end{mysolution}

    \question Second question
    \begin{mysolution}
    Not so good.
    \end{mysolution}

    \end{questions}
}

\begin{document}
\noprintanswers
\myquestions

\newpage
\printanswers
\myquestions
\end{document}

enter image description here

1
  • Thanks. A little bit of modification on this made it work. I actually needed to not remove the answers but also the answer boxes (but be able to have both if if want the answers). I used your idea but in a block around the first myquestions I redefined the solution environment to gobble its contents.
    – fabikw
    Nov 2, 2016 at 21:37
2

Using erik's answer, I constructed this MWE of what I want, where the answers can be shown or hidden in the second part. However, I'd still like a cleaner solution like writing the question parts into a separate file.

\documentclass[answers]{exam}
\usepackage{environ}

\NewEnviron{mySol}[1][1in]{
\begin{EnvFullwidth}
 \begin{solutionbox}{#1}
  \BODY
 \end{solutionbox}
\end{EnvFullwidth}}


\begin{document}

\newcommand\myquestions{
 \begin{questions}
  \question First question
\begin{mySol}
    This looks good,
\end{mySol}
  \clearpage
  \question Second question
  \begin{mySol}
    Not so good.
  \end{mySol}

 \end{questions}}

 {\RenewEnviron{mySol}[1][\relax]{\relax}
  \renewcommand\newpage{\relax}
  \renewcommand\clearpage{\relax}
  \myquestions
}
\clearpage
\myquestions

\end{document}
2

I think there's definitely a case for setting some variable \anslength and using that in the argument of solutionbox and swapping that from 2in to 0in and then compiling with answers off to produce a copy with just the questions and no space for answers and then stitching together the PDFs with pdfpages. Similarly I think answers to Answers at the end of the exam document could be adaptedly easily enough to print a question list at the end of the question and the necessary reordering not a huge task by hand.

For the MWE given by the OP, the following does the job. Questions are written to the aux file (based on egreg's solution to Write LaTeX3 variable to aux file and recover it) and then retrieved, this will therefore need to be compiled twice. \PrintQuestions prints the questions in the questions environment, which does lead to duplication of labels, probably an enumerate environment is sufficient. I have no idea why \item[] is necessary but LaTeX was very unhappy if I didn't include a plain \item (\question also did the job) and as [] hides the label it doesn't cause much disturbance.

\documentclass[answers]{exam}
\usepackage{xparse}

\ExplSyntaxOn
\seq_new:N \l_exam_beginprint_seq
\seq_new:N \g_exam_beginprint_seq

\NewDocumentCommand \WriteQuestion { +m } {
    \seq_gput_right:Nx \l_exam_beginprint_seq { #1 }
    \question #1
}

\NewDocumentCommand \PrintQuestions { } {
    \begin{questions}
        \seq_map_inline:Nn \g_exam_beginprint_seq { \question ~ ##1 }
        \item[] 
    \end{questions}
}

\cs_new:Npn \QuestionFromAux #1 {
    \seq_gput_right:Nn \g_exam_beginprint_seq { #1 }
}

\AtEndDocument {
    \seq_map_inline:Nn \l_exam_beginprint_seq {
        \iow_now:cx { @auxout } {
            \token_to_str:N \QuestionFromAux { #1 }
            ^^J
        }
    }
}
\ExplSyntaxOff

\begin{document}
\PrintQuestions

\newpage

\begin{questions}
\WriteQuestion{First question}
\begin{EnvFullwidth}
\begin{solutionbox}{2in}
    This looks good,
\end{solutionbox}
\end{EnvFullwidth}

\WriteQuestion{Second question}
\begin{EnvFullwidth}
\begin{solutionbox}{2in}
    Not so good.
\end{solutionbox}
\end{EnvFullwidth}
\end{questions}

\end{document}

enter image description here

You must log in to answer this question.

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