How do I set this up so that when the random numbers generate the exercises, they also automatically generate the answers for the answer table?
Do I need to revamp the whole thing or can I make a more minor tweak?
\documentclass{article}
\usepackage{ifthen}
\usepackage{pgf}
\usepackage{pgffor}
\pgfmathsetseed{\number\pdfrandomseed}
\newcommand{\InitVariables}
{
\pgfmathsetmacro{\PartA}{int(random(0,9))}
\pgfmathsetmacro{\PartB}{int(random(0,9))}
\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
}
\begin{document}
\section{Exercises}
\begin{tabular}{lll}
\onefact \Question & \onefact \Question & \onefact \Question \\
\end{tabular}
\section{Answers to the Above Exercises}
\begin{tabular}{lll}
\onefact \Answer & \onefact \Answer & \onefact \Answer\\
\end{tabular}
\end{document}
That code yields the following. Of course, I don't want random numbers to be generated a second time for the answers, I just want the completed equations from the exercises to be the answers.
Ideally, I would not have to type out a separate answer table either.


\Answermacro to just print answers in a sequential way that would match the order of the\NewQuestion. That way you won't have to provide a<tag>. Or, one could make the<tag>optional (in both cases; for\NewQuestionand\Answer). – Werner Oct 06 '16 at 01:35\NewQuestionyou control how many questions by setting them each separately. A macro like\SetAnswerscould be used to set all answers to all questions without having to specify the structure... – Werner Oct 06 '16 at 01:50\DeclareRobustCommandmean? (2) Why is there an underscore inAnswer_#1? (3) What does\expandafterdo? (4) Why do the column settings in the tables have@{}? – WeCanLearnAnything Oct 06 '16 at 17:55