It occurred to me that one could use a savebox to collect solutions and preserve the formatting. However the formatting was not appropriate for a separate list.
\documentclass[12pt,a4paper,addpoints,answers]{exam}
\usepackage{showframe}% MWE only
\usepackage{blindtext}% MWE only
\newsavebox{\mybox}
\newcommand{\mysolution}[1]{\global\setbox\mybox=\vbox{\unvbox\mybox\medskip
\begin{solution}#1\end{solution}}}
\begin{document}
\begin{questions}
\question
Write an algebraic expression for "a number minus 3".
\mysolution{$x-3$}
\question
Write an algebraic expression for " a number plus 5".
\mysolution{\blindtext}
\end{questions}
\begin{flushleft}
Solutions
\unvbox\mybox% box is now empty
\end{flushleft}
\end{document}
My next attempt tried to emulate the question formatting.
\documentclass[12pt,a4paper,addpoints,answers]{exam}
\usepackage{showframe}% MWE only
\usepackage{blindtext}% MWE only
\newsavebox{\mybox}
\newcommand{\mysolution}[1]{\global\setbox\mybox=\vbox{\unvbox\mybox\medskip
\makebox[\labelwidth][r]{\questionlabel}\hskip\labelsep
\parbox[t]{\linewidth}{#1}\par}}
\begin{document}
\begin{questions}
\question
Write an algebraic expression for "a number minus 3".
\mysolution{$x-3$}
\question
Write an algebraic expression for " a number plus 5".
\mysolution{\blindtext}
\end{questions}
\begin{flushleft}
Solutions
\unvbox\mybox% box is now empty
\end{flushleft}
\end{document}
This version automatically prints the solution at the end of each layer. I only implemented questions and parts, but extending it is straightforward.
\documentclass[12pt,a4paper,addpoints,answers]{exam}
\usepackage{xpatch}
\usepackage{showframe}% MWE only
\usepackage{blindtext}% MWE only
\newsavebox{\mybox}
\makeatletter
\newcommand{\mysolution}[1]{\setbox\mybox=\vbox{\unvbox\mybox\medskip
\leftskip=\@totalleftmargin
\noindent\llap{\makebox[\labelwidth][r]{\mylabel}\hskip\labelsep}%
#1\par}}
\newcommand{\printmybox}{\ifprintanswers
\ifvoid\mybox
\else
\uplevel{\textbf{Solutions}}\par
\unvbox\mybox
\fi
\fi}
\newcommand{\emptybox}{\setbox\mybox=\copy\voidb@x}
\makeatother
% modify environments
\xapptocmd{\questions}{\emptybox
\let\mylabel=\questionlabel}{}{appto questions failed}
\xpretocmd{\endquestions}{\printmybox}{}{preto endquestions failed}
\xapptocmd{\parts}{\emptybox
\let\mylabel=\partlabel}{}{appto parts failed}
\xpretocmd{\endparts}{\printmybox}{}{preto endparts failed}
\begin{document}
\begin{questions}
\question
Write an algebraic expression for "a number minus 3".
\mysolution{$x-3$}
\question
Write an algebraic expression for " a number plus 5".
\mysolution{\blindtext}
\question
\begin{parts}
\part
Write an algebraic expression for "a number minus 3".
\mysolution{$x-3$}
\part
Write an algebraic expression for " a number plus 5".
\mysolution{\blindtext}
\end{parts}
\end{questions}
\end{document}
This version reproduces the question labels in their entirety. The purpose of the \prelabel
was to add 3.
in front of (a)
.
\documentclass[12pt,a4paper,addpoints,answers]{exam}
\usepackage{xpatch}
\usepackage{showframe}% MWE only
\usepackage{blindtext}% MWE only
\newsavebox{\mybox}
\newsavebox{\prelabel}% previous label
\makeatletter
\newcommand{\mysolution}[1]{\global\setbox\mybox=\vbox{\unvbox\mybox\medskip
\leftskip=\@totalleftmargin
\noindent\llap{\box\prelabel\makebox[\labelwidth][r]{\mylabel}\hskip\labelsep}%
#1\par}}
\makeatother
\newcommand{\printmybox}{\ifprintanswers
\ifvoid\mybox
\else
\fullwidth{\textbf{Solutions}}\par
\unvbox\mybox
\fi
\fi}
\newcommand{\newprelabel}{\setbox\prelabel=\hbox{% store current level label
\box\prelabel\makebox[\labelwidth][r]{\mylabel}\hskip\labelsep}}
% modify environments
\xapptocmd{\questions}{\let\mylabel=\questionlabel}{}{appto questions failed}
\xpretocmd{\parts}{\newprelabel}{}{preto parts failed}
\xapptocmd{\parts}{\let\mylabel=\partlabel}{}{appto parts failed}
\begin{document}
\begin{questions}
\question
Write an algebraic expression for ``a number minus 3''.
\mysolution{$x-3$}
\question
Write a paragraph in Latin.
\mysolution{\blindtext}
\question
\begin{parts}
\part
Write an algebraic expression for ``a number plus 5''.
\mysolution{$x+5$}
\part
Write a paragraph in Latin.
\mysolution{\blindtext}
\end{parts}
\printmybox
\end{questions}
\end{document}