1

I want to show the computation of two values in the questionaire where the user choose one option per question. Many measure proprietary sources of research have PDF documents such as Mielenterveystalo.fi which have such a feature. I am thinking how to accomplish it with LaTeX. The task is the computation (2+2) from checkboxes and then showing the result (4). Expected frontend in Fig. 1. Most potential thread answer about the issue is the answer here with etoolbox package but from 2012 approach so I hope the approach has improved. Code where no checkboxes and gamma box is not a textbox so not accepting input

\documentclass{article}
\usepackage{amssymb}
\usepackage{hyperref}
\begin{document}

\begin{Form}

% TODO make two questions with three checkboxes

% https://tex.stackexchange.com/a/76500/13173
\TextField[maxlen=40,align=2,height=10pt,width=45pt,name=alpha]{Alpha}

\bigskip
\TextField[maxlen=40,align=2,height=10pt,width=45pt,name=beta]{Beta}

\bigskip
\TextField[
  maxlen=40,
  calculate={%
    var f_alpha = this.getField("alpha");
    var f_beta = this.getField("beta");
    event.value = f_alpha.value + f_beta.value;
  },
   align=2,height=10pt,width=45pt,name=gamma,readonly=true]{Gamma}
\end{Form}

% Count core here
Summary score:

\end{document}

Bugs in the code

  • does not prevent selection of one or more options. Allow selection two options per question but count the average then. Connect average function then there.

Action: check two places of point (2) in questions (1-2)

Expected output: show value 4 in Summary Score in Adobe Acrobat (acroread in Linux)

Fig. 1 Expected frontend

enter image description here

OS: Debian 9
TeXLive: 2017

2

In order to create radio buttons using hyperref, use the \ChoiceMenu command. Combining this with a custom JavaScript command to populate a text field as demonstrated in the linked answer, we get

\documentclass{article}
\usepackage{hyperref}
\begin{document}

\begin{Form}
\begin{enumerate}
\item \ChoiceMenu[name=football,radio,default=0]{Do you play football?}{Much (2)=2,Little (1)=1,Not at all (0)=0}
\item \ChoiceMenu[name=ice-hockey,radio,default=0]{Do you play ice-hockey?}{Much (2)=2,Little (1)=1,Not at all (0)=0}
\end{enumerate}

\TextField[readonly=true,value=0,calculate={event.value=this.getField("football").value+this.getField("ice-hockey").value;}]{Summary score:}
\end{Form}

\end{document}

screenshot of the resulting interactive form

Note that this only works with PDF viewers that support JavaScript, i.e. you are essentially limited to Adobe Acrobat (Reader).

In the above code, the option "Not at all" is preselected using default=0 in order to allow calculations before all questions are answered. If you do not like the selection symbol to appear there from the beginning, change default=0 to default=-0 in order to stop hyperref to automatically select the "Not at all" field, and possibly remove the default value=0 from the \TextField. That way, unanswered questions will contribute with score 0 to the end result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.