## AMC with sagetex

Does anyone have experience with AMC and sagetex [1]?

This could really expand the possibilities of package fp.

With sage, it is possible to do symbolic derivation, plotting a computed function, or solving a linear system of equations, to name a few.

### Replies (3)

#### RE: AMC with sagetex - Added by Pablo UPMover 1 year ago

A small inconvenience is that in order to use sagetex you have to:

1. run pdflatex sagetex_example
2. run sage sagetex_example.sagetex.sage
3. run pdflatex sagetex_example again

#### RE: AMC with sagetex - Added by Pablo UPMover 1 year ago

Running sage in between is only a small inconvenience. The problems with the sage verbatim environments are more serious:

• Inline environments can not be used to assign to variables
• Multiline environments are equivalent to verbatim environments, which mess up with AMC seriously
• It is crucial to define variables to do really meaningful examples

Partial+inconvenient workaround: combine latex variables and sage commands. Example:

\element{T1q1}{
\FPeval\a{trunc(6 + random * 3,0)}
\FPeval\b{10-\a}

\begin{question}{T1q1}

Statement of the question:

\a, $\a$, \sage{2+3}, \sage{\a}, \sage{\b}, \sage{2+\a}

\AMCOpen{lines=2, dots=False}{
\wrongchoice[0]{0}\scoring{0}
\wrongchoice[0.5]{0.5}\scoring{0.5}
\wrongchoice[1]{1}\scoring{1}
\wrongchoice[1.5]{1.5}\scoring{1.5}
\wrongchoice[2]{2}\scoring{2}
}
\explain{
Solution is

\sageplot[width=10cm]{plot(sin(x), 0, pi)}

\sageplot[width=10cm]{plot(\a*sin(x), 0, pi)}
}
\end{question}
}


I guess I should be happy, since this is better than fpeval alone, but it is not possible to store a polynomial, matrix, function, etc in an fpeval variable.

#### RE: AMC with sagetex - Added by Pablo UPMover 1 year ago

Much better, I added a sageraw environment to sagetex.sty

\newcommand{\sagerawline}[1]{\ST@wsf{\%
try:^^J
_st_.current_tex_line = \the\inputlineno^^J
#1^^J\%
except:^^J
_st_.goboom(\the\inputlineno)}\%
\stepcounter{ST@inline}}


(note: the \% go without a backslash, but this forum complains if I leave the percentage sign alone)

and now I can define variables within latex

\sagerawline{m=randint(10,20)}


Then make computations and plots
\sage{Combinations(100,m).cardinality()}



My limited knowledge of latex does not allow me to do multiline sage raw input without relying on verbatim. I understand the problem is not easy even for the latex masters.

My sagerawline also breaks sageplot in DOC-corrige (although it works in DOC-sujet), but this can be done in other ways.

Not perfect, but this will suffice: each student gets a different question, but we are not limited to basic arithmetic.

This could be done with any other script language (at least haskell and R had latex extensions before Sage did).

(1-3/3)