2

I want to store an answer key for an exam and then automatically move the multiple choice answer choices around for each question match its predetermined answer. (I need to conform my multiple choice exam to a predetermined answer key in order to use these cool scratch-off scantrons called "IF-AT" scantrons).

I have a partial solution (see MWE at bottom) based one three steps.

  1. Store the answers for the exam in an array using arrayjob.sty, 1234 meaning ABCD respectively. For example, \usepackage{arrayjob} \newarray\answerkey \readarray{answerkey}{3&4} records the answer to Q1 as C and the answer to Q2 as D.
  2. Then \arabic{question} gives you the question number, and so \answerkey(\arabic{question}) gives you that previously stored answer for that question number.
  3. Use ifthenelse to execute different behavior based on that number \answerkey(\arabic{question}).

However, I can't figure out how to get ifthenelse to read \answerkey(\arabic{question}). In other words,

\ifthenelse{\equal{1}{\answerkey(\arabic{question})}}{some code}{}

doesn't work. I think it's a matter of expanding macros at the right time.

In the MWE below I commented out the code testing arrayjob.sty + ifthen.sty because it fails to compile.

For good measure, I also tried replacing the two packages with pgfmath.sty and fp.sty respectively (Qs 6, 7, 8 in MWE below), but no combination compiles.

\documentclass{exam}
\usepackage{ifthen, fp, arrayjob, pgfmath}
\newarray\answerkey %arrayjob
\readarray{answerkey}{4&3&2&1&1&1&1&1}
\def\answerkeytwo{{{},4,3,2,1,1,1,1,1}} %pgfmath
\begin{document}
\begin{questions}
\question testing arrayjob...
    \answerkey(\arabic{question})
\question testing pgf...
    \pgfmathparse{\answerkeytwo[\arabic{question}]}\pgfmathresult
\question testing ifthen...
    \ifthenelse{\equal 2 2}{itbetrue}{itbefalse}.
\question testing fp...
    \FPifeq 1 1 itbetrue\else itbefalse\fi.
\question testing arrayjob + ifthen... (should print itbetrue)
    %\ifthenelse{\equal 1 {
    % \answerkey(\arabic{question})
    % }}{itbetrue}{itbefalse}.
\question testing arrayjob + fp... (should print itbetrue)
    %\FPifeq 1 {
    % \answerkeyone(\arabic{question})
    % } itbetrue \else itbefalse \fi.
\question testing pgfmath + ifthen... (should print itbetrue)
    %\ifthenelse{\equal 1 {
    % \pgfmathparse{\answerkeytwo[\arabic{question}]}\pgfmathresult
    % }}{itbetrue}{itbefalse}.
\question testing pgfmath + fp... (should print itbetrue)
    %\FPifeq 1 {
    % \pgfmathparse{\answerkeytwo[\arabic{question}]}\pgfmathresult
    % } itbetrue\else itbefalse\fi.
\end{questions}
\end{document}

enter image description here

So how do I get \answerkey(\arabic{question})} to expand early enough for \ifthenelse{\equal{1}{\answerkey(\arabic{question})}}}{}{} to work?

1 Answer 1

1

IMHO you could do everything with pgfmath and simple \ifnum statements. (This does not mean the other options are bad.) I illustrate this with this example. The only thing I changed was to use \pgfmathtruncatemaccro to make sure I get an integer that one can test with \ifnum.

\documentclass{exam}
\usepackage{pgfmath}
\def\answerkeytwo{{{},4,3,2,1,1,1,1,1}} %pgfmath
\begin{document}
\begin{questions}
\question testing pgfmath + ifnum\dots (should print itbefalse)
    \pgfmathtruncatemacro{\itest}{\answerkeytwo[\number\value{question}]}
    \ifnum\itest=1
     itbetrue
    \else
     itbefalse
    \fi
\question testing pgfmath + ifnum\dots (should print itbefalse)
    \pgfmathtruncatemacro{\itest}{\answerkeytwo[\number\value{question}]}
    \ifnum\itest=1
     itbetrue
    \else
     itbefalse
    \fi
\question testingpgfmath + ifnum\dots (should print itbefalse)
    \pgfmathtruncatemacro{\itest}{\answerkeytwo[\number\value{question}]}
    \ifnum\itest=1
     itbetrue
    \else
     itbefalse
    \fi
\question testing pgfmath + ifnum\dots (should print itbetrue)
    \pgfmathtruncatemacro{\itest}{\answerkeytwo[\number\value{question}]}
    \ifnum\itest=1
     itbetrue
    \else
     itbefalse
    \fi
\end{questions}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .