1

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.

enter image description here

1 Answers1

1

If your Answers section will always follow your Questions section, then the following is a sufficient start. The principle behind the solution is to mark each \NewQuestion{<tag>} with a <tag>. This stores the answer associated with <tag> in a macro, which you can recall later.

enter image description here

\documentclass{article}

\usepackage{pgf}

\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(0,2)}%
}

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

\newcommand{\Answer}[1]{$\csname Answer_#1\endcsname$}
\newcommand{\NewQuestion}[1]
{%
  \InitVariables
  \expandafter\xdef\csname Answer_#1\endcsname{\PartA + \PartB = \Sum}%
  \ifcase\Structure\relax %
      \let\PartA\Blank
    \or
      \let\PartB\Blank
    \or
      \let\Sum\Blank
  \fi
  $\PartA + \PartB = \Sum$
}

\begin{document}

\section{Exercises}

\begin{tabular}{ @{}lll }
  \NewQuestion{first} & \NewQuestion{2} & \NewQuestion{last}
\end{tabular}

\section{Answers to the Above Exercises}
\begin{tabular}{ @{}lll }
  \Answer{first} & \Answer{2} & \Answer{last}
\end{tabular}

\end{document}

I removed some of the redundant code as there was a number of duplication that is not needed. There were also some spurious spaces removed.

If you want to be able to refer to the Answers before the Questions, one can use the \label-\ref system to retrieve this content.

Werner
  • 603,163
  • Ok, cool. I think I will be able to make sense of this when I take a closer look later tonight or tomorrow. Before then, though, would I stick with your method if I wanted to make, say, 100 questions? – WeCanLearnAnything Oct 06 '16 at 01:32
  • @WeCanLearnAnything: It would be possible to set up the \Answer macro 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 \NewQuestion and \Answer). – Werner Oct 06 '16 at 01:35
  • And is there a reasonably easy way to not have to make two tables? – WeCanLearnAnything Oct 06 '16 at 01:40
  • @WeCanLearnAnything: Sure, but you'll have to be specific regarding the input. With \NewQuestion you control how many questions by setting them each separately. A macro like \SetAnswers could be used to set all answers to all questions without having to specify the structure... – Werner Oct 06 '16 at 01:50
  • Took a closer look and now I have a bunch more questions! (1) What does \DeclareRobustCommand mean? (2) Why is there an underscore in Answer_#1? (3) What does \expandafter do? (4) Why do the column settings in the tables have @{}? – WeCanLearnAnything Oct 06 '16 at 17:55