TeX - LaTeX Asked on September 25, 2021
I’m using the exam
class, and I want to add answers. Right now the solutions are printet after the question, but before the next question. Ideally they would be presented at the end of a serie of questions. I’d like either
begin{numans} ... end{numans}
environment), or solution
environment, at the end of the document. Any ideas?
An MWE:
documentclass[a4paper]{exam}
usepackage[utf8]{inputenc}
printanswers
begin{document}
begin{questions}
addpoints question This is the first question
begin{solution}
This is the solution to question one.
end{solution}
addpoints question This is the second question
begin{solution}
This is the solution to questino two.
end{solution}
end{questions}
end{document}
Edit: the solution in Erik’s suggestion is possible, but I would like to be able to stick to my already exam
classed documents.
Presumably you want to type in the answer after each question (as you have them now), but to collect them and periodically display them in a group. This is just how endnotes work, so I'd use one of the endnotes packages: They all seem to support multiple sets of endnotes without any fuss. Here is a proof of concept using good old endnotes.sty
. You can easily tweak it to get the look you want, number answers automatically, etc.
documentclass[a4paper]{exam}
usepackage[utf8]{inputenc}
printanswers
usepackage{url}
usepackage{endnotes}
defenotesize{normalsize}
defmakeenmark{relax}
defnotesname{Answers}
defanswer#1{endnotetext{vspace*{-3.5ex}begin{solution}#1end{solution}unskip}}
deftheanswers{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}
Note that you can call theanswers
(i.e., theendnotes
) as often as you want, and it will output the accumulated answers and clear the endnote file.
Edit: Tweaked it with some hacks to reduce spacing between solutions. Really the space comes from the solution
environment so this should be fixed by modifying its behavior, but it's hard to track down just which macro is responsible for how much of the vertical space.
The endnote package enotez
is another option, but endnotes.sty
is simpler and hence easier to hack.
Correct answer by alexis on September 25, 2021
exam
supports redefining the solution environment. See page 66 of this document, section 8.3.3. Simply create an environment that has a counter:
documentclass[a4paper]{exam}
usepackage[utf8]{inputenc}
printanswers
begin{document}
begin{questions}
addpoints question This is the first question
addpoints question This is the second question
newcounter{solcounter}
newcommand{solcount}{stepcounter{solcounter}arabic{solcounter}. }
renewenvironment{TheSolution}{
solcount
}
begin{solution}
This is the solution to question one.
end{solution}
begin{solution}
This is the solution to question two.
end{solution}
end{questions}
end{document}
Answered by Texman on September 25, 2021
Here is a solution with answers package
documentclass[a4paper]{exam}
usepackage[utf8]{inputenc}
usepackage{answers}
Newassociation{sol}{Sol}{mycor}
renewcommand{Sollabel}[1]{textbf{Solution #1.}}
printanswers
begin{document}
Opensolutionfile{mycor}
ifprintanswers
renewenvironment{Sol}[1]{%
framednoindentSollabel{#1}}{endframed}%
else
answerfilestrue
renewcommand{Readsolutionfile}[1]{}fi
begin{questions}
addpoints
begin{question} This is the first question
begin{sol}
This is the solution to question one.
end{sol}
end{question}
addpoints
begin{question} This is the first question
end{question}
addpoints
begin{question} This is the first question
begin{sol}
This is the solution to question one.
end{sol}
end{question}
end{questions}
Closesolutionfile{mycor}
Readsolutionfile{mycor}
end{document}
if you want solutions just after question
usepackage[nosolutionfiles]{answers}
Answered by touhami on September 25, 2021
You can combine two of the above styles to make a simple and separate set of solutions that avoids the [Solutions:] presentation for each question and makes an answer bank at the end of the exam. This is great if you want to presumably make a list separate from the exam.
documentclass[a4paper]{exam}
usepackage[utf8]{inputenc}
printanswers
usepackage{url}
usepackage{endnotes}
newcounter{solcounter}
newcommand{solcount}{stepcounter{solcounter}arabic{solcounter}. }
renewenvironment{TheSolution}{
solcount
}
defenotesize{normalsize}
defmakeenmark{relax}
defnotesname{Answers}
defanswer#1{endnotetext{vspace*{1ex}begin{solution}#1end{solution}unskip}}
deftheanswers{theendnotes medskip}
begin{document}
begin{questions}
question Is this the first question?
begin{choices}
choice{yes}
choice{no}
choice{maybe}
choice{doesn't matter}
end{choices}
answer{ textbf{A)} because it is the first question.}
question This the second question
answer{This is the solution to question two.}
end{questions}
theanswers
end{document}
Answered by Serge Garcia on September 25, 2021
Similar to alexis' answer but instead of storing solutions with endnotes
package, expl3
is used, storing each solution in a seq
variable
documentclass[a4paper]{exam}
usepackage{xparse}
printanswers
ExplSyntaxOn
seq_new:N l_exam_endprint_seq
NewDocumentCommand WriteAnswer { +m } { seq_gput_right:Nn l_exam_endprint_seq { #1 } }
NewDocumentCommand EndPrintAnswers { } {
seq_map_inline:Nn l_exam_endprint_seq { begin{solution} ##1 end{solution} }
}
ExplSyntaxOff
begin{document}
begin{questions}
addpoints question This is question one.
WriteAnswer{This is the solution to question one.}
addpoints question This is question two.
WriteAnswer{This is the solution to question two.}
addpoints question This is the third question
WriteAnswer{This is the solution to question three.}
end{questions}
EndPrintAnswers
end{document}
If desired, numbering can be introduced by adding a counter (this will have issues if the questions have parts). The following should be added to the preamble
newcounter{solution}
stepcounter{solution}
renewcommand{solutiontitle}{noindenttextbf{Solution arabic{solution}:}enspacestepcounter{solution}}
As noted elsewhere in this question, adjusting the spacing between successive solution
environments may be desirable.
Answered by Dai Bowen on September 25, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP