# Forms and graphs

I am trying to write a document for my students to practice on quadratic functions. Using a form, the student can enter the coefficients of the quadratic functions and is then given the discriminant (and later the roots of the function).

I think it would be great if the graph of the function could be traced on the pdf. Is there a way to do this? How can the form transfer values to the tikzpicture environment?

Here is my MWE:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.3}

\usepackage[pdftex]{insdljs}

\begin{insDLJS}[test]{test}{JavaScript}

function doCalculation()
{var disc=0.0 + (this.getField("valb").value)*(this.getField("valb").value)
-4*(this.getField("vala").value)*(this.getField("valc").value);
this.getField("valdisc").value=disc;
}

function doDrawing()
{}

\end{insDLJS}

\begin{document}

\begin{Form}

Please enter the coefficients of the quadratic function $f(x)=ax^2+bx+c$

\vskip 5mm

\TextField[name=vala,width=2em, bordercolor={0.650 .790 .94}]{Coefficient a: }
\TextField[name=valb,width=2em, bordercolor={0.650 .790 .94}]{Coefficient b:}
\TextField[name=valc,width=2em, bordercolor={0.650 .790 .94}]{Coefficient c:}

\vskip 5mm

\PushButton[name=start,onclick={doCalculation();},bordercolor={0.650 .790 .94}{Compute!}

\vskip 5mm

\vskip 10mm

\PushButton[name=start,onclick={doDrawing();},bordercolor={0.650 .790 .94}]{Draw!}

\vskip 5mm

\begin{tikzpicture}
\begin{axis}[title=Parabola, axis lines=left, xlabel={$x$},ylabel={$y$},ymin=-3, ymax=3,minor y tick num=1,]
\newcommand\coefa{1}
\newcommand\coefb{4}
\newcommand\coefc{3}
{\coefa*x^2+\coefb*x+\coefc};
\end{axis}
\end{tikzpicture}

\end{Form}

\end{document}


I am trying to tell the functions \coefa, \coefb and \coefc to take the values given by the user in the form. An alternative version not using tikz is also welcomed.

Is it possible to do it? I am afraid it is not possible since it may mean re-compiling the tex file in order to get the picture. But, for the computation of discriminant, the result "magically" appears without any compilation. I am totally ignorant on how forms work in Latex...

Edit: As explained in the comments, the answer will depend on the viewer. You can assume the viewer is Acrobat Reader, since it is free and ubiquitous. If there is a solution that works with another viewer, I am also interested in it.

• You aren't using TeX to do the calculation, though. You are using the PDF viewer. (And it will work in some PDF viewers and fail to work in others for just this reason. So make sure your students know they need to use an appropriate viewer.) Unless the viewer can also plot functions on-the-fly, you are not going to be able to do this with having access to something else to do the plotting (TeX or whatever). That is, you can't set up in advance for the PDF viewer to have access to the facilities offered by the compiler unless the compiler will be available. Sorry, I'm not explaining well. – cfr Oct 8 '14 at 2:39
• @cfr: thank for the explanation on how forms work. It makes things clear to me. Do you know a free reader that can plot on-the-fly? I don't understand the second part. Is there a way to make the compiler available? – Taladris Oct 8 '14 at 2:53
• I think as you have put it, it is not possible to update the plot itself since there is no pgfplots engine is present at the time of recomputation. It compiles only once. But if you are feeling adventurous there is the clickable library that uses insdljs too. You can delve into that code to make the curve also a dynamic object. – percusse Oct 8 '14 at 5:12