# How to use sagesilent (or verbatim) environment in pgffor loops?

I want to create different versions of an exam paper and corresponding sample solutions using sagetex. I need to assign variables for more involved computations, so I am reliant on the sagesilent environment. However, this environment seems to be a type verbatim environment and that seems to be incompatible with \foreach loops the the pgffor package provides. The latter is needed to generate the different versions of the exam.

Error messages look like this:

ERROR: Paragraph ended before \verbatim@ was complete.

Is there a way to make the two packages work together? Ideally, a solution would optionally allow the expansion of macros in the sagesilent environment, like in this answer.

An MWE example is given below (without macro expansion).

\documentclass{minimal}
\usepackage{pgffor}
\usepackage{sagetex}
\begin{document}
\foreach \x/\y in {3/4,5/6,7/8} {

The product of $\x$ and $\y$ is $\sage{\x*\y}$. % this works

% but not this: (the assignment z= cannot be done in \sage{})
\begin{sagesilent}
z=2*9
\end{sagesilent}

}
\end{document}

• I should add that I did look into different loop options, e.g., using latex3 like suggested in this answer. This didn't work either. – bjoseru Jan 12 '16 at 6:57
• Why do you need to mix the two packages? It seems like sagetex can do everything by itself. – DJP Jan 12 '16 at 20:14
• I am actually planning to use a non-public package supporting dedicated question banks, and this works by storing exam questions in macros that can be pulled in later as needed. pgffor is a placeholder for that. I guess my main problem is that I need sagetex environments that can be stored in a macro and be evaluated (by sage) only once the macro is expanded. For this, verbatim-type environments seem to be the wrong path. – bjoseru Jan 13 '16 at 21:28
• I don't think that it can be done; but I don't know enough to say it can't be done. If you aren't able to get a response here, you might try posting a question at AskSagemath. It seems like Python with sagetex should be able to do everything by, say, storing each exam question as a string in sagesilent and printing it as needed. – DJP Jan 14 '16 at 0:44
• Thanks for your opinion. I'd really like this to work with this exam package I mentioned, so ideally without any external tools like python. I remain hopeful that someone comes up with an idea. – bjoseru Jan 14 '16 at 13:37