Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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 '12 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 '12 at 4:06

1 Answer 1

up vote 9 down vote accepted

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.

share|improve this answer
    
will it still print the answers when I use \printanswers? –  MaoYiyi Nov 11 '12 at 4:13
    
@MaoYiyi: Yes. Try it! –  Werner Nov 11 '12 at 4:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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