7

using exam class. I want the answerline on the same line as the question, to be used in mid-sentence actually.

I have this:

\documentclass[a4paper]{exam}
\begin{document}
\begin{questions}
\question Write hello  \answerline[hello] to your mother
\end{questions}
\end{document}
2
  • Is your problem that \answerline is placed a line below \question but you want both of them to be in the same line? (If so, please state it, if not, please tell us your problem to be solved.) (A \vspace{-2\baselineskip} before \answerline could help, but there is probably some better solution, if that is indeed the issue here.)
    – Stephen
    Nov 10, 2012 at 15:08
  • I've updated my answer to include your request, as well as provided some more modifications (using \answerline*) to remove the number typesetting.
    – Werner
    Nov 11, 2012 at 4:06

2 Answers 2

13

You can remove the separation by patching \answerline with xpatch:

enter image description here

\documentclass[a4paper]{exam}
\usepackage{xpatch}% http://ctan.org/pkg/etoolbox
\xpatchcmd{\answerline}% <cmd>
  {\par\nobreak\vskip\answerskip}% <search>
  {}% <replace>
  {}{}% <success><failure>
\begin{document}
\begin{questions}
\question Write hello  \answerline[hello]
\end{questions}
\end{document}

This will remove the skip for all \answerlines. Alternatively, on a case-by-case basis, you can set \answerskip carefully:

\question Write hello%
\setlength{\answerskip}{\dimexpr-\baselineskip-\parskip}
\answerline[hello]

Further modifications are also possible. The following MWE incorporates the above changes, but also provides a starred variant of \answerline that removes the number before the answer and allows for things to follow \answerline:

enter image description here

\documentclass[a4paper]{exam}
\usepackage{letltxmacro}% http://ctan.org/pkg/letltxmacro
\usepackage{xpatch}% http://ctan.org/pkg/etoolbox
\xpatchcmd{\answerline}% <cmd>
  {\par\nobreak\vskip\answerskip}% <search>
  {}% <replace>
  {}{}% <success><failure>
\xpatchcmd{\answerline}{\fi \par}{\fi}{}{}% Remove line break after \answerline
\makeatletter
\LetLtxMacro{\oldanswerline}{\answerline}
\RenewDocumentCommand{\answerline}{s o}{%
  \begingroup
  \IfBooleanTF{#1}
    {\def\@queslevel{\relax}}% \answerline*
    {}% \answerline
  \IfNoValueTF{#2}
    {\oldanswerline[{}]}% \answerline
    {\oldanswerline[#2]}% \answerline[..]
  \endgroup
}
\makeatother
\begin{document}
\begin{questions}
\question Write hello  \answerline[hello] to your mother
\question Write hello  \answerline*[hello] to your mother
\question Write hello  \answerline[hello] to your mother
\end{questions}
\end{document}

letltxmacro provides the means to store macros with optional arguments created using \newcommand.

5
  • will it still print the answers when I use \printanswers?
    – MaoYiyi
    Nov 11, 2012 at 4:13
  • 1
    @MaoYiyi: Yes. Try it!
    – Werner
    Nov 11, 2012 at 4:21
  • 1
    Nice solution. How do I turn this on in one part of my document and then back to the normal behavior for the rest of it?
    – Jeff
    Apr 18, 2015 at 20:45
  • 1
    @me: Put the usepackage commands in the top (before start document), then put the rest inside \begingroup and \par\endgroup. (But it adds an xparse/redefine-command warning).
    – Jeff
    Apr 18, 2015 at 20:53
  • @Werner, This still includes the question number (or letter) before the answerline and it's still not quite in line with the text. Can that be fixed?
    – Jeff
    Apr 18, 2015 at 21:04
5

You can use the \fillin environment instead. See 5.6 Fill in the blank questions of Using the exam document class

\documentclass[a4paper]{exam}
\begin{document}
\begin{questions}
\question Write hello \fillin[hello] to your mother
\end{questions}
\end{document}

You must log in to answer this question.

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