Loop over an optional argument

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












7















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



MWE



documentclassreport

newcountermycount
setcountermycount0
newcommandfoostepcountermycountcounter is textbfthemycount

begindocument
first use: foo

second use: foo

now using with option to iterate: foo[2]

medskip

textitdesired output from line above should be:

medskip

now using with option to iterate: counter is textbf3,4

enddocument


enter image description here










share|improve this question


























    7















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



    MWE



    documentclassreport

    newcountermycount
    setcountermycount0
    newcommandfoostepcountermycountcounter is textbfthemycount

    begindocument
    first use: foo

    second use: foo

    now using with option to iterate: foo[2]

    medskip

    textitdesired output from line above should be:

    medskip

    now using with option to iterate: counter is textbf3,4

    enddocument


    enter image description here










    share|improve this question
























      7












      7








      7


      2






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



      MWE



      documentclassreport

      newcountermycount
      setcountermycount0
      newcommandfoostepcountermycountcounter is textbfthemycount

      begindocument
      first use: foo

      second use: foo

      now using with option to iterate: foo[2]

      medskip

      textitdesired output from line above should be:

      medskip

      now using with option to iterate: counter is textbf3,4

      enddocument


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



      MWE



      documentclassreport

      newcountermycount
      setcountermycount0
      newcommandfoostepcountermycountcounter is textbfthemycount

      begindocument
      first use: foo

      second use: foo

      now using with option to iterate: foo[2]

      medskip

      textitdesired output from line above should be:

      medskip

      now using with option to iterate: counter is textbf3,4

      enddocument


      enter image description here







      macros counters loops






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 18 at 23:45









      SigurSigur

      24.7k356139




      24.7k356139




















          5 Answers
          5






          active

          oldest

          votes


















          8














          With expl3 it's just a few lines.



          documentclassreport
          usepackagexparse

          newcountermycount
          setcountermycount0

          ExplSyntaxOn
          NewDocumentCommandfooO1

          int_compare:nT #1 > 0

          int_compare:nTF #1 = 1 counter~is counters~are~
          stepcountermycount
          textbfthemycount
          prg_replicate:nn #1 - 1 ,stepcountermycounttextbfthemycount


          ExplSyntaxOff

          begindocument
          first use: foo

          second use: foo

          now using with option to iterate: foo[2]

          now using with option to iterate: counters are textbf3,4

          foo[5]

          enddocument


          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
            Jan 18 at 23:56











          • 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
            Jan 18 at 23:57






          • 2





            @Sigur Your wish is my command. Updated.

            – egreg
            Jan 19 at 0:03











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

            – Sigur
            Jan 19 at 0:08


















          4














          Here is short code with multido, xparse and etoolbox:



          documentclassreport
          usepackagemultido
          usepackagexparse, etoolbox
          newcountermycount
          setcountermycount0
          NewDocumentCommandfooO1counter is multidoi=1+1#1stepcountermycounttextbfthemycountifnumlessmultidocount#1,,.

          begindocument

          first use: foo

          second use: foo

          now using with option to iterate: foo[5]

          enddocument


          enter image description here






          share|improve this answer






























            3














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



            enter image description here



            documentclassarticle

            usepackagemultido

            newcountermycount

            newcommandfoo[1][1]%
            counter%
            ifnum#1>1relax s areelse space isfi
            space
            % https://tex.stackexchange.com/a/89187/5764
            defitemdelimunskipspacedefitemdelim,space% Item delimiter delayed by one cycle
            bfseriesmultidoi=1+1#1itemdelim stepcountermycountthemycount%


            begindocument

            first use: foo

            second use: foo

            now using with option to iterate: foo[2]

            medskip

            textitdesired output from line above should be:

            medskip

            now using with option to iterate: counters are textbf3, 4

            enddocument





            share|improve this answer























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

              – Sigur
              Jan 19 at 0:11


















            3














            With pgffor it is even less lines.



            documentclassreport
            usepackagepgffor
            newcountermycount
            setcountermycount0
            newcommandfoo[1][1]counter is foreach X [count=Y] in
            1,...,#1stepcountermycount%
            textbfthemycountifnumY=#1%
            else%
            ,fi

            begindocument
            first use: foo

            second use: foo

            now using with option to iterate: foo[2]

            enddocument


            enter image description here






            share|improve this answer
































              1














              A bit more code but without additional packages:



              documentclassreport

              newcountermycount
              setcountermycount0

              newcommandExchange[2]#2#1%
              newcommandfoo[1][1]fooloop#1counter value is
              makeatletter
              newcommandfooloop[3]%
              ifnum#1>0 stepcountermycountfi
              ifnum#1>1 expandafter@firstoftwoelseexpandafter@secondoftwofi
              %
              begingroup
              protected@edef@tempathemycount%
              c@mycount=#1relax
              advancec@mycount by -1relax
              expandafterendgroup
              expandafterfooloopexpandafter
              numberexpandafterc@mycountexpandafterexpandafter
              romannumeral0expandafterExchangeexpandafter@tempa #2, %
              counter values are%
              %
              #3 textbf#2themycount%
              %
              %
              makeatother

              begindocument
              first use: foo

              second use: foo

              now using with option to iterate: foo[4]

              medskip

              textitdesired output from line above should be:

              medskip

              now using with option to iterate: counter values are textbf3, 4, 5, 6

              medskip

              now using with option to iterate 0 times---counter is unchanged and displayed: foo[0]

              medskip

              now using with option to iterate -1 times---counter is unchanged and displayed: foo[-1]

              enddocument


              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

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                8














                With expl3 it's just a few lines.



                documentclassreport
                usepackagexparse

                newcountermycount
                setcountermycount0

                ExplSyntaxOn
                NewDocumentCommandfooO1

                int_compare:nT #1 > 0

                int_compare:nTF #1 = 1 counter~is counters~are~
                stepcountermycount
                textbfthemycount
                prg_replicate:nn #1 - 1 ,stepcountermycounttextbfthemycount


                ExplSyntaxOff

                begindocument
                first use: foo

                second use: foo

                now using with option to iterate: foo[2]

                now using with option to iterate: counters are textbf3,4

                foo[5]

                enddocument


                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
                  Jan 18 at 23:56











                • 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
                  Jan 18 at 23:57






                • 2





                  @Sigur Your wish is my command. Updated.

                  – egreg
                  Jan 19 at 0:03











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

                  – Sigur
                  Jan 19 at 0:08















                8














                With expl3 it's just a few lines.



                documentclassreport
                usepackagexparse

                newcountermycount
                setcountermycount0

                ExplSyntaxOn
                NewDocumentCommandfooO1

                int_compare:nT #1 > 0

                int_compare:nTF #1 = 1 counter~is counters~are~
                stepcountermycount
                textbfthemycount
                prg_replicate:nn #1 - 1 ,stepcountermycounttextbfthemycount


                ExplSyntaxOff

                begindocument
                first use: foo

                second use: foo

                now using with option to iterate: foo[2]

                now using with option to iterate: counters are textbf3,4

                foo[5]

                enddocument


                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
                  Jan 18 at 23:56











                • 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
                  Jan 18 at 23:57






                • 2





                  @Sigur Your wish is my command. Updated.

                  – egreg
                  Jan 19 at 0:03











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

                  – Sigur
                  Jan 19 at 0:08













                8












                8








                8







                With expl3 it's just a few lines.



                documentclassreport
                usepackagexparse

                newcountermycount
                setcountermycount0

                ExplSyntaxOn
                NewDocumentCommandfooO1

                int_compare:nT #1 > 0

                int_compare:nTF #1 = 1 counter~is counters~are~
                stepcountermycount
                textbfthemycount
                prg_replicate:nn #1 - 1 ,stepcountermycounttextbfthemycount


                ExplSyntaxOff

                begindocument
                first use: foo

                second use: foo

                now using with option to iterate: foo[2]

                now using with option to iterate: counters are textbf3,4

                foo[5]

                enddocument


                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.



                documentclassreport
                usepackagexparse

                newcountermycount
                setcountermycount0

                ExplSyntaxOn
                NewDocumentCommandfooO1

                int_compare:nT #1 > 0

                int_compare:nTF #1 = 1 counter~is counters~are~
                stepcountermycount
                textbfthemycount
                prg_replicate:nn #1 - 1 ,stepcountermycounttextbfthemycount


                ExplSyntaxOff

                begindocument
                first use: foo

                second use: foo

                now using with option to iterate: foo[2]

                now using with option to iterate: counters are textbf3,4

                foo[5]

                enddocument


                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 Jan 19 at 0:03

























                answered Jan 18 at 23:54









                egregegreg

                717k8719023197




                717k8719023197












                • Exactly what I need.

                  – Sigur
                  Jan 18 at 23:56











                • 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
                  Jan 18 at 23:57






                • 2





                  @Sigur Your wish is my command. Updated.

                  – egreg
                  Jan 19 at 0:03











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

                  – Sigur
                  Jan 19 at 0:08

















                • Exactly what I need.

                  – Sigur
                  Jan 18 at 23:56











                • 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
                  Jan 18 at 23:57






                • 2





                  @Sigur Your wish is my command. Updated.

                  – egreg
                  Jan 19 at 0:03











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

                  – Sigur
                  Jan 19 at 0:08
















                Exactly what I need.

                – Sigur
                Jan 18 at 23:56





                Exactly what I need.

                – Sigur
                Jan 18 at 23:56













                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
                Jan 18 at 23:57





                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
                Jan 18 at 23:57




                2




                2





                @Sigur Your wish is my command. Updated.

                – egreg
                Jan 19 at 0:03





                @Sigur Your wish is my command. Updated.

                – egreg
                Jan 19 at 0:03













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

                – Sigur
                Jan 19 at 0:08





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

                – Sigur
                Jan 19 at 0:08











                4














                Here is short code with multido, xparse and etoolbox:



                documentclassreport
                usepackagemultido
                usepackagexparse, etoolbox
                newcountermycount
                setcountermycount0
                NewDocumentCommandfooO1counter is multidoi=1+1#1stepcountermycounttextbfthemycountifnumlessmultidocount#1,,.

                begindocument

                first use: foo

                second use: foo

                now using with option to iterate: foo[5]

                enddocument


                enter image description here






                share|improve this answer



























                  4














                  Here is short code with multido, xparse and etoolbox:



                  documentclassreport
                  usepackagemultido
                  usepackagexparse, etoolbox
                  newcountermycount
                  setcountermycount0
                  NewDocumentCommandfooO1counter is multidoi=1+1#1stepcountermycounttextbfthemycountifnumlessmultidocount#1,,.

                  begindocument

                  first use: foo

                  second use: foo

                  now using with option to iterate: foo[5]

                  enddocument


                  enter image description here






                  share|improve this answer

























                    4












                    4








                    4







                    Here is short code with multido, xparse and etoolbox:



                    documentclassreport
                    usepackagemultido
                    usepackagexparse, etoolbox
                    newcountermycount
                    setcountermycount0
                    NewDocumentCommandfooO1counter is multidoi=1+1#1stepcountermycounttextbfthemycountifnumlessmultidocount#1,,.

                    begindocument

                    first use: foo

                    second use: foo

                    now using with option to iterate: foo[5]

                    enddocument


                    enter image description here






                    share|improve this answer













                    Here is short code with multido, xparse and etoolbox:



                    documentclassreport
                    usepackagemultido
                    usepackagexparse, etoolbox
                    newcountermycount
                    setcountermycount0
                    NewDocumentCommandfooO1counter is multidoi=1+1#1stepcountermycounttextbfthemycountifnumlessmultidocount#1,,.

                    begindocument

                    first use: foo

                    second use: foo

                    now using with option to iterate: foo[5]

                    enddocument


                    enter image description here







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 19 at 0:32









                    BernardBernard

                    169k773198




                    169k773198





















                        3














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



                        enter image description here



                        documentclassarticle

                        usepackagemultido

                        newcountermycount

                        newcommandfoo[1][1]%
                        counter%
                        ifnum#1>1relax s areelse space isfi
                        space
                        % https://tex.stackexchange.com/a/89187/5764
                        defitemdelimunskipspacedefitemdelim,space% Item delimiter delayed by one cycle
                        bfseriesmultidoi=1+1#1itemdelim stepcountermycountthemycount%


                        begindocument

                        first use: foo

                        second use: foo

                        now using with option to iterate: foo[2]

                        medskip

                        textitdesired output from line above should be:

                        medskip

                        now using with option to iterate: counters are textbf3, 4

                        enddocument





                        share|improve this answer























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

                          – Sigur
                          Jan 19 at 0:11















                        3














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



                        enter image description here



                        documentclassarticle

                        usepackagemultido

                        newcountermycount

                        newcommandfoo[1][1]%
                        counter%
                        ifnum#1>1relax s areelse space isfi
                        space
                        % https://tex.stackexchange.com/a/89187/5764
                        defitemdelimunskipspacedefitemdelim,space% Item delimiter delayed by one cycle
                        bfseriesmultidoi=1+1#1itemdelim stepcountermycountthemycount%


                        begindocument

                        first use: foo

                        second use: foo

                        now using with option to iterate: foo[2]

                        medskip

                        textitdesired output from line above should be:

                        medskip

                        now using with option to iterate: counters are textbf3, 4

                        enddocument





                        share|improve this answer























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

                          – Sigur
                          Jan 19 at 0:11













                        3












                        3








                        3







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



                        enter image description here



                        documentclassarticle

                        usepackagemultido

                        newcountermycount

                        newcommandfoo[1][1]%
                        counter%
                        ifnum#1>1relax s areelse space isfi
                        space
                        % https://tex.stackexchange.com/a/89187/5764
                        defitemdelimunskipspacedefitemdelim,space% Item delimiter delayed by one cycle
                        bfseriesmultidoi=1+1#1itemdelim stepcountermycountthemycount%


                        begindocument

                        first use: foo

                        second use: foo

                        now using with option to iterate: foo[2]

                        medskip

                        textitdesired output from line above should be:

                        medskip

                        now using with option to iterate: counters are textbf3, 4

                        enddocument





                        share|improve this answer













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



                        enter image description here



                        documentclassarticle

                        usepackagemultido

                        newcountermycount

                        newcommandfoo[1][1]%
                        counter%
                        ifnum#1>1relax s areelse space isfi
                        space
                        % https://tex.stackexchange.com/a/89187/5764
                        defitemdelimunskipspacedefitemdelim,space% Item delimiter delayed by one cycle
                        bfseriesmultidoi=1+1#1itemdelim stepcountermycountthemycount%


                        begindocument

                        first use: foo

                        second use: foo

                        now using with option to iterate: foo[2]

                        medskip

                        textitdesired output from line above should be:

                        medskip

                        now using with option to iterate: counters are textbf3, 4

                        enddocument






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jan 19 at 0:04









                        WernerWerner

                        443k679761673




                        443k679761673












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

                          – Sigur
                          Jan 19 at 0:11

















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

                          – Sigur
                          Jan 19 at 0:11
















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

                        – Sigur
                        Jan 19 at 0:11





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

                        – Sigur
                        Jan 19 at 0:11











                        3














                        With pgffor it is even less lines.



                        documentclassreport
                        usepackagepgffor
                        newcountermycount
                        setcountermycount0
                        newcommandfoo[1][1]counter is foreach X [count=Y] in
                        1,...,#1stepcountermycount%
                        textbfthemycountifnumY=#1%
                        else%
                        ,fi

                        begindocument
                        first use: foo

                        second use: foo

                        now using with option to iterate: foo[2]

                        enddocument


                        enter image description here






                        share|improve this answer





























                          3














                          With pgffor it is even less lines.



                          documentclassreport
                          usepackagepgffor
                          newcountermycount
                          setcountermycount0
                          newcommandfoo[1][1]counter is foreach X [count=Y] in
                          1,...,#1stepcountermycount%
                          textbfthemycountifnumY=#1%
                          else%
                          ,fi

                          begindocument
                          first use: foo

                          second use: foo

                          now using with option to iterate: foo[2]

                          enddocument


                          enter image description here






                          share|improve this answer



























                            3












                            3








                            3







                            With pgffor it is even less lines.



                            documentclassreport
                            usepackagepgffor
                            newcountermycount
                            setcountermycount0
                            newcommandfoo[1][1]counter is foreach X [count=Y] in
                            1,...,#1stepcountermycount%
                            textbfthemycountifnumY=#1%
                            else%
                            ,fi

                            begindocument
                            first use: foo

                            second use: foo

                            now using with option to iterate: foo[2]

                            enddocument


                            enter image description here






                            share|improve this answer















                            With pgffor it is even less lines.



                            documentclassreport
                            usepackagepgffor
                            newcountermycount
                            setcountermycount0
                            newcommandfoo[1][1]counter is foreach X [count=Y] in
                            1,...,#1stepcountermycount%
                            textbfthemycountifnumY=#1%
                            else%
                            ,fi

                            begindocument
                            first use: foo

                            second use: foo

                            now using with option to iterate: foo[2]

                            enddocument


                            enter image description here







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 19 at 0:31

























                            answered Jan 19 at 0:26









                            marmotmarmot

                            97.6k4112215




                            97.6k4112215





















                                1














                                A bit more code but without additional packages:



                                documentclassreport

                                newcountermycount
                                setcountermycount0

                                newcommandExchange[2]#2#1%
                                newcommandfoo[1][1]fooloop#1counter value is
                                makeatletter
                                newcommandfooloop[3]%
                                ifnum#1>0 stepcountermycountfi
                                ifnum#1>1 expandafter@firstoftwoelseexpandafter@secondoftwofi
                                %
                                begingroup
                                protected@edef@tempathemycount%
                                c@mycount=#1relax
                                advancec@mycount by -1relax
                                expandafterendgroup
                                expandafterfooloopexpandafter
                                numberexpandafterc@mycountexpandafterexpandafter
                                romannumeral0expandafterExchangeexpandafter@tempa #2, %
                                counter values are%
                                %
                                #3 textbf#2themycount%
                                %
                                %
                                makeatother

                                begindocument
                                first use: foo

                                second use: foo

                                now using with option to iterate: foo[4]

                                medskip

                                textitdesired output from line above should be:

                                medskip

                                now using with option to iterate: counter values are textbf3, 4, 5, 6

                                medskip

                                now using with option to iterate 0 times---counter is unchanged and displayed: foo[0]

                                medskip

                                now using with option to iterate -1 times---counter is unchanged and displayed: foo[-1]

                                enddocument


                                enter image description here






                                share|improve this answer





























                                  1














                                  A bit more code but without additional packages:



                                  documentclassreport

                                  newcountermycount
                                  setcountermycount0

                                  newcommandExchange[2]#2#1%
                                  newcommandfoo[1][1]fooloop#1counter value is
                                  makeatletter
                                  newcommandfooloop[3]%
                                  ifnum#1>0 stepcountermycountfi
                                  ifnum#1>1 expandafter@firstoftwoelseexpandafter@secondoftwofi
                                  %
                                  begingroup
                                  protected@edef@tempathemycount%
                                  c@mycount=#1relax
                                  advancec@mycount by -1relax
                                  expandafterendgroup
                                  expandafterfooloopexpandafter
                                  numberexpandafterc@mycountexpandafterexpandafter
                                  romannumeral0expandafterExchangeexpandafter@tempa #2, %
                                  counter values are%
                                  %
                                  #3 textbf#2themycount%
                                  %
                                  %
                                  makeatother

                                  begindocument
                                  first use: foo

                                  second use: foo

                                  now using with option to iterate: foo[4]

                                  medskip

                                  textitdesired output from line above should be:

                                  medskip

                                  now using with option to iterate: counter values are textbf3, 4, 5, 6

                                  medskip

                                  now using with option to iterate 0 times---counter is unchanged and displayed: foo[0]

                                  medskip

                                  now using with option to iterate -1 times---counter is unchanged and displayed: foo[-1]

                                  enddocument


                                  enter image description here






                                  share|improve this answer



























                                    1












                                    1








                                    1







                                    A bit more code but without additional packages:



                                    documentclassreport

                                    newcountermycount
                                    setcountermycount0

                                    newcommandExchange[2]#2#1%
                                    newcommandfoo[1][1]fooloop#1counter value is
                                    makeatletter
                                    newcommandfooloop[3]%
                                    ifnum#1>0 stepcountermycountfi
                                    ifnum#1>1 expandafter@firstoftwoelseexpandafter@secondoftwofi
                                    %
                                    begingroup
                                    protected@edef@tempathemycount%
                                    c@mycount=#1relax
                                    advancec@mycount by -1relax
                                    expandafterendgroup
                                    expandafterfooloopexpandafter
                                    numberexpandafterc@mycountexpandafterexpandafter
                                    romannumeral0expandafterExchangeexpandafter@tempa #2, %
                                    counter values are%
                                    %
                                    #3 textbf#2themycount%
                                    %
                                    %
                                    makeatother

                                    begindocument
                                    first use: foo

                                    second use: foo

                                    now using with option to iterate: foo[4]

                                    medskip

                                    textitdesired output from line above should be:

                                    medskip

                                    now using with option to iterate: counter values are textbf3, 4, 5, 6

                                    medskip

                                    now using with option to iterate 0 times---counter is unchanged and displayed: foo[0]

                                    medskip

                                    now using with option to iterate -1 times---counter is unchanged and displayed: foo[-1]

                                    enddocument


                                    enter image description here






                                    share|improve this answer















                                    A bit more code but without additional packages:



                                    documentclassreport

                                    newcountermycount
                                    setcountermycount0

                                    newcommandExchange[2]#2#1%
                                    newcommandfoo[1][1]fooloop#1counter value is
                                    makeatletter
                                    newcommandfooloop[3]%
                                    ifnum#1>0 stepcountermycountfi
                                    ifnum#1>1 expandafter@firstoftwoelseexpandafter@secondoftwofi
                                    %
                                    begingroup
                                    protected@edef@tempathemycount%
                                    c@mycount=#1relax
                                    advancec@mycount by -1relax
                                    expandafterendgroup
                                    expandafterfooloopexpandafter
                                    numberexpandafterc@mycountexpandafterexpandafter
                                    romannumeral0expandafterExchangeexpandafter@tempa #2, %
                                    counter values are%
                                    %
                                    #3 textbf#2themycount%
                                    %
                                    %
                                    makeatother

                                    begindocument
                                    first use: foo

                                    second use: foo

                                    now using with option to iterate: foo[4]

                                    medskip

                                    textitdesired output from line above should be:

                                    medskip

                                    now using with option to iterate: counter values are textbf3, 4, 5, 6

                                    medskip

                                    now using with option to iterate 0 times---counter is unchanged and displayed: foo[0]

                                    medskip

                                    now using with option to iterate -1 times---counter is unchanged and displayed: foo[-1]

                                    enddocument


                                    enter image description here







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Jan 27 at 5:21

























                                    answered Jan 27 at 5:10









                                    Ulrich DiezUlrich Diez

                                    4,500616




                                    4,500616



























                                        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

                                        How to check contact read email or not when send email to Individual?

                                        Bahrain

                                        Postfix configuration issue with fips on centos 7; mailgun relay