1

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}
  • \xdef can never be used on general latex text – David Carlisle Oct 20 '17 at 08:53
  • What should I be using instead, then? Or, rather, how else should I compile a list of solutions? – WeCanLearnAnything Oct 20 '17 at 08:53
  • 1
    probably \expandafter\g@addto@macro\expandafter\ManySolutions\expandafter{\EquationSolution} – David Carlisle Oct 20 '17 at 08:58
  • That code is hard for me to make sense of, but I will try anyway. I was able in the past to get \xdef working 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
  • you should never use \xdef except 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

1 Answers1

1

The first thing that breaks within \xdef is \ifthenelse, then also \Large and \_.

For \Large it's easy: it doesn't belong there, but to an environment around the commands for calling \ManyExercises and \ManySolutions.

For the other pieces:

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

enter image description here

egreg
  • 1,121,712
  • Thanks! I didn't know all of those things prevented \xdef from working. Now onto the next issue... :) – WeCanLearnAnything Oct 20 '17 at 10:29
  • Which \if... commands work properly in \xdef and which ones don't? is there a list for such things? – WeCanLearnAnything Oct 28 '17 at 01:59
  • @WeCanLearnAnything That’s not an easy question; but what doesn’t work by pure macro expansion will not survive \xdef – egreg Oct 28 '17 at 08:07
  • What does "pure macro expansion" mean? – WeCanLearnAnything Oct 30 '17 at 05:37
  • 1
    @WeCanLearnAnything I have a wonderful explanation for the term, but unfortunately there is a 30K limit on characters in answers. Basically, whatever just need macro expansion and no assignment of values to macros or registers or boxes or… – egreg Oct 30 '17 at 07:21
  • @WeCanLearnAnything https://tex.stackexchange.com/questions/66118/advantages-and-disadvantages-of-fully-expandable-macros/66168#66168 – David Carlisle Nov 01 '17 at 13:39