2

I am trying to modify \answerline to do two things:

  1. have answer labels which could be different for each question;
  2. have the answers (just the answers and not the labels) done in a color of one's choosing (There is \SolutionEmphasis{} which may be handy for doing this but I don't know how to exploit it to also achieve 1. above).

Ideally, the final macro, call it \solutionline should be able to respond to \solutionline{$H=$}{3} and produce:

H = _____3______

with 3 in a different color.

Following Steven B. Segletes' answer to this question, I tried the following but I now understand that this does not work.

\documentclass{exam}
\usepackage{xcolor}
\newcommand{\answerprompt}[1][\textbf{Answer:}]{#1}
\usepackage{xpatch}% http://ctan.org/pkg/etoolbox
\xpatchcmd{\answerline}% <cmd>
  {\questionlabel}% <search>
  {\answerprompt}% <replace>
  {}{}% <success><failure>

  \newcommand{\solutionline}[2]{\answerprompt{#1}\answerline[#2]}

  \usepackage{xpatch}% http://ctan.org/pkg/etoolbox
\xpatchcmd{\answerline}% <cmd>
  {\hfil #1}% <search>
  {\hfil \color{blue}{#1}}% <replace>
  {}{}% <success><failure>
 \printanswers

\begin{document}
Hello!

\begin{questions}
  \question Hello there! \answerline[Back at you!]
  \question There! \answerline[Hey!]
  \question Hey! \solutionline{H}{T} 
\end{questions}
\end{document}

Thank you in advance!

  • Just replace \newcommand{\solutionline}[2]{\answerprompt{#1}\answerline[#2]} with \newcommand{\solutionline}[2]{\renewcommand{\answerprompt}{#1}\answerline[#2]}. This will, however, also remove the bold style from the answer labels … – Jasper Habicht Sep 6 '17 at 5:31
  • @JasperHabicht Hah, \renewcommand it is! Thank you! (Also I am not sure what you mean by "remove the bold style from the answer labels, but could we not factor that into it as \newcommand{\solutionline}[2]{\renewcommand{\answerprompt}{\textbf{#‌​1}}\answerline[#2]}? (Could you please post your comment as an answer too?) – kan Sep 6 '17 at 11:59
  • But actually, I think, Arzigoglu’s answer is better, because: 1) the code is cleaner, 2) it patches \ans@l which actually stores the answer prompt, 3) it makes it possible to use different colors. So, why not use this answer? – Jasper Habicht Sep 6 '17 at 16:15
  • Now that I've corrected the answer, doesn't it do what you wanted? – Arzigoglu Sep 9 '17 at 11:06
  • Thank you for your answer! Sorry that I could not accept it earlier! – kan Sep 9 '17 at 17:13
2

Use the following definition for \solutionline:

\makeatletter
\newcommand{\solutionline}[3][blue]{%
\begingroup%
\xpatchcmd{\answerline}% <cmd>
  {\ans@l~}% <search>
  {\answerprompt[#2]~}% <replace>
  {}{}% <success><failure>
\answerline[\textcolor{#1}{#3}]%
\endgroup%
}
\makeatother

Complete MWE:

\documentclass{exam}
\usepackage{xcolor}
\usepackage{xpatch}% http://ctan.org/pkg/etoolbox

\newcommand{\answerprompt}[1][\textbf{Answer:}]{#1}

\makeatletter
\newcommand{\solutionline}[3][blue]{%
\begingroup%
\xpatchcmd{\answerline}% <cmd>
  {\ans@l~}% <search>
  {\answerprompt[#2]~}% <replace>
  {}{}% <success><failure>
\answerline[\textcolor{#1}{#3}]%
\endgroup%
}
\makeatother

 \printanswers

\begin{document}
Hello!

\begin{questions}
  \question Hello there! \answerline[Back at you!]
  \question There! \answerline[Hey!]
  \question Hey! \solutionline{H}{T}
  \question Hey! \solutionline[green]{H}{green}
  \question There! \answerline[Hey!]
\end{questions}
\end{document} 

enter image description here

| improve this answer | |
  • Hey, did you notice the "failure" printed after "Hey!" in 3 and 4? I have a feeling the fragment ##1 is not a part of the macro \answerline (for eg, try texdef -t latex -c exam answerline on your terminal). But I don't know how the rest of it works though. Confusing to me. – kan Sep 6 '17 at 11:50
  • I will experiment more and post this pm... – kan Sep 6 '17 at 12:03
  • @kan Yes, I was experimenting and I've posted the wrong solution :-( Now I've corrected it and should be OK. – Arzigoglu Sep 6 '17 at 15:49

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.