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! – 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

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? – WeCanLearnAnything Jun 4 '17 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... – WeCanLearnAnything Jun 4 '17 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? – WeCanLearnAnything Jun 12 '17 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 Jun 13 '17 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.