How do I reference a custom counter that shows the section number?custom counter and cross-referencingnameref...
How to say "In Japan, I want to ..."?
How can Schrödinger's cat be both dead and alive?
Why is it that I have to play this note on the piano as A sharp?
If every star in the universe except the Sun were destroyed, would we die?
When does order matter in probability?
Are programming languages necessary/useful for operations research practitioner?
Gap in tcolorbox after title
How invisible hand adjusts stock prices if company is listed on multiple exchanges, under multiple currencies, and one of the currencies plunges?
Walking on an infinite grid
Are there any space probes or landers which regained communication after being lost?
When calculating averages, why can we treat exploding die as if they're independent?
How strong is aircraft-grade spruce?
Contractor cut joist hangers to make them fit
LGPL HDL in larger FPGA design
What is the difference between tl_to_str:V and tl_to_str:N?
What makes an ending "happy"?
How to set any file manager in Linux to show the duration like the Length feature in Windows Explorer?
The meaning of "offing" in "an agreement in the offing"
Contour plot of a sequence of spheres with increasing radius
What makes things real?
When did computers stop checking memory on boot?
How do Scrum teams manage their dependencies on other teams?
After a few interviews, What should I do after told to wait?
Isn't that (two voices leaping to C like this) a breaking of the rules of four-part harmony?
How do I reference a custom counter that shows the section number?
custom counter and cross-referencingnameref : how to get counter's label value instead of section nameMaking an “auto-counter” command - test whether a reference marker already exists?Reference to section where is labelHow to customize cross-reference numbering of custom environments?Referencing Definition by Name and Optional ArgumentCleveref reference name depending on label instead of counter?Setting the enumi counter under custom enumeration with enumitemNumber only referenced equations for local equation numberingUltimate proof at the end/in appendix: different sections, with references to proof/theorem, restate, and synctex
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I am trying to use the proof environment that comes with the amsthm package. And I would like the proof environment to be numbered, such as the theorem environment for the sake of adding a label, so I have added a custom counter in the preamble and changed the embedded proofname in the proof environment. It all works well and displays my counters correctly in the text. However, when I try to reference my proofs, only the proof counter gets displayed instead of the full counter.
My code is below:
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{hyperref}
newcounter{proof}[section] % adds a new counter for the proof environment included in the amsthm package that restarts for every new section
renewcommand{theproof}{thesection.arabic{proof}} % adds the section number before your proof counters
renewcommand{proofname}{refstepcounter{proof}Proof theproof} % the proofname was embedded in your proof environment to output the italicized 'Proof', which is displayed at the beginning of every proof. This changes the proofname to print 'Proof' and theproof, which we defined as the section-proof counter above. This adds a counter to your proofs so you can hyperref them.
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
end{document}
But this is the result I get:

hyperref cross-referencing labels proof-package
add a comment |
I am trying to use the proof environment that comes with the amsthm package. And I would like the proof environment to be numbered, such as the theorem environment for the sake of adding a label, so I have added a custom counter in the preamble and changed the embedded proofname in the proof environment. It all works well and displays my counters correctly in the text. However, when I try to reference my proofs, only the proof counter gets displayed instead of the full counter.
My code is below:
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{hyperref}
newcounter{proof}[section] % adds a new counter for the proof environment included in the amsthm package that restarts for every new section
renewcommand{theproof}{thesection.arabic{proof}} % adds the section number before your proof counters
renewcommand{proofname}{refstepcounter{proof}Proof theproof} % the proofname was embedded in your proof environment to output the italicized 'Proof', which is displayed at the beginning of every proof. This changes the proofname to print 'Proof' and theproof, which we defined as the section-proof counter above. This adds a counter to your proofs so you can hyperref them.
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
end{document}
But this is the result I get:

hyperref cross-referencing labels proof-package
add a comment |
I am trying to use the proof environment that comes with the amsthm package. And I would like the proof environment to be numbered, such as the theorem environment for the sake of adding a label, so I have added a custom counter in the preamble and changed the embedded proofname in the proof environment. It all works well and displays my counters correctly in the text. However, when I try to reference my proofs, only the proof counter gets displayed instead of the full counter.
My code is below:
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{hyperref}
newcounter{proof}[section] % adds a new counter for the proof environment included in the amsthm package that restarts for every new section
renewcommand{theproof}{thesection.arabic{proof}} % adds the section number before your proof counters
renewcommand{proofname}{refstepcounter{proof}Proof theproof} % the proofname was embedded in your proof environment to output the italicized 'Proof', which is displayed at the beginning of every proof. This changes the proofname to print 'Proof' and theproof, which we defined as the section-proof counter above. This adds a counter to your proofs so you can hyperref them.
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
end{document}
But this is the result I get:

