ProjectsPythonTeX and ANS

PythonTeX is a LaTeX-package that provides the opportunity to use Python inside a document. Python code can be executed and accessed. This makes possible to create and generate documents based on parameters inside the Python code. In particular, it provides opportunities to create and generate parameterized mathematical exercises.

In the online assessment system ANS, it is also possible to use Python code to generate parameterized mathematical exercises.

In this project we have created a database of parameterized mathematical exercises in LaTeX, and scripts to translate these exercises to ANS exercises using the same python code. In this way, exercises from our database can be used both for written exams as well as for online exams.

As ANS does not support upload of batches of exercises that contain python code one has to copy-paste the several parts of the exercise into an ANS-exercise, using their online exercise editor. How this can be done, you can see in the video below.

Example

Below you find an example of an exercise.

\question

\begin{pycode}
import sympy as sp;
import random as ra;
import fractions as fr;
x,y,z,C=sp.symbols('x y z C');

a=ra.randint(2,5);
aa=a**2

b=ra.randint(2,5);
bb=b**2

f=sp.Lambda(x,1/(1+a*sp.sqrt(x)));
g=sp.Lambda(y,y**2);
fy=2*y/(1+a*y)

gtex=sp.latex(g(y)).replace("log","ln")

fytex=sp.latex(fy).replace("log","ln")


integrant=sp.latex(f(x)).replace("log","ln")

inty=sp.Lambda(y,sp.integrate(fy,y))
intytex=sp.latex(inty(y))

F=sp.Lambda(x,sp.integrate(f(x),x))

int=F(bb)-F(0)


correct=sp.latex(int).replace("log","ln");
wrong1=sp.latex(2*int).replace("log","ln")
wrong2=sp.latex(int/2).replace("log","ln")
wrong3=sp.latex(-int).replace("log","ln")
wrong4=sp.latex(-2*int).replace("log","ln")
wrong5=sp.latex(-int/2).replace("log","ln")
\end{pycode}


%%%%Question


$\displaystyle\int_0^{\py{bb}} \py{integrant}dx=$


%%%%Answers

\begin{randomizechoices}
\choice $\py{wrong1}$
\choice $\py{wrong2}$
\choice $\py{wrong3}$
\choice $\py{wrong4}$
\choice $\py{wrong5}$
\CorrectChoice $\py{correct}$
\end{randomizechoices}


%%%Solution
\begin{solution}
With substitution $x=y^2$ we find $dx=2y dy$.
So
$$\begin{array}{ll}\displaystyle\int_0^{\py{bb}} \py{integrant}dx
&=\displaystyle\int_0^{\py{b}} \py{fytex}dy\\
&=\displaystyle\int_0^{\py{b}} \frac{2}{\py{a}}-\frac{2/\py{a}}{\py{a}y+1}dy\\
&=\py{intytex}]_0^{\py{b}}\\
&=\py{correct}\end{array}$$
\end{solution}

In this example we use the LaTeX-package exams for creating a multiple choice exercise. The answer options are to be found within the tags \begin{randomizechoices}...\end{randomizechoices}. Python code is added within the \begin{pycode}...\end{pycode} environment. The variables can be accessed using the LaTeX command \py{name_of_variable}.

Of course the above exercise can not only be used as a multiple choice exercise, but also as an open exercise, just by removing the answer options.

A solution to the exercise is given between \begin{solution}...\end{solution}.

Notice that we can load Python libraries. In particular, loading the sympy-library is of importance. This library makes it possible to use Python as a computer algebra package, and enables symbolic computations. Moreover, sympy has LaTeX support which enables a nice rendering of the python variables.

We use a script to translate the LaTeX source to a file which is better suited for using in ANS.

\question

\begin{pycode}
import sympy as sp;
import fractions as fr;
x,y,z,C=sp.symbols('x y z C');

a=random(2,5);
aa=a**2

b=random(2,5);
bb=b**2

f=sp.Lambda(x,1/(1+a*sp.sqrt(x)));
g=sp.Lambda(y,y**2);
fy=2*y/(1+a*y)

gtex=sp.latex(g(y)).replace("log","ln")

fytex=sp.latex(fy).replace("log","ln")


integrant=sp.latex(f(x)).replace("log","ln")

inty=sp.Lambda(y,sp.integrate(fy,y))
intytex=sp.latex(inty(y))

F=sp.Lambda(x,sp.integrate(f(x),x))

int=F(bb)-F(0)


correct=sp.latex(int).replace("log","ln");
wrong1=sp.latex(2*int).replace("log","ln")
wrong2=sp.latex(int/2).replace("log","ln")
wrong3=sp.latex(-int).replace("log","ln")
wrong4=sp.latex(-2*int).replace("log","ln")
wrong5=sp.latex(-int/2).replace("log","ln")


\end{pycode}


$$\displaystyle\int_0^{<span data-redactor-type='variable'>bb</span>} <span data-redactor-type='variable'>integrant</span>dx=$$

\begin{multicols}{2}
\begin{randomizechoices}

\choice $$<span data-redactor-type='variable'>wrong1</span>$$
\choice $$<span data-redactor-type='variable'>wrong2</span>$$
\choice $$<span data-redactor-type='variable'>wrong3</span>$$
\choice $$<span data-redactor-type='variable'>wrong4</span>$$
\choice $$<span data-redactor-type='variable'>wrong5</span>$$


\CorrectChoice $$<span data-redactor-type='variable'>correct</span>$$

\end{randomizechoices}
\end{multicols}
\begin{solution}
With substitution $$x=y^2$$ we find $$dx=2y dy$$.
So
$$$\begin{array}{ll}\displaystyle\int_0^{<span data-redactor-type='variable'>bb</span>} <span data-redactor-type='variable'>integrant</span>dx
&=\displaystyle\int_0^{<span data-redactor-type='variable'>b</span>} <span data-redactor-type='variable'>fytex</span>dy\\
&=\displaystyle\int_0^{<span data-redactor-type='variable'>b</span>} \frac{2}{<span data-redactor-type='variable'>a</span>}-\frac{2/<span data-redactor-type='variable'>a</span>}{<span data-redactor-type='variable'>a</span>y+1}dy\\
&=<span data-redactor-type='variable'>intytex</span>]_0^{<span data-redactor-type='variable'>b</span>}\\
&=<span data-redactor-type='variable'>correct</span>\end{array}$$$


\end{solution}

Notice that the import of the random library is removed. Moreover, a=random(2,5) is a new syntax for defining a random variable. This is because ANS has its own random-library that acts differently from the standard python library.

This library has some strange behavior.

If you run the code

a=random(2,5)
b=random(2,5)

then a and b will have the same value!

Of course here one can use

a=random(2,5)
b=random(1,4)+1

The different parts of the above exercise can be copy-pasted in the online editor of ANS.

Run PythonTeX

In the video below you can see how PythonTeX can be used. We include the above exercise three times in our exam file, and run pdflatex, pythontex and again pdflatex. The result is an exam with three instances of the same exercise but different parameters,

Notice that running the commands again will result in the same file. Indeed, the random choices use a fixed seed. If you want new instances, you can change the seed.

Edit ANS exercise

Go to ANS and open an exercise. Open the file generated by our script. Now copy the relevant parts of the file into the exercise. Notice that you have to copy it as raw XML/HTML text!

Test the script by pressing run, or view the generated exercise in ANS.

More information

For more information, contact Hans Cuypers (f.g.m.t.cuypers@tue.nl)

References