# Exercise & Solution Page

In an earlier post of mine, I asked how to create separate exercise and solution pages for basic arithmetic. I'm trying the same thing now for factoring simple trinomials, but it's not working and I'm not sure why. I feel like the key parts of the code - defining the exercise page and the solution page - are basically the same, but they obviously are not since my code does not compile.

I do know that if I comment out the line in the body \FactoredPage, it compiles, though I still get minor error messages.

What's happening?

\documentclass{article}

\usepackage{pgf}
\usepackage{pgffor}

\setlength{\parindent}{0pt}

\pagestyle{empty}

\pgfmathsetseed{\number\pdfrandomseed}

\newcommand{\InitVariablesSTA} %all positives
{%
\pgfmathsetmacro{\avar}{int(1)}
\pgfmathsetmacro{\bvar}{int(random(1,11))}
\pgfmathsetmacro{\cvar}{int(1)}
\pgfmathsetmacro{\dvar}{int(random(1,11))}

\pgfmathsetmacro{\Avar}{int(\avar*\cvar)}
\pgfmathsetmacro{\Bvar}{int(\avar*\dvar+\bvar*\cvar)}
\pgfmathsetmacro{\Cvar}{int(\bvar*\dvar)}
}

\newcommand{\STA}
{%
\InitVariablesSTA%
\newcommand{\factored}{$$(x+\bvar)(x+\dvar)$$}%
\newcommand{\expanded}{$$x^2+{\Bvar}x+\Cvar$$}%
}

\newcommand{\FactoredPage}{}

\newcommand{\ExpandedPage}[1]
{%
\foreach \x in {1,...,#1}
{%
\STA \expanded \par
\xdef\FactoredPage{\FactoredPage \factored \par}%
}
}

\begin{document}

\ExpandedPage{10}

\FactoredPage

\end{document}

• I am still completely stumped on this. I've compared and tinkered with the two codes and cannot find any meaningful difference between the two that would cause this code to fail. Could there be a bug in pgf of pgffor that I haven't heard of? Anyone else have ideas? I'd really appreciate any help! Thanks! – WeCanLearnAnything Jun 3 '17 at 23:34
• Just found a hint of the problem. In line with \xdef, if I replace \factored with plain text, the code compiles. So, I'm guessing that the definition of \factored is not defined in the \xdef line but I'm not sure why. – WeCanLearnAnything Jun 3 '17 at 23:53
