1

you posted a really nice solution to get a separate file with solutions here. I've tried for hours to modify your solution. Is it possible the get the number of the question to the solution?

This is what i get with your solution:

a) The solution is ...
b) The solution is ...
c) The solution is ...
a) The solution is ...
b) The solution is ...
a) The solution is ...

And this is what I want to have:

Exercise 1
a) The solution is ...
b) The solution is ...
c) The solution is ...

Exercise 2
a) The solution is ...
b) The solution is ...

Exercise 3
a) The solution is ...

I hobe, you can help. Thank you very much!

Matthias

1

New solution based on my own answer to Answers at the end of the exam document, this correctly handles when the questions have parts, subparts and subsubparts unlike my earlier answer (below).

Both labels and answer contents are stored in separate seq variables and then reprinted together iterating through both sequences at the same time. The counters are just printed and only repressed if the counter is set to zero, to format the solution number more elegantly would require conditionals based on whether they are zero.

\documentclass[a4paper]{exam}
\usepackage{xparse}
\printanswers

\ExplSyntaxOn

% Counters are not reset on \end{parts} so I add code to reset them
\tl_put_right:Nn \endparts { \setcounter{partno}{0} }
\tl_put_right:Nn \endsubparts { \setcounter{subpart}{0} }
\tl_put_right:Nn \endsubsubparts { \setcounter{subsubpart}{0} }

\seq_new:N \l_exam_endprint_seq
\seq_new:N \l_exam_endprint_labels_seq
\tl_new:N \l__exam_endprint_temp_tl

\NewDocumentCommand \WriteAnswer { +m } {
    \seq_gput_right:Nx \l_exam_endprint_labels_seq {\arabic{question}\alph{partno}\roman{subpart}\greeknum{subsubpart}} \seq_gput_right:Nn \l_exam_endprint_seq { #1 } 
}

\NewDocumentCommand \EndPrintAnswers { } {
    \seq_map_inline:Nn \l_exam_endprint_seq {
        \seq_pop_left:NN \l_exam_endprint_labels_seq \l__exam_endprint_temp_tl
        \renewcommand{\solutiontitle}{\noindent\textbf{Solution~\l__exam_endprint_temp_tl}:\enspace}
        \begin{solution} ##1 \end{solution}
    }
}

\ExplSyntaxOff

\begin{document}

\begin{questions}
\addpoints \question
\begin{parts}
    \part This is the first part of question one.
    \WriteAnswer{This is the solution to part one of question one.}
    \part This is the second part of question one.
    \begin{subparts}
        \subpart This is the first subpart of the second part of question one.
        \WriteAnswer{This is the solution to the first subpart of part two of question one.}
        \subpart
        \begin{subsubparts}
        \subsubpart This is the first subsubpart of the second subpart of the second part of question one.
        \WriteAnswer{This is the solution to the first subsubpart of the second subpart of the second part of question one.}
        \end{subsubparts}
    \end{subparts}
    \part This is the third part of question one
    \WriteAnswer{This is the solution to part three of question one.}
\end{parts} 
\addpoints \question This is the second question.
    \WriteAnswer{This is the solution to question two.}
\end{questions}

\EndPrintAnswers

\end{document}

enter image description here


This was my original answer which simply numbers each solution through a counter which is incremented each time a solution is printed. If parts are used then this will break numbering (i.e. Solution 1 could be for question 1a, solution 2 for question 1b, and solution 3 for question 2).

Using the MWE from the solution to Answers at the end of the exam document

Adding a new counter for each solution and stepping it with the \solutiontitle macro from the exam class as follows

\newcounter{solution}
\stepcounter{solution}

\renewcommand{\solutiontitle}{\noindent\textbf{Solution \arabic{solution}:}\enspace\stepcounter{solution}}

yields

enter image description here

From the full code

\documentclass[a4paper]{exam}
\usepackage[utf8]{inputenc}
\printanswers

\newcounter{solution}
\stepcounter{solution}

\renewcommand{\solutiontitle}{\noindent\textbf{Solution \arabic{solution}:}\enspace\stepcounter{solution}}

\usepackage{url}
\usepackage{endnotes}

\def\enotesize{\normalsize}
\def\makeenmark{\relax}
\def\notesname{Answers}
\def\answer#1{\endnotetext{\vspace*{-3.5ex}\begin{solution}#1\end{solution}\unskip}}
\def\theanswers{\theendnotes \medskip}

\begin{document}

\begin{questions}
\addpoints \question This is the first question

\answer{This is the solution to question one.}

\addpoints \question This the second question

\answer{This is the solution to question two.}

\end{questions}

\theanswers

\end{document}
  • Thanks for your quick answer. If I work with parts for one question, there should be "Solution 1a", "Solution 1b", "Solution 2", ... instead of "Solution 1", "Solution 2", "Solution 3", ... – Matthias Oct 20 '16 at 7:47
  • Sorry for the comments, the example I meant is in the answer below – Matthias Oct 20 '16 at 7:52
  • @Matthias please don't edit my answer but instead edit your question to include a minimal working example (MWE). I suggest that you use the code from Answers at the end of the exam document or one of the answers to it and edit it to include parts. I appreciate how my answer fails when parts are used, due to the nonexpanding nature of \endnotetextI'm not sure how easily this current answer can be hacked to deal with parts. I'll try and have a think about it. – Dai Bowen Oct 20 '16 at 12:56
  • Oh, I'm sorry! It's the first time I use a forum and I'm not familiar with the rules. Thank you very much for your work. – Matthias Oct 20 '16 at 13:55
  • @Matthias don't worry about it, you're right to point out that my answer hasn't fully met the specifications you gave. It's more helpful to add an MWE for which any answers should work in the question where everybody can see it rather than in an answer where it may be missed (and questions with MWEs are more likely to get answers and upvotes). – Dai Bowen Oct 20 '16 at 13:59

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.