2

In my previous post, I asked how to save random numbers for the xsim package.

Now I would like to know how to do that when those random numbers are inserted into a TikZ picture.

What about my counter is failing? That is, why is it only printing the solutions from the final exercise?

\documentclass{article}

\usepackage{pgf}

\usepackage{pgffor}
    \pgfmathsetseed{\number\pdfrandomseed}

\usepackage{tikz}

\usepackage{xsim}



\newcounter{ExNum}

\newcommand{\InitVariables}{%
    \stepcounter{ExNum}
    \pgfmathrandominteger{\IntegerPoint}{3}{10}
        \expandafter\edef\csname IntegerPoint\number\value{ExNum}\endcsname{\IntegerPoint}
    }


\newcommand{\NumberLine}{%

    \begin{tikzpicture}[xscale=1]
    
    \draw[thick] (0,0)--(10,0);
    
    \foreach \x in {0,1,2}{
        \node at (\x,-1) {\large \x};
        }

    \foreach \x in {0,1,2,...,10}{
        \draw[thick] (\x,-0.35)--(\x,0.35);
        }

        
    \filldraw [red, opacity=0.4]
          (\csname IntegerPoint\number\value{ExNum}\endcsname,0) circle (0.15cm) ;
    
    \end{tikzpicture}
    }



\newcommand{\Exercise}{%
    \NumberLine
    }

\newcommand{\Solution}{%
    $\csname IntegerPoint\number\value{ExNum}\endcsname$    
    }




\setlength{\parindent}{0cm}

\begin{document}

Where is the dot?

\InitVariables
\begin{exercise}
    \Exercise
    \end{exercise}
    
\begin{solution}
    \Solution
    \end{solution}


\InitVariables
\begin{exercise}
    \Exercise
    \end{exercise}
    
\begin{solution}
    \Solution
    \end{solution}


\printallsolutions

\end{document}

enter image description here

2

As in the previous question this is a matter of expansion and timing. The solution in both cases is \Solution. \Solution refers to the current value of the counter ExNum which at the time of printing the solutions unfortunately is the same for all exercises: the last value it has been set to before \printallsolutions.

We could try to expand the body of the solution environments. But I suggest another solution. Instead of a new counter use something that is unique for an exercise/solution pair. xsim already provides something: \ExerciseID.

Below is a modified version of your code which uses that:

\documentclass{article}

\usepackage{tikz}
\pgfmathsetseed{\number\pdfrandomseed}

\usepackage{xsim}

\newcommand{\InitVariables}{%
  \pgfmathrandominteger{\IntegerPoint}{3}{10}%
}

\newcommand{\NumberLine}{%
  \begin{tikzpicture}[xscale=1]
    \draw[thick] (0,0)--(10,0);
    \foreach \x in {0,1,2}{
      \node at (\x,-1) {\large \x};
    }
    \foreach \x in {0,1,2,...,10}{
      \draw[thick] (\x,-0.35)--(\x,0.35);
    }
    \filldraw [red, opacity=0.4] (\IntegerPoint,0) circle (0.15cm) ;
  \end{tikzpicture}
}

\newcommand{\Exercise}{%
  \NumberLine
  \expandafter\xdef\csname IntegerPoint\ExerciseID\endcsname{\IntegerPoint}%
}

\newcommand{\Solution}{%
  $\csname IntegerPoint\ExerciseID\endcsname$%
}

\begin{document}

Where is the dot?

\InitVariables
\begin{exercise}
  \Exercise
\end{exercise}
    
\begin{solution}
  \Solution
\end{solution}

\InitVariables
\begin{exercise}
  \Exercise
\end{exercise}
    
\begin{solution}
  \Solution
\end{solution}

\printallsolutions

\end{document}

enter image description here

1
  • This looks perfect. Thanks! I will take a closer look tonight and tomorrow and will report back here. :) – WeCanLearnAnything Jun 28 '20 at 5:17

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.