Loop over an optional argument












5















I'd like to use the command foo with an optional argument to step and print the counter value iterated according to the number passed.



So, for example, foo[5] should step the counter 5 times, one by one.



(!) Note that it is not the same as stepcounter{}{5}.



MWE



documentclass{report}

newcounter{mycount}
setcounter{mycount}{0}
newcommand{foo}{stepcounter{mycount}counter is textbf{themycount}}

begin{document}
first use: foo

second use: foo

now using with option to iterate: foo[2]

medskip

textit{desired output from line above should be:}

medskip

now using with option to iterate: counter is textbf{3,4}

end{document}


enter image description here










share|improve this question



























    5















    I'd like to use the command foo with an optional argument to step and print the counter value iterated according to the number passed.



    So, for example, foo[5] should step the counter 5 times, one by one.



    (!) Note that it is not the same as stepcounter{}{5}.



    MWE



    documentclass{report}

    newcounter{mycount}
    setcounter{mycount}{0}
    newcommand{foo}{stepcounter{mycount}counter is textbf{themycount}}

    begin{document}
    first use: foo

    second use: foo

    now using with option to iterate: foo[2]

    medskip

    textit{desired output from line above should be:}

    medskip

    now using with option to iterate: counter is textbf{3,4}

    end{document}


    enter image description here










    share|improve this question

























      5












      5








      5








      I'd like to use the command foo with an optional argument to step and print the counter value iterated according to the number passed.



      So, for example, foo[5] should step the counter 5 times, one by one.



      (!) Note that it is not the same as stepcounter{}{5}.



      MWE



      documentclass{report}

      newcounter{mycount}
      setcounter{mycount}{0}
      newcommand{foo}{stepcounter{mycount}counter is textbf{themycount}}

      begin{document}
      first use: foo

      second use: foo

      now using with option to iterate: foo[2]

      medskip

      textit{desired output from line above should be:}

      medskip

      now using with option to iterate: counter is textbf{3,4}

      end{document}


      enter image description here










      share|improve this question














      I'd like to use the command foo with an optional argument to step and print the counter value iterated according to the number passed.



      So, for example, foo[5] should step the counter 5 times, one by one.



      (!) Note that it is not the same as stepcounter{}{5}.



      MWE



      documentclass{report}

      newcounter{mycount}
      setcounter{mycount}{0}
      newcommand{foo}{stepcounter{mycount}counter is textbf{themycount}}

      begin{document}
      first use: foo

      second use: foo

      now using with option to iterate: foo[2]

      medskip

      textit{desired output from line above should be:}

      medskip

      now using with option to iterate: counter is textbf{3,4}

      end{document}


      enter image description here







      macros counters loops






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 5 hours ago









      SigurSigur

      24.5k355138




      24.5k355138






















          4 Answers
          4






          active

          oldest

          votes


















          5














          With expl3 it's just a few lines.



          documentclass{report}
          usepackage{xparse}

          newcounter{mycount}
          setcounter{mycount}{0}

          ExplSyntaxOn
          NewDocumentCommand{foo}{O{1}}
          {
          int_compare:nT { #1 > 0 }
          {
          int_compare:nTF { #1 = 1 } {counter~is} {counters~are}~
          stepcounter{mycount}
          textbf{themycount}
          prg_replicate:nn { #1 - 1 } {,stepcounter{mycount}textbf{themycount}}
          }
          }
          ExplSyntaxOff

          begin{document}
          first use: foo

          second use: foo

          now using with option to iterate: foo[2]

          now using with option to iterate: counters are textbf{3,4}

          foo[5]

          end{document}


          The trick is to do the first iteration and then proceed again adding first a comma.



          If the number passed is zero or less nothing happens.



          enter image description here






          share|improve this answer


























          • Exactly what I need.

            – Sigur
            5 hours ago











          • Do you think is it possible to change the text also if there is argument? I mean, to print counters are 5,6,7,8 for example.

            – Sigur
            5 hours ago






          • 1





            @Sigur Your wish is my command. Updated.

            – egreg
            5 hours ago











          • Oh, I think I should learn about NewDocumentCommand as soon as possible. Amazing and powerful.

            – Sigur
            5 hours ago



















          2














          Here is an approach using multido for looping and a delayed delimiter definition:



          enter image description here



          documentclass{article}

          usepackage{multido}

          newcounter{mycount}

          newcommand{foo}[1][1]{%
          counter%
          ifnum#1>1relax s areelse space isfi
          space
          % https://tex.stackexchange.com/a/89187/5764
          defitemdelim{unskipspacedefitemdelim{,space}}% Item delimiter delayed by one cycle
          {bfseriesmultido{i=1+1}{#1}{itemdelim stepcounter{mycount}themycount}}%
          }

          begin{document}

          first use: foo

          second use: foo

          now using with option to iterate: foo[2]

          medskip

          textit{desired output from line above should be:}

          medskip

          now using with option to iterate: counters are textbf{3, 4}

          end{document}





          share|improve this answer
























          • This solution is more understandable to me... but I had never done it by myself. Very nice.

            – Sigur
            5 hours ago



















          1














          With pgffor it is even less lines.



          documentclass{report}
          usepackage{pgffor}
          newcounter{mycount}
          setcounter{mycount}{0}
          newcommand{foo}[1][1]{counter is foreach X [count=Y] in
          {1,...,#1}{stepcounter{mycount}%
          textbf{themycount}ifnumY=#1%
          else%
          ,fi}}

          begin{document}
          first use: foo

          second use: foo

          now using with option to iterate: foo[2]

          end{document}


          enter image description here






          share|improve this answer


























          • @egreg You're right. Fixed it.

            – marmot
            5 hours ago





















          1














          Here is short code with multido, xparse and etoolbox:



          documentclass{report}
          usepackage{multido}
          usepackage{xparse, etoolbox}
          newcounter{mycount}
          setcounter{mycount}{0}
          NewDocumentCommand{foo}{O{1}}{counter is multido{i=1+1}{#1}{stepcounter{mycount}textbf{themycount}ifnumless{multidocount}{#1}{,,}{.}}}

          begin{document}

          first use: foo

          second use: foo

          now using with option to iterate: foo[5]

          end{document}


          enter image description here






          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',
            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/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%2f470788%2floop-over-an-optional-argument%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            With expl3 it's just a few lines.



            documentclass{report}
            usepackage{xparse}

            newcounter{mycount}
            setcounter{mycount}{0}

            ExplSyntaxOn
            NewDocumentCommand{foo}{O{1}}
            {
            int_compare:nT { #1 > 0 }
            {
            int_compare:nTF { #1 = 1 } {counter~is} {counters~are}~
            stepcounter{mycount}
            textbf{themycount}
            prg_replicate:nn { #1 - 1 } {,stepcounter{mycount}textbf{themycount}}
            }
            }
            ExplSyntaxOff

            begin{document}
            first use: foo

            second use: foo

            now using with option to iterate: foo[2]

            now using with option to iterate: counters are textbf{3,4}

            foo[5]

            end{document}


            The trick is to do the first iteration and then proceed again adding first a comma.



            If the number passed is zero or less nothing happens.



            enter image description here






            share|improve this answer


























            • Exactly what I need.

              – Sigur
              5 hours ago











            • Do you think is it possible to change the text also if there is argument? I mean, to print counters are 5,6,7,8 for example.

              – Sigur
              5 hours ago






            • 1





              @Sigur Your wish is my command. Updated.

              – egreg
              5 hours ago











            • Oh, I think I should learn about NewDocumentCommand as soon as possible. Amazing and powerful.

              – Sigur
              5 hours ago
















            5














            With expl3 it's just a few lines.



            documentclass{report}
            usepackage{xparse}

            newcounter{mycount}
            setcounter{mycount}{0}

            ExplSyntaxOn
            NewDocumentCommand{foo}{O{1}}
            {
            int_compare:nT { #1 > 0 }
            {
            int_compare:nTF { #1 = 1 } {counter~is} {counters~are}~
            stepcounter{mycount}
            textbf{themycount}
            prg_replicate:nn { #1 - 1 } {,stepcounter{mycount}textbf{themycount}}
            }
            }
            ExplSyntaxOff

            begin{document}
            first use: foo

            second use: foo

            now using with option to iterate: foo[2]

            now using with option to iterate: counters are textbf{3,4}

            foo[5]

            end{document}


            The trick is to do the first iteration and then proceed again adding first a comma.



            If the number passed is zero or less nothing happens.



            enter image description here






            share|improve this answer


























            • Exactly what I need.

              – Sigur
              5 hours ago











            • Do you think is it possible to change the text also if there is argument? I mean, to print counters are 5,6,7,8 for example.

              – Sigur
              5 hours ago






            • 1





              @Sigur Your wish is my command. Updated.

              – egreg
              5 hours ago











            • Oh, I think I should learn about NewDocumentCommand as soon as possible. Amazing and powerful.

              – Sigur
              5 hours ago














            5












            5








            5







            With expl3 it's just a few lines.



            documentclass{report}
            usepackage{xparse}

            newcounter{mycount}
            setcounter{mycount}{0}

            ExplSyntaxOn
            NewDocumentCommand{foo}{O{1}}
            {
            int_compare:nT { #1 > 0 }
            {
            int_compare:nTF { #1 = 1 } {counter~is} {counters~are}~
            stepcounter{mycount}
            textbf{themycount}
            prg_replicate:nn { #1 - 1 } {,stepcounter{mycount}textbf{themycount}}
            }
            }
            ExplSyntaxOff

            begin{document}
            first use: foo

            second use: foo

            now using with option to iterate: foo[2]

            now using with option to iterate: counters are textbf{3,4}

            foo[5]

            end{document}


            The trick is to do the first iteration and then proceed again adding first a comma.



            If the number passed is zero or less nothing happens.



            enter image description here






            share|improve this answer















            With expl3 it's just a few lines.



            documentclass{report}
            usepackage{xparse}

            newcounter{mycount}
            setcounter{mycount}{0}

            ExplSyntaxOn
            NewDocumentCommand{foo}{O{1}}
            {
            int_compare:nT { #1 > 0 }
            {
            int_compare:nTF { #1 = 1 } {counter~is} {counters~are}~
            stepcounter{mycount}
            textbf{themycount}
            prg_replicate:nn { #1 - 1 } {,stepcounter{mycount}textbf{themycount}}
            }
            }
            ExplSyntaxOff

            begin{document}
            first use: foo

            second use: foo

            now using with option to iterate: foo[2]

            now using with option to iterate: counters are textbf{3,4}

            foo[5]

            end{document}


            The trick is to do the first iteration and then proceed again adding first a comma.



            If the number passed is zero or less nothing happens.



            enter image description here







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 5 hours ago

























            answered 5 hours ago









            egregegreg

            713k8618943182




            713k8618943182













            • Exactly what I need.

              – Sigur
              5 hours ago











            • Do you think is it possible to change the text also if there is argument? I mean, to print counters are 5,6,7,8 for example.

              – Sigur
              5 hours ago






            • 1





              @Sigur Your wish is my command. Updated.

              – egreg
              5 hours ago











            • Oh, I think I should learn about NewDocumentCommand as soon as possible. Amazing and powerful.

              – Sigur
              5 hours ago



















            • Exactly what I need.

              – Sigur
              5 hours ago











            • Do you think is it possible to change the text also if there is argument? I mean, to print counters are 5,6,7,8 for example.

              – Sigur
              5 hours ago






            • 1





              @Sigur Your wish is my command. Updated.

              – egreg
              5 hours ago











            • Oh, I think I should learn about NewDocumentCommand as soon as possible. Amazing and powerful.

              – Sigur
              5 hours ago

















            Exactly what I need.

            – Sigur
            5 hours ago





            Exactly what I need.

            – Sigur
            5 hours ago













            Do you think is it possible to change the text also if there is argument? I mean, to print counters are 5,6,7,8 for example.

            – Sigur
            5 hours ago





            Do you think is it possible to change the text also if there is argument? I mean, to print counters are 5,6,7,8 for example.

            – Sigur
            5 hours ago




            1




            1





            @Sigur Your wish is my command. Updated.

            – egreg
            5 hours ago





            @Sigur Your wish is my command. Updated.

            – egreg
            5 hours ago













            Oh, I think I should learn about NewDocumentCommand as soon as possible. Amazing and powerful.

            – Sigur
            5 hours ago





            Oh, I think I should learn about NewDocumentCommand as soon as possible. Amazing and powerful.

            – Sigur
            5 hours ago











            2














            Here is an approach using multido for looping and a delayed delimiter definition:



            enter image description here



            documentclass{article}

            usepackage{multido}

            newcounter{mycount}

            newcommand{foo}[1][1]{%
            counter%
            ifnum#1>1relax s areelse space isfi
            space
            % https://tex.stackexchange.com/a/89187/5764
            defitemdelim{unskipspacedefitemdelim{,space}}% Item delimiter delayed by one cycle
            {bfseriesmultido{i=1+1}{#1}{itemdelim stepcounter{mycount}themycount}}%
            }

            begin{document}

            first use: foo

            second use: foo

            now using with option to iterate: foo[2]

            medskip

            textit{desired output from line above should be:}

            medskip

            now using with option to iterate: counters are textbf{3, 4}

            end{document}





            share|improve this answer
























            • This solution is more understandable to me... but I had never done it by myself. Very nice.

              – Sigur
              5 hours ago
















            2














            Here is an approach using multido for looping and a delayed delimiter definition:



            enter image description here



            documentclass{article}

            usepackage{multido}

            newcounter{mycount}

            newcommand{foo}[1][1]{%
            counter%
            ifnum#1>1relax s areelse space isfi
            space
            % https://tex.stackexchange.com/a/89187/5764
            defitemdelim{unskipspacedefitemdelim{,space}}% Item delimiter delayed by one cycle
            {bfseriesmultido{i=1+1}{#1}{itemdelim stepcounter{mycount}themycount}}%
            }

            begin{document}

            first use: foo

            second use: foo

            now using with option to iterate: foo[2]

            medskip

            textit{desired output from line above should be:}

            medskip

            now using with option to iterate: counters are textbf{3, 4}

            end{document}





            share|improve this answer
























            • This solution is more understandable to me... but I had never done it by myself. Very nice.

              – Sigur
              5 hours ago














            2












            2








            2







            Here is an approach using multido for looping and a delayed delimiter definition:



            enter image description here



            documentclass{article}

            usepackage{multido}

            newcounter{mycount}

            newcommand{foo}[1][1]{%
            counter%
            ifnum#1>1relax s areelse space isfi
            space
            % https://tex.stackexchange.com/a/89187/5764
            defitemdelim{unskipspacedefitemdelim{,space}}% Item delimiter delayed by one cycle
            {bfseriesmultido{i=1+1}{#1}{itemdelim stepcounter{mycount}themycount}}%
            }

            begin{document}

            first use: foo

            second use: foo

            now using with option to iterate: foo[2]

            medskip

            textit{desired output from line above should be:}

            medskip

            now using with option to iterate: counters are textbf{3, 4}

            end{document}





            share|improve this answer













            Here is an approach using multido for looping and a delayed delimiter definition:



            enter image description here



            documentclass{article}

            usepackage{multido}

            newcounter{mycount}

            newcommand{foo}[1][1]{%
            counter%
            ifnum#1>1relax s areelse space isfi
            space
            % https://tex.stackexchange.com/a/89187/5764
            defitemdelim{unskipspacedefitemdelim{,space}}% Item delimiter delayed by one cycle
            {bfseriesmultido{i=1+1}{#1}{itemdelim stepcounter{mycount}themycount}}%
            }

            begin{document}

            first use: foo

            second use: foo

            now using with option to iterate: foo[2]

            medskip

            textit{desired output from line above should be:}

            medskip

            now using with option to iterate: counters are textbf{3, 4}

            end{document}






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 5 hours ago









            WernerWerner

            440k669681662




            440k669681662













            • This solution is more understandable to me... but I had never done it by myself. Very nice.

              – Sigur
              5 hours ago



















            • This solution is more understandable to me... but I had never done it by myself. Very nice.

              – Sigur
              5 hours ago

















            This solution is more understandable to me... but I had never done it by myself. Very nice.

            – Sigur
            5 hours ago





            This solution is more understandable to me... but I had never done it by myself. Very nice.

            – Sigur
            5 hours ago











            1














            With pgffor it is even less lines.



            documentclass{report}
            usepackage{pgffor}
            newcounter{mycount}
            setcounter{mycount}{0}
            newcommand{foo}[1][1]{counter is foreach X [count=Y] in
            {1,...,#1}{stepcounter{mycount}%
            textbf{themycount}ifnumY=#1%
            else%
            ,fi}}

            begin{document}
            first use: foo

            second use: foo

            now using with option to iterate: foo[2]

            end{document}


            enter image description here






            share|improve this answer


























            • @egreg You're right. Fixed it.

              – marmot
              5 hours ago


















            1














            With pgffor it is even less lines.



            documentclass{report}
            usepackage{pgffor}
            newcounter{mycount}
            setcounter{mycount}{0}
            newcommand{foo}[1][1]{counter is foreach X [count=Y] in
            {1,...,#1}{stepcounter{mycount}%
            textbf{themycount}ifnumY=#1%
            else%
            ,fi}}

            begin{document}
            first use: foo

            second use: foo

            now using with option to iterate: foo[2]

            end{document}


            enter image description here






            share|improve this answer


























            • @egreg You're right. Fixed it.

              – marmot
              5 hours ago
















            1












            1








            1







            With pgffor it is even less lines.



            documentclass{report}
            usepackage{pgffor}
            newcounter{mycount}
            setcounter{mycount}{0}
            newcommand{foo}[1][1]{counter is foreach X [count=Y] in
            {1,...,#1}{stepcounter{mycount}%
            textbf{themycount}ifnumY=#1%
            else%
            ,fi}}

            begin{document}
            first use: foo

            second use: foo

            now using with option to iterate: foo[2]

            end{document}


            enter image description here






            share|improve this answer















            With pgffor it is even less lines.



            documentclass{report}
            usepackage{pgffor}
            newcounter{mycount}
            setcounter{mycount}{0}
            newcommand{foo}[1][1]{counter is foreach X [count=Y] in
            {1,...,#1}{stepcounter{mycount}%
            textbf{themycount}ifnumY=#1%
            else%
            ,fi}}

            begin{document}
            first use: foo

            second use: foo

            now using with option to iterate: foo[2]

            end{document}


            enter image description here







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 5 hours ago

























            answered 5 hours ago









            marmotmarmot

            92.5k4109203




            92.5k4109203













            • @egreg You're right. Fixed it.

              – marmot
              5 hours ago





















            • @egreg You're right. Fixed it.

              – marmot
              5 hours ago



















            @egreg You're right. Fixed it.

            – marmot
            5 hours ago







            @egreg You're right. Fixed it.

            – marmot
            5 hours ago













            1














            Here is short code with multido, xparse and etoolbox:



            documentclass{report}
            usepackage{multido}
            usepackage{xparse, etoolbox}
            newcounter{mycount}
            setcounter{mycount}{0}
            NewDocumentCommand{foo}{O{1}}{counter is multido{i=1+1}{#1}{stepcounter{mycount}textbf{themycount}ifnumless{multidocount}{#1}{,,}{.}}}

            begin{document}

            first use: foo

            second use: foo

            now using with option to iterate: foo[5]

            end{document}


            enter image description here






            share|improve this answer




























              1














              Here is short code with multido, xparse and etoolbox:



              documentclass{report}
              usepackage{multido}
              usepackage{xparse, etoolbox}
              newcounter{mycount}
              setcounter{mycount}{0}
              NewDocumentCommand{foo}{O{1}}{counter is multido{i=1+1}{#1}{stepcounter{mycount}textbf{themycount}ifnumless{multidocount}{#1}{,,}{.}}}

              begin{document}

              first use: foo

              second use: foo

              now using with option to iterate: foo[5]

              end{document}


              enter image description here






              share|improve this answer


























                1












                1








                1







                Here is short code with multido, xparse and etoolbox:



                documentclass{report}
                usepackage{multido}
                usepackage{xparse, etoolbox}
                newcounter{mycount}
                setcounter{mycount}{0}
                NewDocumentCommand{foo}{O{1}}{counter is multido{i=1+1}{#1}{stepcounter{mycount}textbf{themycount}ifnumless{multidocount}{#1}{,,}{.}}}

                begin{document}

                first use: foo

                second use: foo

                now using with option to iterate: foo[5]

                end{document}


                enter image description here






                share|improve this answer













                Here is short code with multido, xparse and etoolbox:



                documentclass{report}
                usepackage{multido}
                usepackage{xparse, etoolbox}
                newcounter{mycount}
                setcounter{mycount}{0}
                NewDocumentCommand{foo}{O{1}}{counter is multido{i=1+1}{#1}{stepcounter{mycount}textbf{themycount}ifnumless{multidocount}{#1}{,,}{.}}}

                begin{document}

                first use: foo

                second use: foo

                now using with option to iterate: foo[5]

                end{document}


                enter image description here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 5 hours ago









                BernardBernard

                167k770195




                167k770195






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f470788%2floop-over-an-optional-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

                    Loup dans la culture

                    How to solve the problem of ntp “Unable to contact time server” from KDE?

                    Connection limited (no internet access)