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}









share|improve this question
























  • Welcome to TeX.SE!
    – Kurt
    Nov 28 at 1:28















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}









share|improve this question
























  • Welcome to TeX.SE!
    – Kurt
    Nov 28 at 1:28













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}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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










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:



enter image description here



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%
}





share|improve this answer























  • This is excellent--many thanks!
    – AegisCruiser
    Nov 30 at 0:20











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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:



enter image description here



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%
}





share|improve this answer























  • This is excellent--many thanks!
    – AegisCruiser
    Nov 30 at 0:20















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:



enter image description here



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%
}





share|improve this answer























  • This is excellent--many thanks!
    – AegisCruiser
    Nov 30 at 0:20













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:



enter image description here



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%
}





share|improve this answer














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:



enter image description here



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%
}






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?