hyperref cross-referencing labels proof-package
I am trying to use the proof environment that comes with the amsthm package. And I would like the proof environment to be numbered, such as the theorem environment for the sake of adding a label, so I have added a custom counter in the preamble and changed the embedded proofname in the proof environment. It all works well and displays my counters correctly in the text. However, when I try to reference my proofs, only the proof counter gets displayed instead of the full counter.
My code is below:
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{hyperref}
newcounter{proof}[section] % adds a new counter for the proof environment included in the amsthm package that restarts for every new section
renewcommand{theproof}{thesection.arabic{proof}} % adds the section number before your proof counters
renewcommand{proofname}{refstepcounter{proof}Proof theproof} % the proofname was embedded in your proof environment to output the italicized 'Proof', which is displayed at the beginning of every proof. This changes the proofname to print 'Proof' and theproof, which we defined as the section-proof counter above. This adds a counter to your proofs so you can hyperref them.
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
end{document}
But this is the result I get:

hyperref cross-referencing labels proof-package
hyperref cross-referencing labels proof-package
edited 51 mins ago
Peter Mortensen
5643 silver badges7 bronze badges
5643 silver badges7 bronze badges
asked 13 hours ago
Carina ChenCarina Chen
363 bronze badges
363 bronze badges
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can manually set the label value by defining @currentlabel at the start of the proof environment.
Adding something to the start of a command can be done using the xpatch package which provides the xpretocmd (xpatch pre to command) macro. This works the same for environments, an environment like proof defines two commands, one that is executed with begin{proof} which is internally called proof, and one that is executed with end{proof} which is internally called endproof.
Patching the environment allows you to use the normal label command instead of a newly defined labelling command.
Because @currentlabel has an @ symbol in the name the redefinition should be enclosed in makeatletter and makeatother.
MWE:
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
makeatletter
xpretocmd{proof}{def@currentlabel{theproof}}{}{}
makeatother
newcounter{proof}[section] % adds a new counter for the proof environment included in the amsthm package that restarts for every new section
renewcommand{theproof}{thesection.arabic{proof}} % adds the section number before your proof counters
renewcommand{proofname}{refstepcounter{proof}Proof theproof} % the proofname was embedded in your proof environment to output the italicized 'Proof', which is displayed at the beginning of every proof. This changes the proofname to print 'Proof' and theproof, which we defined as the section-proof counter above. This adds a counter to your proofs so you can hyperref them.
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
end{document}
Result:

