Using exam question counter as an argument
up vote
2
down vote
favorite
I am trying to use the exam class to create a study guide for my students that has all of the answers at the end of the document.
I would like to have the answers automatically labeled by question number and part, but I'm hung up on how to pass the question and part counters to the command I'm using to construct the end notes.
All of my attempts to do this end up labeling all answers with the most recent question and part counter values. My problem seems very related to this question, but I'm unable to bridge the gap between them. I've tried reading through a few posts on expanding arguments, but I don't know enough of the deeper aspects of TeX to understand what's going on.
Here's a (relatively minimal) working example. Again, I would like to have output that numbers answers like "1: 5, 2(a): 9, 2(b): 30, 3: 2 apples".
documentclass{exam}
usepackage[utf8]{inputenc}
usepackage{endnotes}
defenotesize{normalsize}
defmakeenmark{relax}
defnotesname{normalsize Answers}
defanswer#1{endnotetext{#1\}}
deftheanswers{theendnotes}
begin{document}
noindenttextbf{Practice Problems}
begin{questions}
question How many sides does a pentagon have?
answer{thequestion: $5$}
question Calculate the following:
begin{parts}
part $2+7$
answer{thequestion(thepartno): $9$}
part $5cdot 6$
answer{thequestion(thepartno): $30$}
end{parts}
question If I have four apples and I eat two, how many are left?
answer{thequestion: $2$ apples}
end{questions}
theanswers
end{document}
macros counters expansion exam arguments
add a comment |
up vote
2
down vote
favorite
I am trying to use the exam class to create a study guide for my students that has all of the answers at the end of the document.
I would like to have the answers automatically labeled by question number and part, but I'm hung up on how to pass the question and part counters to the command I'm using to construct the end notes.
All of my attempts to do this end up labeling all answers with the most recent question and part counter values. My problem seems very related to this question, but I'm unable to bridge the gap between them. I've tried reading through a few posts on expanding arguments, but I don't know enough of the deeper aspects of TeX to understand what's going on.
Here's a (relatively minimal) working example. Again, I would like to have output that numbers answers like "1: 5, 2(a): 9, 2(b): 30, 3: 2 apples".
documentclass{exam}
usepackage[utf8]{inputenc}
usepackage{endnotes}
defenotesize{normalsize}
defmakeenmark{relax}
defnotesname{normalsize Answers}
defanswer#1{endnotetext{#1\}}
deftheanswers{theendnotes}
begin{document}
noindenttextbf{Practice Problems}
begin{questions}
question How many sides does a pentagon have?
answer{thequestion: $5$}
question Calculate the following:
begin{parts}
part $2+7$
answer{thequestion(thepartno): $9$}
part $5cdot 6$
answer{thequestion(thepartno): $30$}
end{parts}
question If I have four apples and I eat two, how many are left?
answer{thequestion: $2$ apples}
end{questions}
theanswers
end{document}
macros counters expansion exam arguments
Welcome to TeX.SE!
– Kurt
Nov 28 at 1:28
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am trying to use the exam class to create a study guide for my students that has all of the answers at the end of the document.
I would like to have the answers automatically labeled by question number and part, but I'm hung up on how to pass the question and part counters to the command I'm using to construct the end notes.
All of my attempts to do this end up labeling all answers with the most recent question and part counter values. My problem seems very related to this question, but I'm unable to bridge the gap between them. I've tried reading through a few posts on expanding arguments, but I don't know enough of the deeper aspects of TeX to understand what's going on.
Here's a (relatively minimal) working example. Again, I would like to have output that numbers answers like "1: 5, 2(a): 9, 2(b): 30, 3: 2 apples".
documentclass{exam}
usepackage[utf8]{inputenc}
usepackage{endnotes}
defenotesize{normalsize}
defmakeenmark{relax}
defnotesname{normalsize Answers}
defanswer#1{endnotetext{#1\}}
deftheanswers{theendnotes}
begin{document}
noindenttextbf{Practice Problems}
begin{questions}
question How many sides does a pentagon have?
answer{thequestion: $5$}
question Calculate the following:
begin{parts}
part $2+7$
answer{thequestion(thepartno): $9$}
part $5cdot 6$
answer{thequestion(thepartno): $30$}
end{parts}
question If I have four apples and I eat two, how many are left?
answer{thequestion: $2$ apples}
end{questions}
theanswers
end{document}
macros counters expansion exam arguments
I am trying to use the exam class to create a study guide for my students that has all of the answers at the end of the document.
I would like to have the answers automatically labeled by question number and part, but I'm hung up on how to pass the question and part counters to the command I'm using to construct the end notes.
All of my attempts to do this end up labeling all answers with the most recent question and part counter values. My problem seems very related to this question, but I'm unable to bridge the gap between them. I've tried reading through a few posts on expanding arguments, but I don't know enough of the deeper aspects of TeX to understand what's going on.
Here's a (relatively minimal) working example. Again, I would like to have output that numbers answers like "1: 5, 2(a): 9, 2(b): 30, 3: 2 apples".
documentclass{exam}
usepackage[utf8]{inputenc}
usepackage{endnotes}
defenotesize{normalsize}
defmakeenmark{relax}
defnotesname{normalsize Answers}
defanswer#1{endnotetext{#1\}}
deftheanswers{theendnotes}
begin{document}
noindenttextbf{Practice Problems}
begin{questions}
question How many sides does a pentagon have?
answer{thequestion: $5$}
question Calculate the following:
begin{parts}
part $2+7$
answer{thequestion(thepartno): $9$}
part $5cdot 6$
answer{thequestion(thepartno): $30$}
end{parts}
question If I have four apples and I eat two, how many are left?
answer{thequestion: $2$ apples}
end{questions}
theanswers
end{document}
macros counters expansion exam arguments
macros counters expansion exam arguments
edited Nov 28 at 21:40
asked Nov 28 at 1:22
AegisCruiser
1135
1135
Welcome to TeX.SE!
– Kurt
Nov 28 at 1:28
add a comment |
Welcome to TeX.SE!
– Kurt
Nov 28 at 1:28
Welcome to TeX.SE!
– Kurt
Nov 28 at 1:28
Welcome to TeX.SE!
– Kurt
Nov 28 at 1:28
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You're seeing this problem, as you may already understand, because you need to expand the values of thequestion
and thepartno
at the point of use. As you've written it, only the final value is used.
As expansion is one of the areas of TeX/LaTeX that offers the most complexity, I usually find that expl3
gives the most intuitive solution. In the following, I've rewritten your answer
macro to fully expand anything you pass it using the expl3
approach.
documentclass{exam}
usepackage[utf8]{inputenc}
usepackage{endnotes}
usepackage{xparse}
renewcommand{enotesize}{normalsize}
renewcommand{makeenmark}{relax}
defnotesname{normalsize Answers}
deftheanswers{theendnotes}
ExplSyntaxOn
cs_new:Nn aegis_answer:n
{
endnotetext{#1par}
}
cs_generate_variant:Nn aegis_answer:n {x}
NewDocumentCommand{answer}{m}
{
aegis_answer:x {#1}
}
ExplSyntaxOff
begin{document}
noindenttextbf{Practice Problems}
begin{questions}
question How many sides does a pentagon have?
answer{thequestion: $5$}
question Calculate the following:
begin{parts}
part $2+7$
answer{thequestion(thepartno): $9$}
part $5cdot 6$
answer{thequestion(thepartno): $30$}
end{parts}
question If I have four apples and I eat two, how many are left?
answer{thequestion: $2$ apples}
end{questions}
theanswers
end{document}
The result:
Update:
If you find expl3
off-putting, here's a method with edef
. Frankly, I find this much less transparent, but your mileage may vary:
newcommand{answer}[1]{%
begingroup
edefx{%
endgroup
noexpandendnotetext{#1par}}%
x%
}
This is excellent--many thanks!
– AegisCruiser
Nov 30 at 0:20
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You're seeing this problem, as you may already understand, because you need to expand the values of thequestion
and thepartno
at the point of use. As you've written it, only the final value is used.
As expansion is one of the areas of TeX/LaTeX that offers the most complexity, I usually find that expl3
gives the most intuitive solution. In the following, I've rewritten your answer
macro to fully expand anything you pass it using the expl3
approach.
documentclass{exam}
usepackage[utf8]{inputenc}
usepackage{endnotes}
usepackage{xparse}
renewcommand{enotesize}{normalsize}
renewcommand{makeenmark}{relax}
defnotesname{normalsize Answers}
deftheanswers{theendnotes}
ExplSyntaxOn
cs_new:Nn aegis_answer:n
{
endnotetext{#1par}
}
cs_generate_variant:Nn aegis_answer:n {x}
NewDocumentCommand{answer}{m}
{
aegis_answer:x {#1}
}
ExplSyntaxOff
begin{document}
noindenttextbf{Practice Problems}
begin{questions}
question How many sides does a pentagon have?
answer{thequestion: $5$}
question Calculate the following:
begin{parts}
part $2+7$
answer{thequestion(thepartno): $9$}
part $5cdot 6$
answer{thequestion(thepartno): $30$}
end{parts}
question If I have four apples and I eat two, how many are left?
answer{thequestion: $2$ apples}
end{questions}
theanswers
end{document}
The result:
Update:
If you find expl3
off-putting, here's a method with edef
. Frankly, I find this much less transparent, but your mileage may vary:
newcommand{answer}[1]{%
begingroup
edefx{%
endgroup
noexpandendnotetext{#1par}}%
x%
}
This is excellent--many thanks!
– AegisCruiser
Nov 30 at 0:20
add a comment |
up vote
2
down vote
accepted
You're seeing this problem, as you may already understand, because you need to expand the values of thequestion
and thepartno
at the point of use. As you've written it, only the final value is used.
As expansion is one of the areas of TeX/LaTeX that offers the most complexity, I usually find that expl3
gives the most intuitive solution. In the following, I've rewritten your answer
macro to fully expand anything you pass it using the expl3
approach.
documentclass{exam}
usepackage[utf8]{inputenc}
usepackage{endnotes}
usepackage{xparse}
renewcommand{enotesize}{normalsize}
renewcommand{makeenmark}{relax}
defnotesname{normalsize Answers}
deftheanswers{theendnotes}
ExplSyntaxOn
cs_new:Nn aegis_answer:n
{
endnotetext{#1par}
}
cs_generate_variant:Nn aegis_answer:n {x}
NewDocumentCommand{answer}{m}
{
aegis_answer:x {#1}
}
ExplSyntaxOff
begin{document}
noindenttextbf{Practice Problems}
begin{questions}
question How many sides does a pentagon have?
answer{thequestion: $5$}
question Calculate the following:
begin{parts}
part $2+7$
answer{thequestion(thepartno): $9$}
part $5cdot 6$
answer{thequestion(thepartno): $30$}
end{parts}
question If I have four apples and I eat two, how many are left?
answer{thequestion: $2$ apples}
end{questions}
theanswers
end{document}
The result:
Update:
If you find expl3
off-putting, here's a method with edef
. Frankly, I find this much less transparent, but your mileage may vary:
newcommand{answer}[1]{%
begingroup
edefx{%
endgroup
noexpandendnotetext{#1par}}%
x%
}
This is excellent--many thanks!
– AegisCruiser
Nov 30 at 0:20
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You're seeing this problem, as you may already understand, because you need to expand the values of thequestion
and thepartno
at the point of use. As you've written it, only the final value is used.
As expansion is one of the areas of TeX/LaTeX that offers the most complexity, I usually find that expl3
gives the most intuitive solution. In the following, I've rewritten your answer
macro to fully expand anything you pass it using the expl3
approach.
documentclass{exam}
usepackage[utf8]{inputenc}
usepackage{endnotes}
usepackage{xparse}
renewcommand{enotesize}{normalsize}
renewcommand{makeenmark}{relax}
defnotesname{normalsize Answers}
deftheanswers{theendnotes}
ExplSyntaxOn
cs_new:Nn aegis_answer:n
{
endnotetext{#1par}
}
cs_generate_variant:Nn aegis_answer:n {x}
NewDocumentCommand{answer}{m}
{
aegis_answer:x {#1}
}
ExplSyntaxOff
begin{document}
noindenttextbf{Practice Problems}
begin{questions}
question How many sides does a pentagon have?
answer{thequestion: $5$}
question Calculate the following:
begin{parts}
part $2+7$
answer{thequestion(thepartno): $9$}
part $5cdot 6$
answer{thequestion(thepartno): $30$}
end{parts}
question If I have four apples and I eat two, how many are left?
answer{thequestion: $2$ apples}
end{questions}
theanswers
end{document}
The result:
Update:
If you find expl3
off-putting, here's a method with edef
. Frankly, I find this much less transparent, but your mileage may vary:
newcommand{answer}[1]{%
begingroup
edefx{%
endgroup
noexpandendnotetext{#1par}}%
x%
}
You're seeing this problem, as you may already understand, because you need to expand the values of thequestion
and thepartno
at the point of use. As you've written it, only the final value is used.
As expansion is one of the areas of TeX/LaTeX that offers the most complexity, I usually find that expl3
gives the most intuitive solution. In the following, I've rewritten your answer
macro to fully expand anything you pass it using the expl3
approach.
documentclass{exam}
usepackage[utf8]{inputenc}
usepackage{endnotes}
usepackage{xparse}
renewcommand{enotesize}{normalsize}
renewcommand{makeenmark}{relax}
defnotesname{normalsize Answers}
deftheanswers{theendnotes}
ExplSyntaxOn
cs_new:Nn aegis_answer:n
{
endnotetext{#1par}
}
cs_generate_variant:Nn aegis_answer:n {x}
NewDocumentCommand{answer}{m}
{
aegis_answer:x {#1}
}
ExplSyntaxOff
begin{document}
noindenttextbf{Practice Problems}
begin{questions}
question How many sides does a pentagon have?
answer{thequestion: $5$}
question Calculate the following:
begin{parts}
part $2+7$
answer{thequestion(thepartno): $9$}
part $5cdot 6$
answer{thequestion(thepartno): $30$}
end{parts}
question If I have four apples and I eat two, how many are left?
answer{thequestion: $2$ apples}
end{questions}
theanswers
end{document}
The result:
Update:
If you find expl3
off-putting, here's a method with edef
. Frankly, I find this much less transparent, but your mileage may vary:
newcommand{answer}[1]{%
begingroup
edefx{%
endgroup
noexpandendnotetext{#1par}}%
x%
}
edited Nov 28 at 22:45
answered Nov 28 at 3:33
Karl Hagen
615416
615416
This is excellent--many thanks!
– AegisCruiser
Nov 30 at 0:20
add a comment |
This is excellent--many thanks!
– AegisCruiser
Nov 30 at 0:20
This is excellent--many thanks!
– AegisCruiser
Nov 30 at 0:20
This is excellent--many thanks!
– AegisCruiser
Nov 30 at 0:20
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f462096%2fusing-exam-question-counter-as-an-argument%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome to TeX.SE!
– Kurt
Nov 28 at 1:28