2

I'm developing worksheets for students to practice determining equations of lines.

Everything is working except for the last command \ManySolutions. In fact, the code compiles without it.

Does anyone know what's wrong? Is there some incompatibility with $$ vs \(\) or is \foreach incompatible with some other command I put in there?

\documentclass{article}

\usepackage{ifthen}
\usepackage{pgf}
 \pgfmathsetseed{\number\pdfrandomseed}
\usepackage{pgffor}
\usepackage{tikz}

\newcommand{\MyScale}{0.4}

\newcommand{\VertLine}
{%
%
 \pgfmathrandominteger{\xvar}{-9}{9}
%
 State the slope, $y_{int}$, and equation in slope-intercept form.

 \newcommand{\Exercise}
  {%
   \begin{tikzpicture}[scale=\MyScale]
   \draw[help lines, gray, thin] (-10,-10) grid  (10,10); 
   \draw[very thick,<->] (-10.3,0)--(10.3,0);
   \draw[very thick,<->] (0,-10.3)--(0,10.3);
   \draw[thick, blue] (\xvar,-10)--(\xvar,10);
   \end{tikzpicture}%
  }
%
 \newcommand{\Solution}
  {%
   Slope is undefined.

   \ifnum\xvar=0
   {$y_{int}$ is all real numbers.}
   \else
   {There is no $y_{int}$.}
   \fi

   Equation: $x=\xvar$%
   \vspace{1cm}
 }
}


\newcommand{\ManySolutions}{}

\newcommand{\ManyExercises}[1]
{%
  \foreach \x in {1,...,#1}
  {%
   \VertLine \Exercise \par \vspace{0.5cm}
   \xdef\ManySolutions{\ManySolutions \Solution \par} 
  }
}


\begin{document}

\ManyExercises{15}

\ManySolutions


%It does compile if I replace the two lines above with:
 % \VertLine
 % \Exercise
 % \Solution

% So I know the graph and solutions can be generated.

\end{document}
cgnieder
  • 66,645
  • 1
    I think your code is too complicated for me to give you an answer, but if you put a \newcommand within a \foreach, at the second step of loop it gives you an error because the command already exists. – CarLaTeX Aug 29 '17 at 03:35
  • I think your questions (At least with this title does not help anyone else and I suggest to change the title to be near the problem found) – koleygr Aug 29 '17 at 04:44
  • I edited my answer and gives the wanted results... You can compare with yours and find the problems on your code. – koleygr Aug 29 '17 at 06:05

1 Answers1

1

Here is my working results for now:

\documentclass{article}

\usepackage{amssymb}
\usepackage{pgf}
 \pgfmathsetseed{\number\pdfrandomseed}
\usepackage{pgffor}
\usepackage{tikz}



\newcommand{\MyScale}{0.4}

\newcommand{\Exercise}{}
\newcommand{\Solution}{}
\newcommand{\VertLine}
{%
%
 \pgfmathrandominteger{\xvar}{-9}{9}
%
 State the slope, $y_{int}$, and equation in slope-intercept form.

 \renewcommand{\Exercise}
  {%
   \begin{tikzpicture}[scale=\MyScale]
   \draw[help lines, gray, thin] (-10,-10) grid  (10,10); 
   \draw[very thick,<->] (-10.3,0)--(10.3,0);
   \draw[very thick,<->] (0,-10.3)--(0,10.3);
   \draw[thick, blue] (\xvar,-10)--(\xvar,10);
   \end{tikzpicture}%
  }
%
 \renewcommand{\Solution}
  {%
   Slope is undefined.
   \ifnum\xvar=0 $y_{int}$ is all real numbers \else There is no $y_{int}$.\fi

   Equation: $x=\xvar$
   \vskip 1cm

 }
}


\newcommand{\ManySolutions}{}



\newcommand{\ManyExercises}[1]
{%
  \foreach \x in {1,...,#1}
  {%
   \VertLine \Exercise \par \vspace{0.5cm}
   \xdef\ManySolutions{\ManySolutions \Solution\par} 
  }
}

%\renewcommand{\ManyExercises}[1]
%{%
%  \newcount\X
%  \X=1
%  \loop
%  \advance \X by 1
%   \VertLine \Exercise \par \vspace{5cm}
%   \edef\oldManySolutions{\ManySolutions}
%   \xdef\ManySolutions{\oldManySolutions \Solution\par }
%  \ifnum\X<#1
%  \relax
%  \repeat
%}





\begin{document}

%This does not compile...%%%Compiles now
\ManyExercises{10}


\ManySolutions
%It does compile if I replace the line above with:
%% \VertLine
%% \Exercise
%% \Solution

\end{document}

The commented way of \ManyExercises gives the same results as the above. This way I found out that the problem which remains is the \vspace{1cm} that I have commented out. Of course there is no spaces in the results.

Edit Replacing \vspace{1cm} with \vskip gives the wanted result

koleygr
  • 20,105
  • 1
    What do you think is the difference between \def\Reals{\origmathbb{R}} and \xdef\Reals{\noexpand\origmathbb{R}} (apart from the latter being global, which is irrelevant at that point)? – egreg Aug 29 '17 at 14:18
  • @egreg... I just made a million changes to find a way to make it work. It didn't and left on my code something that I don't use. I remember that I tried to keep it unexpanded inside tikz (where didn't work) and let it expand on the text afterwards. Also tried to give an \empty value too in some steps and give the old value back later. This just left there. Thanks for noticing. Editting to remove it. – koleygr Aug 29 '17 at 14:28
  • First, thank you! Also, when I changed your code from \vskip 1cm to my original code of \vspace{1cm}, it did not compile. Anyone know what is wrong with \vspace{1cm}? – WeCanLearnAnything Aug 29 '17 at 18:35
  • 1
    @WeCanLearnAnything Some thoughts and a solution: read @JohnWickerson's answer here: https://tex.stackexchange.com/questions/655/what-is-the-difference-between-def-and-newcommand ... we didn't use newcommand* but used \newcommand that means that \par and vertical skips are allowed in our command environment. But this environment used inside a foreach and thus afaik there needed additional methods for the dimensions since the fontsize there is null. But trying to remove the foreach environments and/or even replace all defs by \long\defs the problem doesn't solve. After checking... – koleygr Aug 29 '17 at 19:41
  • 1
    ... the errors and the long files I found out that \vspace gets somehow "undefined" or loosing the dimensions of cm pt etc. placing all the \Vertline command between \makeatletter and \makeatother and replacing \vspace{1cm} with \@vspace{1cm} solves the problem. I hope someone who will see these comments will give as the reason. (Still searching) – koleygr Aug 29 '17 at 19:46