1
You could have just movedrefstepcounterto the beginning ofproof. BTW,proofnameis expanded inside inside a savebox, hence the local definition of@currentlabel. Interestingly, the etoolboxpatchcmddidn't work onproof.
– John Kormylo
5 hours ago
@JohnKormylopatchcmddoesn't work on commands that have an optional argument. Thexpatchpackage was written for the purpose in order to avoid things such asexpandafterpatchcmdcsnamestringproofendcsnamefor the particular case.
– egreg
29 mins ago
add a comment |
Here is an approach that works, but I am not sure what is going on behind the scenes. I adapted the accepted answer from this question, though copying it verbatim increased the counter proof once when the environment began, and once when the label was defined, resulting in offset numbering, so I just decreased the counter every time the label is called.
After you define the counter proof, add the the following line:
newcommand{prooflabel}[1]{addtocounter{proof}{-1}refstepcounter{proof}label{#1}}
Then whenever you have a proof, instead of label, use prooflabel as below.
section{One}
begin{proof}
prooflabel{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
This is the result:

Perhaps someone more knowledgeable than me can adjust the prooflabel and other definitions so that there isn't this back and forth couting, but it seems to work as you would like it to work.
add a comment |
Your refstepcounter is issued inside the optional argument to item and the corresponding value of @currentlabel gets forgotten as soon as item is processed, so you actually get references to the section number. You can check it by clicking on the reference.
A possible solution is to patch proof (the command executed upon entering the proof environment) to issue refstepcounter after trivlist and before item.
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
newcounter{proof}[section]
renewcommand{theproof}{thesection.arabic{proof}}
renewcommand{proofname}{Proof theproof}
xpatchcmd{proof}{trivlist}{trivlistrefstepcounter{proof}}{}{}
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
end{document}

You can also define a proof* environment for unnumbered proofs by saving a copy of proof before patching it.
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
% save proof
letunnumberedproofproof
newenvironment{proof*}
{renewcommandproofname{Proof}unnumberedproof}
{endproof}
newcounter{proof}[section]
renewcommand{theproof}{thesection.arabic{proof}}
renewcommand{proofname}{Proof theproof}
xpatchcmd{proof}{trivlist}{trivlistrefstepcounter{proof}}{}{}
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
begin{proof*}
An unnumbered proof.
end{proof*}
end{document}
add a comment |
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',
autoActivateHeartbeat: false,
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/4.0/"u003ecc by-sa 4.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
});
}
});
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%2f507464%2fhow-do-i-reference-a-custom-counter-that-shows-the-section-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can manually set the label value by defining @currentlabel at the start of the proof environment.
Adding something to the start of a command can be done using the xpatch package which provides the xpretocmd (xpatch pre to command) macro. This works the same for environments, an environment like proof defines two commands, one that is executed with begin{proof} which is internally called proof, and one that is executed with end{proof} which is internally called endproof.
Patching the environment allows you to use the normal label command instead of a newly defined labelling command.
Because @currentlabel has an @ symbol in the name the redefinition should be enclosed in makeatletter and makeatother.
MWE:
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
makeatletter
xpretocmd{proof}{def@currentlabel{theproof}}{}{}
makeatother
newcounter{proof}[section] % adds a new counter for the proof environment included in the amsthm package that restarts for every new section
renewcommand{theproof}{thesection.arabic{proof}} % adds the section number before your proof counters
renewcommand{proofname}{refstepcounter{proof}Proof theproof} % the proofname was embedded in your proof environment to output the italicized 'Proof', which is displayed at the beginning of every proof. This changes the proofname to print 'Proof' and theproof, which we defined as the section-proof counter above. This adds a counter to your proofs so you can hyperref them.
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
end{document}
Result:

1
You could have just movedrefstepcounterto the beginning ofproof. BTW,proofnameis expanded inside inside a savebox, hence the local definition of@currentlabel. Interestingly, the etoolboxpatchcmddidn't work onproof.
– John Kormylo
5 hours ago
@JohnKormylopatchcmddoesn't work on commands that have an optional argument. Thexpatchpackage was written for the purpose in order to avoid things such asexpandafterpatchcmdcsnamestringproofendcsnamefor the particular case.
– egreg
29 mins ago
add a comment |
You can manually set the label value by defining @currentlabel at the start of the proof environment.
Adding something to the start of a command can be done using the xpatch package which provides the xpretocmd (xpatch pre to command) macro. This works the same for environments, an environment like proof defines two commands, one that is executed with begin{proof} which is internally called proof, and one that is executed with end{proof} which is internally called endproof.
Patching the environment allows you to use the normal label command instead of a newly defined labelling command.
Because @currentlabel has an @ symbol in the name the redefinition should be enclosed in makeatletter and makeatother.
MWE:
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
makeatletter
xpretocmd{proof}{def@currentlabel{theproof}}{}{}
makeatother
newcounter{proof}[section] % adds a new counter for the proof environment included in the amsthm package that restarts for every new section
renewcommand{theproof}{thesection.arabic{proof}} % adds the section number before your proof counters
renewcommand{proofname}{refstepcounter{proof}Proof theproof} % the proofname was embedded in your proof environment to output the italicized 'Proof', which is displayed at the beginning of every proof. This changes the proofname to print 'Proof' and theproof, which we defined as the section-proof counter above. This adds a counter to your proofs so you can hyperref them.
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
end{document}
Result:

