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, GuidoJul 14 '14 at 17:46

• 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
• 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

Try this.

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

\input{question-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}

• 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

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:

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

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

% does the current question have an answer?

% file containing answer key is written

\newcommand{\testname}[1]{%
\begin{center}
\textbf{#1}
\end{center}
\noexpand\begin{center}
\noexpand\textbf{#1}
\noexpand\par
\noexpand\end{center}
}}

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

\theexplanation{}}
}
}
}

\newcommand{\correct}[1]{%

• 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