Global counter as argument
up vote
2
down vote
favorite
I am in a need of a global counter which I can be used as argument to a method. When it is used as an argument, I just want the current value of it... so what the method receives is a number, not the counter. Hope I am clear.
Do you have any advice on how to create such a setup?
Here is a example showing the problem I have where the counter is increased unintented:
documentclass{article}
usepackage{xparse}
newcounter{myCounter}
NewDocumentCommand{GetUinuqieID}{}{%
stepcounter{myCounter}themyCounter
}
makeatletter%
% Arguments:
% 1) The text
% 2) The label
newcommand*{textlabel}[2]{%
edef@currentlabel{#1}% Set target label
#1phantomsectionlabel{#2}% Print and store label
}
makeatother
begin{document}
textlabel{GetUinuqieID}{DDD} % prints 1, expected
ref{DDD} % prints 2, expected 1
textlabel{GetUinuqieID}{FFF}
ref{FFF}
textlabel{GetUinuqieID}{GGG}
ref{GGG}
end{document}
counters
|
show 2 more comments
up vote
2
down vote
favorite
I am in a need of a global counter which I can be used as argument to a method. When it is used as an argument, I just want the current value of it... so what the method receives is a number, not the counter. Hope I am clear.
Do you have any advice on how to create such a setup?
Here is a example showing the problem I have where the counter is increased unintented:
documentclass{article}
usepackage{xparse}
newcounter{myCounter}
NewDocumentCommand{GetUinuqieID}{}{%
stepcounter{myCounter}themyCounter
}
makeatletter%
% Arguments:
% 1) The text
% 2) The label
newcommand*{textlabel}[2]{%
edef@currentlabel{#1}% Set target label
#1phantomsectionlabel{#2}% Print and store label
}
makeatother
begin{document}
textlabel{GetUinuqieID}{DDD} % prints 1, expected
ref{DDD} % prints 2, expected 1
textlabel{GetUinuqieID}{FFF}
ref{FFF}
textlabel{GetUinuqieID}{GGG}
ref{GGG}
end{document}
counters
1
your question is not clear at all (an example document would, as always, make it clearer), but if you have a latex counter such assection
(all latex counters are global) then the numeric value isvalue{section}
– David Carlisle
2 days ago
Note however that you can't change the counter's value inside of your macro if you hand in the value of that counter and not the name.
– Skillmon
2 days ago
Okay, I will try aiain. Just a sec.
– 7heViking
2 days ago
@DavidCarlisle I have added an example
– 7heViking
2 days ago
1
If you want to be able to refer to the value ofmyCounter
withlabel
andref
you should probably be usingrefstepcounter
instead ofstepcounter
. In that case you can get rid ofphantomsection
.
– moewe
2 days ago
|
show 2 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am in a need of a global counter which I can be used as argument to a method. When it is used as an argument, I just want the current value of it... so what the method receives is a number, not the counter. Hope I am clear.
Do you have any advice on how to create such a setup?
Here is a example showing the problem I have where the counter is increased unintented:
documentclass{article}
usepackage{xparse}
newcounter{myCounter}
NewDocumentCommand{GetUinuqieID}{}{%
stepcounter{myCounter}themyCounter
}
makeatletter%
% Arguments:
% 1) The text
% 2) The label
newcommand*{textlabel}[2]{%
edef@currentlabel{#1}% Set target label
#1phantomsectionlabel{#2}% Print and store label
}
makeatother
begin{document}
textlabel{GetUinuqieID}{DDD} % prints 1, expected
ref{DDD} % prints 2, expected 1
textlabel{GetUinuqieID}{FFF}
ref{FFF}
textlabel{GetUinuqieID}{GGG}
ref{GGG}
end{document}
counters
I am in a need of a global counter which I can be used as argument to a method. When it is used as an argument, I just want the current value of it... so what the method receives is a number, not the counter. Hope I am clear.
Do you have any advice on how to create such a setup?
Here is a example showing the problem I have where the counter is increased unintented:
documentclass{article}
usepackage{xparse}
newcounter{myCounter}
NewDocumentCommand{GetUinuqieID}{}{%
stepcounter{myCounter}themyCounter
}
makeatletter%
% Arguments:
% 1) The text
% 2) The label
newcommand*{textlabel}[2]{%
edef@currentlabel{#1}% Set target label
#1phantomsectionlabel{#2}% Print and store label
}
makeatother
begin{document}
textlabel{GetUinuqieID}{DDD} % prints 1, expected
ref{DDD} % prints 2, expected 1
textlabel{GetUinuqieID}{FFF}
ref{FFF}
textlabel{GetUinuqieID}{GGG}
ref{GGG}
end{document}
counters
counters
edited 2 days ago
asked 2 days ago
7heViking
576311
576311
1
your question is not clear at all (an example document would, as always, make it clearer), but if you have a latex counter such assection
(all latex counters are global) then the numeric value isvalue{section}
– David Carlisle
2 days ago
Note however that you can't change the counter's value inside of your macro if you hand in the value of that counter and not the name.
– Skillmon
2 days ago
Okay, I will try aiain. Just a sec.
– 7heViking
2 days ago
@DavidCarlisle I have added an example
– 7heViking
2 days ago
1
If you want to be able to refer to the value ofmyCounter
withlabel
andref
you should probably be usingrefstepcounter
instead ofstepcounter
. In that case you can get rid ofphantomsection
.
– moewe
2 days ago
|
show 2 more comments
1
your question is not clear at all (an example document would, as always, make it clearer), but if you have a latex counter such assection
(all latex counters are global) then the numeric value isvalue{section}
– David Carlisle
2 days ago
Note however that you can't change the counter's value inside of your macro if you hand in the value of that counter and not the name.
– Skillmon
2 days ago
Okay, I will try aiain. Just a sec.
– 7heViking
2 days ago
@DavidCarlisle I have added an example
– 7heViking
2 days ago
1
If you want to be able to refer to the value ofmyCounter
withlabel
andref
you should probably be usingrefstepcounter
instead ofstepcounter
. In that case you can get rid ofphantomsection
.
– moewe
2 days ago
1
1
your question is not clear at all (an example document would, as always, make it clearer), but if you have a latex counter such as
section
(all latex counters are global) then the numeric value is value{section}
– David Carlisle
2 days ago
your question is not clear at all (an example document would, as always, make it clearer), but if you have a latex counter such as
section
(all latex counters are global) then the numeric value is value{section}
– David Carlisle
2 days ago
Note however that you can't change the counter's value inside of your macro if you hand in the value of that counter and not the name.
– Skillmon
2 days ago
Note however that you can't change the counter's value inside of your macro if you hand in the value of that counter and not the name.
– Skillmon
2 days ago
Okay, I will try aiain. Just a sec.
– 7heViking
2 days ago
Okay, I will try aiain. Just a sec.
– 7heViking
2 days ago
@DavidCarlisle I have added an example
– 7heViking
2 days ago
@DavidCarlisle I have added an example
– 7heViking
2 days ago
1
1
If you want to be able to refer to the value of
myCounter
with label
and ref
you should probably be using refstepcounter
instead of stepcounter
. In that case you can get rid of phantomsection
.– moewe
2 days ago
If you want to be able to refer to the value of
myCounter
with label
and ref
you should probably be using refstepcounter
instead of stepcounter
. In that case you can get rid of phantomsection
.– moewe
2 days ago
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The usual way to be able to refer back to counters is by using refstepcounter
instead of counter
, then the next label
will automatically pick up the right value.
Something like
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
newcounter{myCounter}
NewDocumentCommand{GetUinuqieID}{}{%
refstepcounter{myCounter}themyCounter
}
makeatletter%
% Arguments:
% 1) The text
% 2) The label
newcommand*{textlabel}[2]{%
#1label{#2}% Print and store label
}
makeatother
begin{document}
textlabel{GetUinuqieID}{DDD} % prints 1, expected
ref{DDD} % prints 2, expected 1
textlabel{GetUinuqieID}{FFF}
ref{FFF}
textlabel{GetUinuqieID}{GGG}
ref{GGG}
end{document}
should work and gives
1 1 2 2 3 3
as expected.
Depending on the intended use case, this could probably be simplified a bit.
Indeed the problem with the code in the question was (amongst others) that GetUinuqieID
is a robust command, which means that it did not expand to a number in edef@currentlabel
. Instead the label was made out to GetUinuqieID
and that command was only expanded when the label was printed in ref
, which meant that at that point the counter would be incremented again, leading to
1 2 3 4 5 6
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The usual way to be able to refer back to counters is by using refstepcounter
instead of counter
, then the next label
will automatically pick up the right value.
Something like
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
newcounter{myCounter}
NewDocumentCommand{GetUinuqieID}{}{%
refstepcounter{myCounter}themyCounter
}
makeatletter%
% Arguments:
% 1) The text
% 2) The label
newcommand*{textlabel}[2]{%
#1label{#2}% Print and store label
}
makeatother
begin{document}
textlabel{GetUinuqieID}{DDD} % prints 1, expected
ref{DDD} % prints 2, expected 1
textlabel{GetUinuqieID}{FFF}
ref{FFF}
textlabel{GetUinuqieID}{GGG}
ref{GGG}
end{document}
should work and gives
1 1 2 2 3 3
as expected.
Depending on the intended use case, this could probably be simplified a bit.
Indeed the problem with the code in the question was (amongst others) that GetUinuqieID
is a robust command, which means that it did not expand to a number in edef@currentlabel
. Instead the label was made out to GetUinuqieID
and that command was only expanded when the label was printed in ref
, which meant that at that point the counter would be incremented again, leading to
1 2 3 4 5 6
add a comment |
up vote
1
down vote
accepted
The usual way to be able to refer back to counters is by using refstepcounter
instead of counter
, then the next label
will automatically pick up the right value.
Something like
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
newcounter{myCounter}
NewDocumentCommand{GetUinuqieID}{}{%
refstepcounter{myCounter}themyCounter
}
makeatletter%
% Arguments:
% 1) The text
% 2) The label
newcommand*{textlabel}[2]{%
#1label{#2}% Print and store label
}
makeatother
begin{document}
textlabel{GetUinuqieID}{DDD} % prints 1, expected
ref{DDD} % prints 2, expected 1
textlabel{GetUinuqieID}{FFF}
ref{FFF}
textlabel{GetUinuqieID}{GGG}
ref{GGG}
end{document}
should work and gives
1 1 2 2 3 3
as expected.
Depending on the intended use case, this could probably be simplified a bit.
Indeed the problem with the code in the question was (amongst others) that GetUinuqieID
is a robust command, which means that it did not expand to a number in edef@currentlabel
. Instead the label was made out to GetUinuqieID
and that command was only expanded when the label was printed in ref
, which meant that at that point the counter would be incremented again, leading to
1 2 3 4 5 6
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The usual way to be able to refer back to counters is by using refstepcounter
instead of counter
, then the next label
will automatically pick up the right value.
Something like
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
newcounter{myCounter}
NewDocumentCommand{GetUinuqieID}{}{%
refstepcounter{myCounter}themyCounter
}
makeatletter%
% Arguments:
% 1) The text
% 2) The label
newcommand*{textlabel}[2]{%
#1label{#2}% Print and store label
}
makeatother
begin{document}
textlabel{GetUinuqieID}{DDD} % prints 1, expected
ref{DDD} % prints 2, expected 1
textlabel{GetUinuqieID}{FFF}
ref{FFF}
textlabel{GetUinuqieID}{GGG}
ref{GGG}
end{document}
should work and gives
1 1 2 2 3 3
as expected.
Depending on the intended use case, this could probably be simplified a bit.
Indeed the problem with the code in the question was (amongst others) that GetUinuqieID
is a robust command, which means that it did not expand to a number in edef@currentlabel
. Instead the label was made out to GetUinuqieID
and that command was only expanded when the label was printed in ref
, which meant that at that point the counter would be incremented again, leading to
1 2 3 4 5 6
The usual way to be able to refer back to counters is by using refstepcounter
instead of counter
, then the next label
will automatically pick up the right value.
Something like
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
newcounter{myCounter}
NewDocumentCommand{GetUinuqieID}{}{%
refstepcounter{myCounter}themyCounter
}
makeatletter%
% Arguments:
% 1) The text
% 2) The label
newcommand*{textlabel}[2]{%
#1label{#2}% Print and store label
}
makeatother
begin{document}
textlabel{GetUinuqieID}{DDD} % prints 1, expected
ref{DDD} % prints 2, expected 1
textlabel{GetUinuqieID}{FFF}
ref{FFF}
textlabel{GetUinuqieID}{GGG}
ref{GGG}
end{document}
should work and gives
1 1 2 2 3 3
as expected.
Depending on the intended use case, this could probably be simplified a bit.
Indeed the problem with the code in the question was (amongst others) that GetUinuqieID
is a robust command, which means that it did not expand to a number in edef@currentlabel
. Instead the label was made out to GetUinuqieID
and that command was only expanded when the label was printed in ref
, which meant that at that point the counter would be incremented again, leading to
1 2 3 4 5 6
edited 2 days ago
answered 2 days ago
moewe
82.6k8105316
82.6k8105316
add a comment |
add a comment |
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%2f460264%2fglobal-counter-as-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
1
your question is not clear at all (an example document would, as always, make it clearer), but if you have a latex counter such as
section
(all latex counters are global) then the numeric value isvalue{section}
– David Carlisle
2 days ago
Note however that you can't change the counter's value inside of your macro if you hand in the value of that counter and not the name.
– Skillmon
2 days ago
Okay, I will try aiain. Just a sec.
– 7heViking
2 days ago
@DavidCarlisle I have added an example
– 7heViking
2 days ago
1
If you want to be able to refer to the value of
myCounter
withlabel
andref
you should probably be usingrefstepcounter
instead ofstepcounter
. In that case you can get rid ofphantomsection
.– moewe
2 days ago