1

I'm trying to create exercise sheets with random numbers and xsim. Each time I generate random numbers, though, the variables are overwritten to create new exercises (desired), but also overwritten so that all solutions only contain values from the final exercise (not desired).

How do I "save" the values of the solutions as they're generated?

I am aware that this maybe somewhat similar to one of my prior threads, but I tried to follow the \edef... syntax advice from there and it did not seem to work.

\documentclass{article}

\usepackage{multicol}

\usepackage{pgf}

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

\usepackage{xsim}

\setlength{\parindent}{0pt}


\newcommand{\InitVariables}
    {   \pgfmathrandominteger{\PartA}{0}{10}
            \edef\PartA{\PartA}
        \pgfmathrandominteger{\PartB}{0}{10}
            \edef\PartB{\PartB}
        \pgfmathsetmacro{\Sum}{int(\PartA + \PartB)}
            \edef\Sum{\Sum}
        }

\newcommand{\Exercise}{$\PartA+\PartB=?$}

\newcommand{\Solution}{$\PartA+\PartB=\Sum$}

\begin{document}

\begin{multicols}{2}


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

\begin{solution}
    \Solution
    \end{solution}


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

\begin{solution}
    \Solution
    \end{solution}

\printallsolutions

\end{multicols}

\end{document}

enter image description here

1 Answers1

2

You only need to add a counter and create unique macro names using that counter. This counter needs to be reset before \printallsolutions.

\documentclass{article}

\usepackage{multicol}

\usepackage{pgf}

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

\usepackage{xsim}

\setlength{\parindent}{0pt}
\newcounter{myex}

\newcommand{\InitVariables}{%
       \stepcounter{myex}%
       \pgfmathrandominteger{\PartA}{0}{10}%
       \expandafter\edef\csname PartA\number\value{myex}\endcsname{\PartA}%
       \pgfmathrandominteger{\PartB}{0}{10}%
       \expandafter\edef\csname PartB\number\value{myex}\endcsname{\PartB}%
       \pgfmathsetmacro{\Sum}{int(\PartA + \PartB)}%
       \expandafter\edef\csname Sum\number\value{myex}\endcsname{\Sum}%
        }

\newcommand{\Exercise}{$\csname PartA\number\value{myex}\endcsname+\csname PartB\number\value{myex}\endcsname=?$}

\newcommand{\Solution}{\stepcounter{myex}%
$\csname PartA\number\value{myex}\endcsname+\csname PartB\number\value{myex}\endcsname=
\csname Sum\number\value{myex}\endcsname$}

\begin{document}

\begin{multicols}{2}


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

\begin{solution}
    \Solution
    \end{solution}


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

\begin{solution}
    \Solution
\end{solution}

\setcounter{myex}{0}
\printallsolutions
\end{multicols}

\end{document}

enter image description here

  • Thanks! I look forward to reading this carefully over the next day or two. – WeCanLearnAnything May 28 '20 at 20:31
  • I just tested the code and it works! I understand most of it, but not all. What does \expandafter\edef do? I am aware that \edef is in code I've written, but I don't actually know what it does on its own either. – WeCanLearnAnything May 29 '20 at 05:11
  • 1
    @WeCanLearnAnything This code creates macros of the type \PartA1, \PartA2 and so on, where 1, 2 is the value of the counter myex. This makes the macros unique, and prevents them from being overwritten. One cannot say in TeX \edef\PartA1{9}, and even if we were working with roman numbers, we would have to compose the macro name. This is what \expandafter\edef\csname PartA\number\value{myex}\endcsname{\PartA}% does. \csname something\endcsname "creates" a macro \something. ... –  May 29 '20 at 05:17
  • 1
    We want to first "create" the macro and then assign it a value with \edef. This is what \expandafter does: it basically says wait with \edef until I have looked at, or, more precisely, expanded, what comes after it. Otherwise \edef would only see \csname and not \PartA1, say. –  May 29 '20 at 05:19
  • 1
    I think this answer has a very decent description of this trick. (Note that pgf keys are based on the same mechanism, and we could have used them here, too.) –  May 29 '20 at 05:37
  • Ok, I will need to study the answers here a little more, but it is clear that this is all, in principle, a very efficient solution. The MWE provided in the OP, though, has far fewer variables than what I originally intended, which is this answer key. https://www.overleaf.com/read/wfvcjkmtvmvd . [Each time you recompile, random numbers should generate a new file.] Do I need to just insert all the counter, \expand\edef, \csname... stuff in for each variable individually? – WeCanLearnAnything May 29 '20 at 22:18
  • @WeCanLearnAnything I think that there is no way to avoid defining unique macros in one way or another. Otherwise they will be just overwritten, as you saw in your question. However, what can definitely be done is to make the generation of these unique macros more convenient/automatic. What is most convenient depends on the actual use case. –  May 29 '20 at 23:09
  • What are these ways to make it more convenient/automatic? – WeCanLearnAnything Jun 24 '20 at 18:18
  • Another question: Why is \stepcounter{myex} necessary within \Solution but not in \Exercise? – WeCanLearnAnything Jun 24 '20 at 18:41