# answerline command in exam class

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}
\usepackage{xpatch}% http://ctan.org/pkg/etoolbox
{\questionlabel}% <search>
{}{}% <success><failure>

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

\begin{document}
Hello!

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


• 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

Use the following definition for \solutionline:

\makeatletter
\newcommand{\solutionline}[3][blue]{%
\begingroup%
{\ans@l~}% <search>
{}{}% <success><failure>
\endgroup%
}
\makeatother


Complete MWE:

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

\makeatletter
\newcommand{\solutionline}[3][blue]{%
\begingroup%
{\ans@l~}% <search>
{}{}% <success><failure>
\endgroup%
}
\makeatother

\begin{document}
Hello!

\begin{questions}
\question Hello there! \answerline[Back at you!]

• 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