1
You could have just movedrefstepcounterto the beginning ofproof. BTW,proofnameis expanded inside inside a savebox, hence the local definition of@currentlabel. Interestingly, the etoolboxpatchcmddidn't work onproof.
– John Kormylo
5 hours ago
@JohnKormylopatchcmddoesn't work on commands that have an optional argument. Thexpatchpackage was written for the purpose in order to avoid things such asexpandafterpatchcmdcsnamestringproofendcsnamefor the particular case.
– egreg
29 mins ago
add a comment |
You can manually set the label value by defining @currentlabel at the start of the proof environment.
Adding something to the start of a command can be done using the xpatch package which provides the xpretocmd (xpatch pre to command) macro. This works the same for environments, an environment like proof defines two commands, one that is executed with begin{proof} which is internally called proof, and one that is executed with end{proof} which is internally called endproof.
Patching the environment allows you to use the normal label command instead of a newly defined labelling command.
Because @currentlabel has an @ symbol in the name the redefinition should be enclosed in makeatletter and makeatother.
MWE:
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
makeatletter
xpretocmd{proof}{def@currentlabel{theproof}}{}{}
makeatother
newcounter{proof}[section] % adds a new counter for the proof environment included in the amsthm package that restarts for every new section
renewcommand{theproof}{thesection.arabic{proof}} % adds the section number before your proof counters
renewcommand{proofname}{refstepcounter{proof}Proof theproof} % the proofname was embedded in your proof environment to output the italicized 'Proof', which is displayed at the beginning of every proof. This changes the proofname to print 'Proof' and theproof, which we defined as the section-proof counter above. This adds a counter to your proofs so you can hyperref them.
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
end{document}
Result:

You can manually set the label value by defining @currentlabel at the start of the proof environment.
Adding something to the start of a command can be done using the xpatch package which provides the xpretocmd (xpatch pre to command) macro. This works the same for environments, an environment like proof defines two commands, one that is executed with begin{proof} which is internally called proof, and one that is executed with end{proof} which is internally called endproof.
Patching the environment allows you to use the normal label command instead of a newly defined labelling command.
Because @currentlabel has an @ symbol in the name the redefinition should be enclosed in makeatletter and makeatother.
MWE:
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
makeatletter
xpretocmd{proof}{def@currentlabel{theproof}}{}{}
makeatother
newcounter{proof}[section] % adds a new counter for the proof environment included in the amsthm package that restarts for every new section
renewcommand{theproof}{thesection.arabic{proof}} % adds the section number before your proof counters
renewcommand{proofname}{refstepcounter{proof}Proof theproof} % the proofname was embedded in your proof environment to output the italicized 'Proof', which is displayed at the beginning of every proof. This changes the proofname to print 'Proof' and theproof, which we defined as the section-proof counter above. This adds a counter to your proofs so you can hyperref them.
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
end{document}
Result:

