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}









share|improve this question




















  • 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












  • 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 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

















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}









share|improve this question




















  • 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












  • 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 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















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}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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










  • 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 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
















  • 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












  • 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 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










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












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







share|improve this answer























    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%2f460264%2fglobal-counter-as-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
    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







    share|improve this answer



























      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







      share|improve this answer

























        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







        share|improve this answer














        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








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 days ago

























        answered 2 days ago









        moewe

        82.6k8105316




        82.6k8105316






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt