2

This question already has an answer here:

So this is going to sound strange, but I'm wondering if there is a clean way to do this.

By the time I have a project done, I will have written two .tex files: one with questions, and another one with solutions. For purposes of peer review, I'd like to know if there's a way for me to use \include so that if each question and solution to each respective question spans only one page, the question and solution will be paired together so that the question is first and the solution is second.

Is there a way (or a better way) to do this beside manually copying/pasting code?

MWE:

Questions:

\documentclass[12pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{enumitem}

\begin{document}
\begin{center}
\textbf{Mock Exam}
\end{center}
\begin{enumerate} 
\item Here's a question.
\begin{enumerate}[label=(\Alph*)]
\item Option A
\item Option B
\item Option C
\item Option D
\item Option E
\end{enumerate}
\newpage  
\item Here's another question.
\begin{enumerate}[label=(\Alph*)]
\item Option A
\item Option B
\item Option C
\item Option D
\item Option E
\end{enumerate}
\end{enumerate}
\end{document}

Solutions:

\documentclass[12pt]{article}
\usepackage[margin=2cm]{geometry}
\begin{document}
\begin{center}
\textbf{SOLUTION}
\end{center}
\begin{enumerate} 
\item (A)

\vspace{0.2cm}
-Some stuff telling you why the answer is (A)-
\newpage 
\item (C)

\vspace{0.2cm}
-Some stuff telling you why the answer is (C)-
\end{enumerate}
\end{document}

This is the general way I have been creating MC exams and solutions in the past for the company, and it's still very early in the project, so I can change the code so that it works if necessary. It's just that I found it very time-consuming to copy-and-paste the solutions into a new file for peer review.

marked as duplicate by Ethan Bolker, Masroor, Jesse, Benedikt Bauer, Guido Jul 14 '14 at 17:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    This depends on the nature of your .tex code, how the questions and solutions are defined. Are they coming one after another one, i.e. question 1, question 2... and solution 1, solution 2... etc.? Are they contained in environments. Please post a MWE what you already have. – user31729 Jul 14 '14 at 14:50
  • I'm not sure if I can post it up due to copyright reasons. The only environment I'm using is the enumerate environment to group the questions and solutions by number. Everything else is just text. – Clarinetist Jul 14 '14 at 14:51
  • Well, you can make a file with some dummy questions and one with corresponding (dummy ;-) solutions? But am unsure about that enumerate approach. – user31729 Jul 14 '14 at 14:53
  • Sure, let me get something written. – Clarinetist Jul 14 '14 at 14:54
  • 1
    I strongly recommend that you use something like the answers package so that you can keep the questions and the answers in the same source file, but compile them to either one or two pdfs as you wish - see tex.stackexchange.com/questions/186530/…. For a simple partial solution, see tex.stackexchange.com/questions/97904/…. – Ethan Bolker Jul 14 '14 at 15:12
2

Try this.

\documentclass{article}
\usepackage{enumitem}
\usepackage{amsmath}
\usepackage{filecontents}
\begin{document}
\begin{filecontents}{question-01}
What is an aardvark?\par
\end{filecontents}
\begin{filecontents}{answer-01}
\pagebreak
The aardvark Orycteropus afer is a medium-sized, burrowing, nocturnal mammal 
\end{filecontents}

% questions and answers here
\input{question-01}
\input{answer-01}
\end{document}

I suggest first to define your requirements clearly, experiment with a main file first and then extend with macros and a naming scheme for the files. I would keep all the answers and questions of one exam in one main file as above. However I would recommend the individual questions and answers are kept in separate files. This way you can mix and match as you go. Keep everything in one directory and name the directory like maths-exam-101-summer-14.

Note you can also relate the files to \jobname as follows:

\begin{filecontents}{\jobname-01-answer}
\pagebreak
The aardvark Orycteropus afer is a medium-sized, burrowing, nocturnal mammal 
\end{filecontents}

\input{question-01}
\input{\jobname-01-answer}
  • Thank you, this was very helpful! I was also able to figure out how to get enumerate to work with this - for people who are interested, I used \begin{enumerate}[resume] \item before each question and \end{enumerate} after each question. – Clarinetist Jul 14 '14 at 19:06
0

This is a substantial reworking of my original solution - there were fun problems to solve, so I'm posting it even though there's an accepted answer.

The MWE below produces these pdfs as output:


enter image description here


enter image description here


enter image description here

Here are the source files.

First, the test, which you must compile twice to get the references right.

\documentclass{article}

\input{testpreamble}

%\toggletrue{showanswers} % this is the default
\togglefalse{showanswers}

\begin{document}

\testname{Mock Exam}

\begin{enumerate}
\item Compute $2 + 2$.

\begin{choices}
\item 1
\item 2
\item 3
\item 4 \correct{Count on your fingers.}
\item 5
\end{choices}

\item Compute $e^{i\pi}$.

\begin{choices}
\item $e$
\item -1
\correct{
There are several ways to go about proving that

\begin{equation*}
e^{i\pi} = -1
\end{equation*}

but for this exam just \emph{knowing} the answer gets full credit.
}

\item $\pi$
\item whatever
\end{choices}

\item What is the name of this test?
\begin{choices}
\item There is no way to tell
\end{choices}

\end{enumerate}
\end{document}

This document answerkeymaster.tex, produces the answer key:

\documentclass{article}

\begin{document}
\input{answerkey}
\end{document}

The mechanism is all hidden in the testpreamble.tex, which is the same for all tests:

% testpreamble
%
\usepackage{enumitem}
\usepackage{amsmath}
\usepackage{etoolbox}

% decide whether answers appear in the test
\newtoggle{showanswers}
\toggletrue{showanswers}

% does the current question have an answer?
\newtoggle{hasanswer}
\togglefalse{hasanswer}

% file containing answer key is written
\newcommand{\answerkeyfilename}{answerkey}
\newwrite\answerkey
\immediate\openout\answerkey=\answerkeyfilename

\newcommand{\testname}[1]{%
\begin{center}
\textbf{#1}
\end{center}
\immediate\write\answerkey{%
\noexpand\begin{center}
\noexpand\textbf{#1}
\noexpand\par
Answer Key
\noexpand\end{center}
}}

% explanatory words that accompany each answer
\newcommand{\theexplanation}{}

\newcommand{\answer}{%
 \iftoggle{hasanswer}{%
   \immediate\write\answerkey{\noexpand\par\theenumi -- \ref{answer:\theenumi}}
   \iftoggle{showanswers}{%
      \par\emph{Answer}: 
      \ref{answer:\theenumi}
      \theexplanation{}}
   {} % don't show answers
  }
  {\immediate\write\answerkey{\noexpand\par\theenumi -- no answer}
   \iftoggle{showanswers}{%
      \par\emph{Answer}: none }
    {} % don't show nonanswer
  }
}

\newcommand{\correct}[1]{%
 \toggletrue{hasanswer}
 \label{answer:\theenumi}
 \global\renewcommand{\theexplanation}{#1}
}

\newenvironment{choices}
{\begin{enumerate}[label=(\Alph*)]}
{\answer{}\end{enumerate}}

You can probably improve your workflow by implementing some of @YiannisLazarides naming conventions for your tests.

  • Actually, as good as this solution is, it doesn't work for my purposes. Next to the numbers (1) and (2) are the questions and next to (A), (B), (C), etc. are the Multiple Choice options. The answers are then placed on a new page, but what you said can easily be extended to that. However, the problem remains if I want to make a question-only document, which contains everything except the solutions (i.e., just the question and the MC options). – Clarinetist Jul 14 '14 at 16:19
  • I have tried creating a new environment which hides the questions, but they seem to hate being around the enumerate items. – Clarinetist Jul 14 '14 at 16:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.