1

I am using exam class. I have an environment called solution which when I run Latex it appears as Solution with a capital letter S. Sometimes I'd like to show two different answers for the same question, so I want to call them Solution 1 and Solution 2. These two answers should have the same environment like Solution. How can I do this? BTW, I run the following code when I want to create a solution:

\begin{solution}
bla bla
\end{solution}

Here is a sample code:

\documentclass[a4paper, 12pt, addpoints, answers]{exam}
\usepackage{amssymb,amsmath,amsthm}

\begin{document}

\begin{questions}

\question[2] bla ...

\begin{solution} bla... \end{solution} \end{questions} \end{document}

Skillmon
  • 60,462

1 Answers1

2

exam uses the macro \solutiontitle to format its solution. We can locally redefine it to get numbers added to it:

Edit: as suggested by @AlanMunn I also added a variant which is automatically numbered and for which you can use LaTeX's label mechanism.

\documentclass[a4paper, 12pt, addpoints, answers]{exam}
\usepackage{amssymb,amsmath,amsthm}

\newenvironment{namedsolution}[1] {% \def\solutiontitle{\noindent\textbf{Solution #1:}\enspace} \begin{solution}% } {\end{solution}}

\newcounter{solution} \newenvironment{csolution}% {% \refstepcounter{solution}% \def\solutiontitle{\noindent\textbf{Solution \thesolution:}\enspace}% \begin{solution}% } {\end{solution}}

\begin{document}

\begin{questions} \question[2] bla ... \begin{namedsolution}{Bla} bla... \end{namedsolution} \begin{namedsolution}{Baz} BAZ\ldots \end{namedsolution}

\question[3] Foo \ldots \begin{csolution}\label{sol:bar} Bar\ldots \end{csolution} \begin{csolution} Similar to solution \ref{sol:bar}, also baz\ldots \end{csolution} \end{questions} \end{document}

enter image description here

Skillmon
  • 60,462
  • Works like a charm! Great answer. – Dr. Kareem Elgindy Jan 30 '21 at 14:40
  • Possibly more general would be to create a counter (reset every \question) and eliminate the need for an argument to the macro. This would have the added advantage of allowing \ref to the solutions in any discussion of them. – Alan Munn Jan 30 '21 at 17:22
  • @AlanMunn yes, that's true and would be better if the solutions should always be numbered, I'll add some code. – Skillmon Jan 30 '21 at 17:36