1

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

ANSWERS PAGE

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

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{\answerlist}{}% reserve macro name

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

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

\newpage
\answerlist

\end{document}
  • Cool. I will take a closer look at this tonight. Thanks! – WeCanLearnAnything Sep 28 '16 at 22:09
  • 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
  • \xdef is equivalent to \global\edef (expand and define). We use \edef since \answerlist and \Answer are defined differently each time, so we need to expand them immediately. We need \global to get the result out of the loop. – John Kormylo Sep 29 '16 at 13:00
  • 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
  • \pgfmathtruncatemacro will convert real to integer without having to use int(). – John Kormylo Oct 3 '16 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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