# Context

I'm trying to get an automatic exam where I can generate random numbers (using pgfmathrandominteger) in the AMC package to create different questions using code (listings) and inputing these random numbers to the code. For example, change the value a variable will get, or change the number of iterations on a loop, etc.

# Problem

However, the listing package is not easy to work with in the AMC environment (see How to expand a macro to use it inside a question in AMC?). Moreover, to add the random number that should appear on the code, and to be generated that number from the question is becoming a challenge.

For example, in the code below, I can generate the number (\pgfmathrandominteger{\mynum}{1}{8}) but when using more questions it doesn't work (same happens when using more than one copy).

Ideally, I should generate the random number inside the question, so every time the question is parsed by AMC a new number is generated and then it should be parsed by the box. However, the box is already typeset when declared (as far as I understand). Thus, when I use it on the lstlisting code (if I could reach the \mynum macro) I still need to be able to reform that environment again and again.

How that behavior can be achieved?

\documentclass{article}

\usepackage[box]{automultiplechoice}
\usepackage{listings}
\usepackage{tikz}
\lstset{  escapeinside={@*}{*@}}

% a simple wrapper to create boxes automatically
\makeatletter
\newcounter{myboxcounter}
\newenvironment{mybox}{%
\stepcounter{myboxcounter}%
\expandafter\newsavebox\csname foobox\roman{myboxcounter}\endcsname
\global\expandafter\setbox\csname foobox\roman{myboxcounter}\endcsname\hbox\bgroup\color@setgroup\ignorespaces
}{%
\color@endgroup\egroup
}
\newcommand{\insertbox}{\stepcounter{myboxcounter}%
\edef\name{foobox\roman{myboxcounter}}\edef\x{%
\expandafter\usebox\csname\name\endcsname}\x}
\makeatother

\begin{document}

%%% preparation of the groups
\pgfmathrandominteger{\mynum}{1}{8}
\begin{mybox}
\begin{lstlisting}[language=C++]
int a = @*\mynum*@;
a = a + 10;
\end{lstlisting}
\end{mybox}
\element{code}{
\begin{question}{code 1}
Which is the result of \texttt{a} \mynum?

\insertbox
\begin{choices}
\correctchoice{\pgfmathparse{\mynum+10}\pgfmathresult}
\wrongchoice{20}
\wrongchoice{0}
\wrongchoice{30}
\end{choices}
\end{question}
}

%%% copies
\setcounter{myboxcounter}{0}
\onecopy{1}{% when changed the number of copies to more than one it won't work
\insertgroup{code}
}

\end{document}

• it doesn't work. Explain what doesn't work. – jub0bs Mar 30 '15 at 1:09
• @Jubobs fixed the problem, by changing the number of copies (in \onecopy{2} to 1). That is one of the problems that the macro part won't work on multiple copies (by several questions or copies). – adn Mar 30 '15 at 1:21

You shouldn't use listings inside arguments, but if you are asking for troubles, another experimental workaround (explained in the documentation) is add a line feed (^^J) at the end of each line, escape \{}% characters and spaces. Unfortunately, then you can’t escape to LaTeX inside such listings (according to package documentation) so you have little chance of include here random values.

However, without listings (or using it only for immutable code) make mathematical problems with random values in AMC is easy using the fp package. A naïve example:

\documentclass{article}
\usepackage[box]{automultiplechoice}
\usepackage{fp}
\usepackage{amsmath}
\usepackage{listings}

\element{code}{
\begin{question}{sum}
\FPeval\Ra{trunc(1+random*8,0)}
\FPeval\Rb{trunc(4+random*5,0)}
\FPeval\RnoA{clip((Ra+1)/Rb)}
\FPeval\RnoB{clip(Ra*Rb+100)}
\FPeval\RnoC{clip(Ra-Rb+1)}
\FPeval\Ryes{clip(Ra+Rb)}
\begin{lstlisting}^^J
a ? b = c ^^J
\end{lstlisting}
Check the equation with correct result ($c$) when $a = \Ra{}$ and $b = \Rb{}$?

\begin{choices}
\wrongchoice{   $\dfrac{\Ra{}}{\Rb{}} = \RnoA$}
\wrongchoice{   $\Ra{}^{\Rb{}} = \RnoB$}
\wrongchoice{   $\Ra{} - \Rb{} = \RnoC$}
\correctchoice{ $\Ra{} + \Rb{} = \Ryes$}
\end{choices}
\end{question}}

\lstset{basicstyle=\color{red!50!black}\bfseries\sffamily,frame=none}

\begin{document}
\onecopy{2}{\insertgroup{code}}
\end{document}


Edit: OK, ok, you want the best of everything, no matther how complicated could be.

One dirty trick to surpass this problem is use \input{} for included code with macros inside. I am not sure how safe is this method, so use at your own risk. To modify the above example step by step:

1) Include an escape character

\lstset{escapechar=!, ....}


2) Cut the lstlisting environment, save it as chunk.tex, remove the line feeds (^^J) and insert the random values escaped:

\begin{lstlisting}
!\Ra\ ? \Rb\ = c
\end{lstlisting}


3) Then insert \input{chunk} in the exam.

4) Compile and check the result:

Edit 2:

Another option could be try other methods of code listing. Check the Topic list­ing in CTAN. You mileage may vary depending of your aims about the format and the compatibility with AMC. For example, the package minted is algo good for code listing but seem also incompatible with the above MWE, whereas the package algpseudocode allow use a algorithmic environment that seem to work well inside the question and including the variables \Ra and \Rb:

\begin{algorithmic}[1]
\State $sum\gets \Ra$
\State $i\gets \Rb$
\While{$i\le n$}
\State $sum\gets sum+i$
\State $i\gets i+1$
\EndWhile
\end{algorithmic}


• In your solution the code inside the listing is always the same. However, I need to have a different code (that is generated using the random numbers for example) and insert it into the questions. The idea is to have for example different values for the loops, or for the variables, and create different versions of the same question. However, in your solution, I need to put the options of the code inside the question, which may difficult the problem if there are several variables that change. I am looking for a way to change the listing inside the question, due to RNG. – adn Apr 15 '15 at 21:06
• @adn See the edit of the answer, but personally my election would be left aside listings and format the small (?) chunks of code in another way less troublesome. – Fran Apr 15 '15 at 22:36
• Is there any other option to produce nice code, like listings but without its drawbacks? The main idea is to obtain the same solution: produce code automatically for each question using the random generation functions. – adn Apr 18 '15 at 19:47
• @adn, I am not an expert in code listing and I do not know do with what you will settle, but may be the last edit can help. Good luck. – Fran Apr 19 '15 at 18:49