4

I'm slowly progressing towards understanding the code for creating random arithmetic questions for young students.

At this point, I'm trying to create ten random addition questions.

Why does this not compile? The error message says that \TenRandomQuestions is not defined, but I thought I did define it...

\documentclass{article}

\usepackage{pgf}

\pgfmathsetseed{\number\pdfrandomseed}

\newcommand*\initVariables
{
 \pgfmathsetmacro{\A}{random(0,10)}
 \pgfmathsetmacro{\B}{random(0,10)}
}

\newcommand*\OneEquation % This macro compiles.
{
\initVariables
{\newcommand{\answer}{\pgfmathprint{int(\A+\B)}}
$\A+\B=\answer$}
}

\newcommand{\TenRandomQuestions} % This macro does NOT compile. The error message says that this command is undefined.
{%
\foreach \x in {0,1,2,...,10}%
 {%
 \OneEquation \\   % How do I incorporate \x into this? Or do I not need to?
 }%
}%


\begin{document}

\TenRandomQuestions

\end{document}
3
  • 2
    \usepackage{pgffor} is missing.
    – user31729
    Aug 6, 2016 at 21:02
  • Wow. Ok, is there a list/explanation of packages related to TikZ and PGF that I should know about? Aug 6, 2016 at 21:47
  • I am sure there are questions about this here on TeX.SX already. In doubt, look into pgfmanual.pdf (the tikz manual)
    – user31729
    Aug 6, 2016 at 21:49

1 Answer 1

2
\documentclass{article}

\usepackage{pgffor}

\pgfmathsetseed{\number\pdfrandomseed}

\newcommand*\initVariables
{
 \pgfmathsetmacro{\A}{random(0,10)}
 \pgfmathsetmacro{\B}{random(0,10)}
}

\newcommand*\OneEquation % This macro compiles.
{
\initVariables
{\newcommand{\answer}{\pgfmathprint{int(\A+\B)}}
$\A+\B=\answer$}
}

\newcommand{\TenRandomQuestions} % This macro does NOT compile. The error message says that this command is undefined.
{%
    \foreach \x in {0,1,2,...,10}%
    {%
        \OneEquation \\   % How do I incorporate \x into this? Or do I not need to?
    }%
}%


\begin{document}

    \TenRandomQuestions

\end{document}
7
  • 3
    The pgfforce is strong in you ;-) (+1)
    – user31729
    Aug 6, 2016 at 21:21
  • haha. You should be getting the credit since you got here first, but it's answer the unanswered questions time.... ;-)
    – JPi
    Aug 6, 2016 at 21:22
  • I don't need the rep (any longer, my 'hunt' is over) ... and you've misunderstood 'answer the unanswered', that's more for older questions :D
    – user31729
    Aug 6, 2016 at 21:24
  • Indeed, I did not know that.
    – JPi
    Aug 6, 2016 at 21:29
  • 1
    More than a few days, I would say. Revival badges for first answers after 30 days with 2 votes (so even if there's an older answer, you could get the badge, if your answer gets two votes first), and Necromancer badges for any answer given after 60 days, having 5 votes then (whenever the 5 votes are completed)
    – user31729
    Aug 6, 2016 at 21:34

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.