answered 12 hours ago
MarijnMarijn
12.3k1 gold badge7 silver badges41 bronze badges
12.3k1 gold badge7 silver badges41 bronze badges
1
You could have just movedrefstepcounterto the beginning ofproof. BTW,proofnameis expanded inside inside a savebox, hence the local definition of@currentlabel. Interestingly, the etoolboxpatchcmddidn't work onproof.
– John Kormylo
5 hours ago
@JohnKormylopatchcmddoesn't work on commands that have an optional argument. Thexpatchpackage was written for the purpose in order to avoid things such asexpandafterpatchcmdcsnamestringproofendcsnamefor the particular case.
– egreg
29 mins ago
add a comment |
1
You could have just movedrefstepcounterto the beginning ofproof. BTW,proofnameis expanded inside inside a savebox, hence the local definition of@currentlabel. Interestingly, the etoolboxpatchcmddidn't work onproof.
– John Kormylo
5 hours ago
@JohnKormylopatchcmddoesn't work on commands that have an optional argument. Thexpatchpackage was written for the purpose in order to avoid things such asexpandafterpatchcmdcsnamestringproofendcsnamefor the particular case.
– egreg
29 mins ago
1
1
You could have just moved
refstepcounter to the beginning of proof. BTW, proofname is expanded inside inside a savebox, hence the local definition of @currentlabel. Interestingly, the etoolbox patchcmd didn't work on proof.– John Kormylo
5 hours ago
You could have just moved
refstepcounter to the beginning of proof. BTW, proofname is expanded inside inside a savebox, hence the local definition of @currentlabel. Interestingly, the etoolbox patchcmd didn't work on proof.– John Kormylo
5 hours ago
@JohnKormylo
patchcmd doesn't work on commands that have an optional argument. The xpatch package was written for the purpose in order to avoid things such as expandafterpatchcmdcsnamestringproofendcsname for the particular case.– egreg
29 mins ago
@JohnKormylo
patchcmd doesn't work on commands that have an optional argument. The xpatch package was written for the purpose in order to avoid things such as expandafterpatchcmdcsnamestringproofendcsname for the particular case.– egreg
29 mins ago
add a comment |
Here is an approach that works, but I am not sure what is going on behind the scenes. I adapted the accepted answer from this question, though copying it verbatim increased the counter proof once when the environment began, and once when the label was defined, resulting in offset numbering, so I just decreased the counter every time the label is called.
After you define the counter proof, add the the following line:
newcommand{prooflabel}[1]{addtocounter{proof}{-1}refstepcounter{proof}label{#1}}
Then whenever you have a proof, instead of label, use prooflabel as below.
section{One}
begin{proof}
prooflabel{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
This is the result:

Perhaps someone more knowledgeable than me can adjust the prooflabel and other definitions so that there isn't this back and forth couting, but it seems to work as you would like it to work.
add a comment |
Here is an approach that works, but I am not sure what is going on behind the scenes. I adapted the accepted answer from this question, though copying it verbatim increased the counter proof once when the environment began, and once when the label was defined, resulting in offset numbering, so I just decreased the counter every time the label is called.
After you define the counter proof, add the the following line:
newcommand{prooflabel}[1]{addtocounter{proof}{-1}refstepcounter{proof}label{#1}}
Then whenever you have a proof, instead of label, use prooflabel as below.
section{One}
begin{proof}
prooflabel{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
This is the result:

Perhaps someone more knowledgeable than me can adjust the prooflabel and other definitions so that there isn't this back and forth couting, but it seems to work as you would like it to work.
add a comment |
Here is an approach that works, but I am not sure what is going on behind the scenes. I adapted the accepted answer from this question, though copying it verbatim increased the counter proof once when the environment began, and once when the label was defined, resulting in offset numbering, so I just decreased the counter every time the label is called.
After you define the counter proof, add the the following line:
newcommand{prooflabel}[1]{addtocounter{proof}{-1}refstepcounter{proof}label{#1}}
Then whenever you have a proof, instead of label, use prooflabel as below.
section{One}
begin{proof}
prooflabel{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
This is the result:

Perhaps someone more knowledgeable than me can adjust the prooflabel and other definitions so that there isn't this back and forth couting, but it seems to work as you would like it to work.
Here is an approach that works, but I am not sure what is going on behind the scenes. I adapted the accepted answer from this question, though copying it verbatim increased the counter proof once when the environment began, and once when the label was defined, resulting in offset numbering, so I just decreased the counter every time the label is called.
After you define the counter proof, add the the following line:
newcommand{prooflabel}[1]{addtocounter{proof}{-1}refstepcounter{proof}label{#1}}
Then whenever you have a proof, instead of label, use prooflabel as below.
section{One}
begin{proof}
prooflabel{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
This is the result:

Perhaps someone more knowledgeable than me can adjust the prooflabel and other definitions so that there isn't this back and forth couting, but it seems to work as you would like it to work.
answered 12 hours ago
Jānis LazovskisJānis Lazovskis
3,55912 silver badges32 bronze badges
3,55912 silver badges32 bronze badges
add a comment |
add a comment |
Your refstepcounter is issued inside the optional argument to item and the corresponding value of @currentlabel gets forgotten as soon as item is processed, so you actually get references to the section number. You can check it by clicking on the reference.
A possible solution is to patch proof (the command executed upon entering the proof environment) to issue refstepcounter after trivlist and before item.
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
newcounter{proof}[section]
renewcommand{theproof}{thesection.arabic{proof}}
renewcommand{proofname}{Proof theproof}
xpatchcmd{proof}{trivlist}{trivlistrefstepcounter{proof}}{}{}
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
end{document}

You can also define a proof* environment for unnumbered proofs by saving a copy of proof before patching it.
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
% save proof
letunnumberedproofproof
newenvironment{proof*}
{renewcommandproofname{Proof}unnumberedproof}
{endproof}
newcounter{proof}[section]
renewcommand{theproof}{thesection.arabic{proof}}
renewcommand{proofname}{Proof theproof}
xpatchcmd{proof}{trivlist}{trivlistrefstepcounter{proof}}{}{}
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
begin{proof*}
An unnumbered proof.
end{proof*}
end{document}
add a comment |
Your refstepcounter is issued inside the optional argument to item and the corresponding value of @currentlabel gets forgotten as soon as item is processed, so you actually get references to the section number. You can check it by clicking on the reference.
A possible solution is to patch proof (the command executed upon entering the proof environment) to issue refstepcounter after trivlist and before item.
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
newcounter{proof}[section]
renewcommand{theproof}{thesection.arabic{proof}}
renewcommand{proofname}{Proof theproof}
xpatchcmd{proof}{trivlist}{trivlistrefstepcounter{proof}}{}{}
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
end{document}

You can also define a proof* environment for unnumbered proofs by saving a copy of proof before patching it.
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
% save proof
letunnumberedproofproof
newenvironment{proof*}
{renewcommandproofname{Proof}unnumberedproof}
{endproof}
newcounter{proof}[section]
renewcommand{theproof}{thesection.arabic{proof}}
renewcommand{proofname}{Proof theproof}
xpatchcmd{proof}{trivlist}{trivlistrefstepcounter{proof}}{}{}
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
begin{proof*}
An unnumbered proof.
end{proof*}
end{document}
add a comment |
Your refstepcounter is issued inside the optional argument to item and the corresponding value of @currentlabel gets forgotten as soon as item is processed, so you actually get references to the section number. You can check it by clicking on the reference.
A possible solution is to patch proof (the command executed upon entering the proof environment) to issue refstepcounter after trivlist and before item.
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
newcounter{proof}[section]
renewcommand{theproof}{thesection.arabic{proof}}
renewcommand{proofname}{Proof theproof}
xpatchcmd{proof}{trivlist}{trivlistrefstepcounter{proof}}{}{}
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
end{document}

You can also define a proof* environment for unnumbered proofs by saving a copy of proof before patching it.
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
% save proof
letunnumberedproofproof
newenvironment{proof*}
{renewcommandproofname{Proof}unnumberedproof}
{endproof}
newcounter{proof}[section]
renewcommand{theproof}{thesection.arabic{proof}}
renewcommand{proofname}{Proof theproof}
xpatchcmd{proof}{trivlist}{trivlistrefstepcounter{proof}}{}{}
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
begin{proof*}
An unnumbered proof.
end{proof*}
end{document}
Your refstepcounter is issued inside the optional argument to item and the corresponding value of @currentlabel gets forgotten as soon as item is processed, so you actually get references to the section number. You can check it by clicking on the reference.
A possible solution is to patch proof (the command executed upon entering the proof environment) to issue refstepcounter after trivlist and before item.
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
newcounter{proof}[section]
renewcommand{theproof}{thesection.arabic{proof}}
renewcommand{proofname}{Proof theproof}
xpatchcmd{proof}{trivlist}{trivlistrefstepcounter{proof}}{}{}
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
end{document}

You can also define a proof* environment for unnumbered proofs by saving a copy of proof before patching it.
documentclass[12pt,letterpaper]{article}
usepackage{amsthm}
usepackage{xpatch}
usepackage{hyperref}
% save proof
letunnumberedproofproof
newenvironment{proof*}
{renewcommandproofname{Proof}unnumberedproof}
{endproof}
newcounter{proof}[section]
renewcommand{theproof}{thesection.arabic{proof}}
renewcommand{proofname}{Proof theproof}
xpatchcmd{proof}{trivlist}{trivlistrefstepcounter{proof}}{}{}
newtheorem{definition}{Definition}[section]
begin{document}
section{One}
begin{proof}
label{p1}
this is proof one.
end{proof}
This should reference proof ref{p1}, which should display as proof 1.1.
begin{definition}
label{d1}
this is definition one.
end{definition}
As we can see, this is not a problem when I reference definition ref{d1}.
section{Two}
begin{proof}
label{p2}
this is proof two.
end{proof}
This should reference proof ref{p2}, which should display as proof 2.1.
begin{definition}
label{d2}
this is definition two.
end{definition}
As we can see, this is not a problem when I reference definition ref{d2}.
begin{proof*}
An unnumbered proof.
end{proof*}
end{document}
edited 30 mins ago
answered 37 mins ago
egregegreg
767k91 gold badges2002 silver badges3358 bronze badges
767k91 gold badges2002 silver badges3358 bronze badges
add a comment |
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.
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%2f507464%2fhow-do-i-reference-a-custom-counter-that-shows-the-section-number%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