# Separate Pages for Questions & Solutions

I am making basic arithmetic worksheets with random numbers.

I want to separate the questions from the answer page. Not sure how to do that.

Ideally, it would look something like this, but with more equations on each page.

QUESTIONS PAGE

2+3=__

7=__+4

1+__=9

2+3=5

7=3+4

1+8=9

My code puts all the questions and answers together, which is what I'd expect given the code I typed, because I don't know how to separate them. Do I need to restructure the whole thing?

\documentclass{article}

\usepackage{ifthen}
\usepackage{pgf}
\usepackage{pgffor}

\pgfmathsetseed{\number\pdfrandomseed}

\newcommand{\InitVariables}
{
\pgfmathsetmacro{\PartA}{int(random(0,10))}
\pgfmathsetmacro{\PartB}{int(random(0,10))}
\pgfmathsetmacro{\Sum}{int(\PartA+\PartB)}
\pgfmathtruncatemacro{\Structure}{random(1,3)}
}

\newcommand{\onefact}
{
\InitVariables
\ifcase\Structure\relax %
\or
\newcommand{\Question}{$$\PartA+\PartB=\_\_\_$$}
\newcommand{\Answer}{$$\PartA+\PartB=\Sum$$}
\or
\newcommand{\Question}{$$\PartA+\_\_\_=\Sum$$}
\newcommand{\Answer}{$$\PartA+\PartB=\Sum$$}
\or
\newcommand{\Question}{$$\_\_\_+\PartB=\Sum$$}
\newcommand{\Answer}{$$\PartA+\PartB=\Sum$$}
\fi
}

\newcommand{\thismany}[1]
{\foreach \x in {1,2,...,#1} { \onefact \par \Question \par \Answer \par}}

\begin{document}

\thismany{10}

\end{document}


There are several ways to do this, but using \xdef to create a list is probably the easiest. Note, \newcommand checks to see if the command has been previously defined, so if you don't know or care, use \def.

I also made some gratuitous formatting changes.

\documentclass{article}

\usepackage{ifthen}
\usepackage{pgf}
\usepackage{pgffor}

\pgfmathsetseed{\number\pdfrandomseed}

\newcommand{\InitVariables}
{
\pgfmathsetmacro{\PartA}{int(random(0,10))}
\pgfmathsetmacro{\PartB}{int(random(0,10))}
\pgfmathsetmacro{\Sum}{int(\PartA+\PartB)}
\pgfmathtruncatemacro{\Structure}{random(1,3)}
}

\newcommand{\Blank}{\,\rule[-3pt]{15pt}{.5pt}\,}

\newcommand{\onefact}
{
\InitVariables
\ifcase\Structure\relax %
\or
\def\Question{$\PartA+\PartB=\Blank$}
\def\Answer{$\PartA+\PartB=\Sum$}
\or
\def\Question{$\PartA+\Blank=\Sum$}
\def\Answer{$\PartA+\PartB=\Sum$}
\or
\def\Question{$\Blank+\PartB=\Sum$}
\def\Answer{$\PartA+\PartB=\Sum$}
\fi
}

\newcommand{\thismany}[1]
{\foreach \x in {1,2,...,#1} { \onefact \Question \par

\begin{document}
\parskip=\baselineskip
\thismany{10}

\newpage

• I think that I understand the majority of the code. I did not understand this:\xdef\answerlist{\answerlist \Answer \par}. I googled \xdef but couldn't make sense of the results. – WeCanLearnAnything Sep 29 '16 at 7:30
• Another question: I am using \pgfmathtruncatemacro because I was told to but I do not know why. Why not just use \pgfmathsetmacro? – WeCanLearnAnything Oct 2 '16 at 20:33