I'm trying to make simple integer exercise pages and solution pages.
Why does this code not compile?
I'm pretty sure the problem is in the \xdef line, but I don't know what the problem is.
Any help would be greatly appreciated. Thanks!
\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}

\xdefcan never be used on general latex text – David Carlisle Oct 20 '17 at 08:53\expandafter\g@addto@macro\expandafter\ManySolutions\expandafter{\EquationSolution}– David Carlisle Oct 20 '17 at 08:58\xdefworking in a very similar situation. For example, see John Kormylo's answer here. https://tex.stackexchange.com/questions/372493/exercise-solution-page – WeCanLearnAnything Oct 20 '17 at 09:06\xdefexcept with content that you have constructed directly, It is more or less guaranteed to fail if there is any non ascii letter or almost any command in the content, non expandable commands and very simple macros may be safe but the general rule is that if you use edef or xdef things will break – David Carlisle Oct 20 '17 at 09:19