TransWikia.com

Create Same Latex Environment with a Different Name

TeX - LaTeX Asked by Dr. Kareem Elgindy on May 31, 2021

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}

One Answer

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]
  {%
    defsolutiontitle{noindenttextbf{Solution #1:}enspace}
    begin{solution}%
  }
  {end{solution}}

newcounter{solution}
newenvironment{csolution}%
  {%
    refstepcounter{solution}%
    defsolutiontitle{noindenttextbf{Solution thesolution:}enspace}%
    begin{solution}%
  }
  {end{solution}}

begin{document}

begin{questions}
  question[2] bla ...
  begin{namedsolution}{Bla}
    bla...
  end{namedsolution}
  begin{namedsolution}{Baz}
    BAZldots
  end{namedsolution}

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

enter image description here

Answered by Skillmon on May 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP