8

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 03 '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 03 '17 at 23:53

2 Answers2

7

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

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

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.

egreg
  • 1,121,712
  • 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 04 '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 04 '17 at 15:48
1

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)
\parindent=0pt

\def\ExpandedPage#1{%
   \def\FactoredPage{}\loopnum=0
   \loop
      \advance\loopnum by1
      \preparepages
      \ifnum\loopnum<#1 \repeat 
}
\def\preparepages{\par
   \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
   \edef\FactoredPage{\FactoredPage$(x+\bvar)(x+\dvar)$\par}
}

\ExpandedPage{10}
\bigskip
\FactoredPage

\bye
wipet
  • 74,238
  • 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