我正在尝试制作简单的整数练习页面和解决方案页面。
为什么这段代码不能编译?
我很确定问题出在线路上\xdef
,但我不知道问题是什么。
任何帮助都将不胜感激。谢谢!
\documentclass{article}
\usepackage{ifthen}
\usepackage{pgf}
\pgfmathsetseed{\number\pdfrandomseed}
\usepackage{pgffor}
\newcommand{\EquationSolution}{}
\newcommand{\EquationExercise}{}
\newcommand{\InitVariables}
{%
\pgfmathrandominteger{\A}{-10}{10} % the first number
\pgfmathrandominteger{\C}{-10}{10} % the answer
\ifthenelse{\equal{\A}{\C}}{\InitVariables}{}
\pgfmathsetmacro{\B}{int(\C-\A)} % The second number
\renewcommand{\EquationExercise}
{%
\ifthenelse{\B<0}
{\Large $\A\B=\_\_\_$}
{\Large $\A+\B=\_\_\_$}
}
\renewcommand{\EquationSolution}
{%
\ifthenelse{\B<0}
{\Large $\A\B=\C$}
{\Large $\A+\B=\C$}
}
}
\newcommand{\ManySolutions}{}
\newcommand{\ManyExercises}[1]
{%
\foreach \x in {1,...,#1}
{%
\InitVariables \EquationExercise
\xdef\ManySolutions{\ManySolutions \EquationSolution} % Compiles if this line is commented out [along with the other line below]
}
}
\setlength\parindent{0pt}
\pagestyle{empty}
\begin{document}
\section*{Exercises}
\ManyExercises{5}
\section*{Solutions}
\ManySolutions % also must be commented out to compile
\end{document}
答案1
首先破裂的\xdef
是\ifthenelse
,然后也是\Large
和\_
。
因为\Large
它很容易:它不属于那里,而是属于围绕调用\ManyExercises
和的命令的环境\ManySolutions
。
对于其他部分:
\documentclass{article}
\usepackage{pgfmath}
\usepackage{pgffor}
\pgfmathsetseed{\number\pdfrandomseed}
\newcommand{\EquationSolution}{}
\newcommand{\EquationExercise}{}
\newcommand{\ManySolutions}{}
\protected\def\dummy{\makebox[2em]{\hrulefill}}
\newcommand{\InitVariables}
{%
\pgfmathrandominteger{\A}{-10}{10} % the first number
\pgfmathrandominteger{\C}{-10}{10} % the answer
\ifnum\A=\C\relax\InitVariables\fi
\pgfmathsetmacro{\B}{int(\C-\A)} % The second number
\edef\EquationExercise
{%
$\A\ifnum\B<0 \else +\fi\B=\dummy$\par
}
\edef\EquationSolution
{%
$\A\ifnum\B<0 \else +\fi\B=\C$
}
}
\newcommand{\ManyExercises}[1]
{%
\foreach \x in {1,...,#1}
{%
\InitVariables \EquationExercise
\toks0=\expandafter{\ManySolutions\par}
\xdef\ManySolutions{\the\toks0 \unexpanded\expandafter{\EquationSolution}} % Compiles if this line is commented out [along with the other line below]
}
}
\setlength\parindent{0pt}
\pagestyle{empty}
\begin{document}
\section*{Exercises}
{\Large\ManyExercises{5}\par}
\section*{Solutions}
{\Large\ManySolutions\par}
\end{document}