2

Here is a minimal example.

\documentclass[a4paper, addpoints]{exam}
\usepackage{etoolbox}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the accumulated answers text
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\answertext}{}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% hook to fillin command
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\renewcommand\fillin[1][{}]{%
% copy the contents of fillin definition from the exam class
% we try to append the content to the \answertext string
\xappto\answertext{\thequestion,#1\space}%
\def\fillin@ans{#1}%
\fillin@relay
} 
\makeatother


\begin{document}

\section{Questions}
\begin{questions}
\question First Quesiton \fillin[Answers1] is the color of my true love’s hair.
\question Second Question \fillin[Answers2] is the \fillin[Answers3].
\question Third Question  \fillin[Answers4] is OK.
\end{questions}


\section{Answers}
\ifprintanswers
    % do nothing
\else
    % this print the answertext
    \answertext
\fi

\end{document}

I have to hook into the \fillin command to accumulate the answers, and put them in the global variable \answertext.

But the result is looks like below:

fillin answers shows badly

I would like to see like below:

1,Answers1 2,Answers2;Answers3 3,Answers4

I mean it should only show the question number "2" once. Because one question could have many fillin answers. Also, I need a way to calculate the total fillin numbers. (That is, one fillin place has 2 points for the example)

2
  • Try to minimize your code, remove packages and macros which have no relation with your problem.
    – Salim Bou
    Commented Dec 31, 2016 at 17:41
  • Hi, thanks for the suggestion, I have simplify the test code, which only contains fillin command, all the other kinds of questions are removed.
    – ollydbg23
    Commented Jan 1, 2017 at 1:22

1 Answer 1

2

I added new counters fillno and lastquesno, to count the number of fillin's and to test if the current fillin is part of the same question that held the previous fillin. I also changed your renewed definition of \fillin to be

\makeatletter
\renewcommand\fillin[1][{}]{%
% copy the contents of fillin definition from the exam class
% we try to append the content to the \answertext string
%\xappto\answertext{\thequestion,#1\space}%
\appendanswertext{\thequestion}{#1}%
\setcounter{lastquesno}{\value{question}}%
\addtocounter{fillno}{1}%
\def\fillin@ans{#1}%
\fillin@relay
} 
\makeatother

\newcommand{\appendanswertext}[2]{%
  % #1 is \thequestion, #2 is the answer
  \ifnum \value{lastquesno} = #1\relax
    % It's another fillin for the same question:
    \xappto\answertext{;#2}%
  \else
    % First fillin for this question:
    \xappto\answertext{\space #1,#2}
  \fi
}

I also added the line There were \thefillno\ fillins. right after you called \answertext. It now seems to do what you requested.

0

You must log in to answer this question.

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