1

Over the past few years, I have constructed a large number of math questions which are stored in various external files. I access them for my quizzes via the probsoln package.

I typically construct the questions using quite a few randomly-generated values. The trouble comes when I try to access the solutions using the "second-pass" suggestion (the first pass producing the quiz, the second the answer key).

Here is a very abbreviated MWE.

\documentclass{article}
\usepackage{probsoln}

\newcounter{firstNum}
\newcounter{secondNum}
\newcounter{answerNum}

\begin{document}
  \hideanswers
  \loadallproblems{MWEQuestions}

  Quiz:
  \begin{enumerate}
    \foreachproblem{%
        \item \thisproblem
    }
  \end{enumerate}

  \medskip  

  Answer Key:
  \showanswers
  \begin{enumerate}
    \foreachproblem{%
        \item \thisproblem
    }
  \end{enumerate}
\end{document}

The question is stored in an external file, and has this format:

\newproblem{question1}{%
  \random{firstNum}{1}{5}
  \random{secondNum}{6}{10}
  \defcounter{answerNum}{\thefirstNum+\thesecondNum}

  \(\thefirstNum + \thesecondNum=\)
}{%
  \thefirstNum+\thesecondNum = \theanswerNum
}

When run, the output looks like this:

Quiz:

  1. 2 + 9 =

Answer Key:

  1. 4 + 10 =

    Solution: 4 + 10 = 14

Obviously the second iteration through has re-randomized the counter values. I need some way to preserve the selected values from being over-written so they can be accessed when the solutions are generated.

Another (possible) wrinkle is that most of my problems use the same counters over again. So the definition of question 2 would also assign random values to the counters. So it's not just a matter of preventing the solutions pass from overwriting the counter values, but the next question as well.

Thanks for any help you can give!

[EDIT]

I may have found a solution. It works with my MWE, but before I post it, I want to test it in a slightly more robust setting.

1
  • I'm still trying to figure out an answer, but in the meantime, would someone venture a guess as to why the question isn't receiving up-votes? If it is a poor question I'll happily re-phrase or even retract it.
    – HTG
    Nov 12 '14 at 9:27
0

The following code seems to work, for simple cases at least. The first block of code is the quiz itself. It opens a write stream and loads the questions from an external file. The second block of code is the file containing the questions, and this is where the actual writing and reading of the solution is done.

\documentclass{article}

\usepackage{probsoln}

\newwrite\answerfile %Going to store current values in an external file

%Counters to hold randomly generated values for the math questions
\newcounter{firstNum}
\newcounter{secondNum}
\newcounter{answerNum}


\begin{document}
    \immediate\openout\answerfile=MWEAnswers.tex %Write to the files immediately
    \loadallproblems{MWEQuestions}               %Load all problems from external file

%Start typesetting the quiz questions
Quiz:
\begin{enumerate}
    \foreachproblem{%                            %Iterates through the loaded problems
        \item \thisproblem                       %Typesets the current problem
    }
\end{enumerate}
\immediate\closeout\answerfile                   %Done writing to the answer file
\medskip

\immediate\openin\answerfile=MWEAnswers.tex      %Now we want to read from the answer file
\showanswers                                     %Boolean to allow typesetting the answers

% Start Answer Key
Answer Key:
\begin{enumerate}
    \foreachproblem{                            %Again, iterate through the loaded problems
        \item \thisproblem
    }
\end{enumerate}
\end{document}

Here is the code for the question file. Note that in order to see that multiple questions didn't cause problems the problem contains the definitions of two separate problems. In terms of code, these are identical (other than one is an addition question and the other a subtraction.

\newproblem{question1}{                                  %Define Question 1
    \random{firstNum}{1}{5}                              %Randomly generate values
    \random{secondNum}{6}{10}
    \defcounter{answerNum}{\thefirstNum+\thesecondNum}   %Calculate answer

\begin{onlyproblem}                                      %This code only runs if the answers
                                                         %are NOT being typeset
    \(\thefirstNum + \thesecondNum=\)                    %Typeset the question for the quiz

                                                        %Write question to external answer file
    \immediate\write\answerfile{\thefirstNum+\thesecondNum=\theanswerNum}
\end{onlyproblem}

}{                                                      %This reads in the solution.
    \read\answerfile to \fileline
    $\fileline$
}

%%% Second Question--Code Identical to the First
\newproblem{question2}{%                                % Define Question 2
    \random{firstNum}{1}{5}
    \random{secondNum}{6}{10}
    \defcounter{answerNum}{\thefirstNum-\thesecondNum}

\begin{onlyproblem}
    \(\thefirstNum - \thesecondNum=\) % Print the question
        
    \immediate\write\answerfile{\thefirstNum-\thesecondNum=\theanswerNum}
\end{onlyproblem}

}{%
    \read\answerfile to \fileline
    $\fileline$
}

The output for this code is

screen cap of code output

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.