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?






\newcommand{\InitVariablesSTA} %all positives




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




  • 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! Commented Jun 3, 2017 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. Commented Jun 3, 2017 at 23:53

2 Answers 2


The macros \( and \) don't survive \xdef. Use $:


enter image description here

Related: Build tabular content via \foreach

You might use \protected@xdef, so \( and \) might be used; it's simpler the other way.

  • 1
    Wow, is that really it? Thank you! Are there any other important differences between \(...\) and $...$ that I should know about with respect to macros? Commented Jun 4, 2017 at 15:47
  • Also, how could/should I have figured that out on my own? I don't remember anybody mentioning anything like this before, except maybe in tables... Commented Jun 4, 2017 at 15:48

Here is, what you need to do. But without any pgf (or similar) package. Only primitives and plain TeX macros are used:

\input random
\newcount\tmpnum  \newcount\loopnum
\randomi=1  % fixed random sequence (independent of time of document processing)

      \advance\loopnum by1
      \ifnum\loopnum<#1 \repeat 
   \setrannum\tmpnum{1}{10} \edef\bvar{\the\tmpnum}
   \setrannum\tmpnum{1}{10} \edef\dvar{\the\tmpnum}
   \tmpnum=\bvar \advance\tmpnum by\dvar  \edef\Bvar{\the\tmpnum}
   \tmpnum=\bvar \multiply\tmpnum by\dvar \edef\Cvar{\the\tmpnum}
   $x^2 + \Bvar x + \Cvar$\par


  • This looks really complicated... Can I ask if there is an advantage to using primitives and plain TeX over LaTeX? Commented Jun 12, 2017 at 23:49
  • @WeCanLearnAnything Because LaTeX is unnecessarily complicated. For example, if you are using plain TeX then you never use such obscure macros aka \(, you use only direct characters with catcode 3, i.e. $. So, this question and this problem never occurs.
    – wipet
    Commented Jun 13, 2017 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .