带有随机数的宏不起作用

带有随机数的宏不起作用

我正在尝试为孩子们创建数学事实工作表来练习,比如 2 + 3=5,但在这个等式的每种形式中:

2+3=_ _ _

2+_ _ _=5

_ _ _+3=5

_ _ _=2+3

5=_ _ _+3

5=2+_ _ _

我想要随机数来选择加数以及结构。

为什么此代码不起作用?\RandomType可能是错误所在,但我没有看到问题所在......

\documentclass{article}

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

\setlength{\parindent}{0pt}

\pgfmathsetseed{\number\pdfrandomseed}

\newcommand{\InitVariables}
{
 \pgfmathsetmacro{\a}{int(random(0,10))}
 \pgfmathsetmacro{\b}{int(random(0,10))}
 \pgfmathsetmacro{\c}{int(\a+\b)}
  \pgfmathsetmacro{\r}{int(random(1,6))} % r will select one of the six types below.
}

\newcommand{\blank}{\_\_\_\_\_}

\newcommand{\TypeOne}
{
\large
\InitVariables
\huge\(\a+\b=\)\blank \hspace{3cm} \(\a+\b=\c\)

\vspace{0.8cm}
}
       % Types two through six you can probably skip reading. Start reading code again at the next comment.
\newcommand{\TypeTwo}
{
\large
\InitVariables
\huge\(\a+\blank=\c\) \hspace{3cm} \(\a+\b=\c\)

\vspace{0.8cm}
}

\newcommand{\TypeThree}
{
\large
\InitVariables
\huge\(\blank+\b=\c\) \hspace{3cm} \(\a+\b=\c\)

\vspace{0.8cm}
}

\newcommand{\TypeFour}
{
\large
\InitVariables
\huge\(\blank=\a+\b\) \hspace{3cm} \(\c=\a+\b\)

\vspace{0.8cm}
}

\newcommand{\TypeFive}
{
\large
\InitVariables
\huge\(\c=\blank+\b\) \hspace{3cm} \(\c=\a+\b\)

\vspace{0.8cm}
}

\newcommand{\TypeSix}
{
\large
\InitVariables
\huge\(\c=\a+\blank\) \hspace{3cm} \(\c=\a+\b\)

\vspace{0.8cm}
}

% start reading here again. :)

\newcommand{\RandomType}
{
\ifcase\r\relax%
 \or \TypeOne
 \or \TypeTwo
 \or \TypeThree
 \or \TypeFour
 \or \TypeFive
 \or \TypeSix
\fi
}

\begin{document}

\RandomType

% if I only write "\TypeSix" and \TypeFour" in the body, it compiles.
% but it does not compile as given

\end{document}

答案1

\RandomType\r在 中初始化的测试\InitVariables。由于\TypeOne,...,\TypeSix不使用\r,因此必须\pgfmathsetmacro{\r}{int(random(1,6))}移至\RandomType

相关内容