# How to use sage commands in a loop?

I would like to create a worksheet with randomized questions and their solution. The following attempt has not been successful, throwing "Paragraph ended before \multido@@ was complete."

\documentclass[11pt,twocolumn]{scrartcl}
\usepackage{sagetex}
\usepackage{multido}

\begin{document}
\begin{sagesilent}
x, y = var('x, y')
\end{sagesilent}

\multido{}{5}{
\begin{sagesilent}
a = Integer(randint(1,9))
b = Integer(randint(1,9))
c = Integer(randint(1,9))
d = Integer(randint(1,9))
e = Integer(randint(1,9))
f = Integer(randint(1,9))
\end{sagesilent}

\begin{eqnarray*}
\sage{a}x+\sage{b}y&=&\sage{c} \\
\sage{d}x+\sage{e}y&=&\sage{f}
\end{eqnarray*}
\begin{sagesilent}
sol=solve([a*x+b*y==-c,d*x+e*y==f],x,y)
\end{sagesilent}
Sol: $\sage{sol}$
}
\end{document}


Any idea how I could get it done? Thanks!

## 2 Answers

The sagetex package goes through 1. a LaTeX compilation followed by 2. a Sage compilation (where all calculations are performed), followed by 3. a LaTeX compilation which inserts the calculations from step 2 into the LaTeX document. When you use multido with sage commands inside of it then your document will fail on the first pass because LaTeX compilation is dependent on sage calculations that haven't been calculated yet. You can't construct a document this way. Here is a possible way to achieve what you want.

\documentclass[11pt,twocolumn]{scrartcl}
\usepackage{sagetex}
\usepackage{amsmath}
\begin{document}
Try this:\\\\
\noindent
\begin{sagesilent}
x, y = var('x,y')
output = r""
for i in range(0,5):
a = Integer(randint(1,9))
b = Integer(randint(1,9))
c = Integer(randint(1,9))
d = Integer(randint(1,9))
e = Integer(randint(1,9))
f = Integer(randint(1,9))
output += r"\begin{tabular}{ccc}"
output += r"$%s x+%s y$ & $=$ & $%s$\\ "%(a,b,c)
output += r"$%s x+ %s y$ & $=$ & $%s$\\ "%(d,e,f)
output += r"\end{tabular}\\"
eqns = [a*x+b*y==-c, d*x+e*y==f]
sol = solve(eqns,x,y,solution_dict=True)
for ans in sol:
output += r"\\Sol: $x=%s, y=%s$ \\\\"%(latex(ans[x]), latex(ans[y]))
\end{sagesilent}
\sagestr{output}
\end{document}


Now there isn't a LaTeX document to speak of since sagestr is part of the sage compilation process. So there is no problem with the first LaTeX compilation. The sage compilation is next, and by putting the output into a string of LaTeX code, all the calculations and LaTeX code are inserted in the third step of compilation. The result is shown running in Sagemath Cloud.

Note that in trying to print out the solution you would get a list. I've illustrated how you can get just "x" and "y" and print them in LaTeX using the latex() command mentioned here

Not really an answer but I can't comment yet. You can try to read this article.

It makes sense if you want to send assignments to your students but doesn't work so well if you plan to have different tests. In the last situation my (ugly) workaround consists in generate a exams.txt file from a sagemath notebook with all the latex code and then insert inside a latex using \input{exams.txt} The advantage is that you can compile again the .tex file without overwrite it. I'm interested in more elegant solutions. This is an example you can find the solutions from page 50.