Result of pgmathsetmacro is lost in loop iterationsIncrement loop variable in inner foreach loopHow to define macros in a foreach loop with effects between iterations and after the loop without using global?Why is there no pgfmath macro that expands directly to the result?Drawing iterations of a recursive structure in TikZ / using node names defined in a loopSetting result from pgfmathifthenelse as variableCombination Function with Factorial - Not correct result - Pgfplotspgfmathresult with unexpected resultTikZ: pgfmath operation doesn't give the expected result

Would the Republic of Ireland and Northern Ireland be interested in joining together?

What is the difference between Major and Minor Bug?

Can more than one wizard copy a spell from a spellbook?

Identify a problem where a potentially winning move draws because of the 50 move rule

Which note goes on which side of the stem?

How to find out the average duration of the peer-review process for a given journal?

Who was president of the USA?

Is gzip atomic?

Do they have Supervillain(s)?

Is “I am getting married with my sister” ambiguous?

Why is there so little discussion / research on the philosophy of precision?

Algorithms vs LP or MIP

Best clipless pedals for sore feet?

How would one country purchase another?

Is using a hyperlink to close a modal a poor design decision?

Prove your innocence

Is there any practical application for performing a double Fourier transform? ...or an inverse Fourier transform on a time-domain input?

What would be the challenges to taking off and landing a typical passenger jet at FL300?

Is for(( ... )) ... ; a valid shell syntax? In which shells?

Anatomically Correct Whomping Willow

Why in most German places is the church the tallest building?

What is the difference between "Grippe" and "Männergrippe"?

LeetCode: Group Anagrams C#

Can't stopover at Sapporo when going from Asahikawa to Chitose airport?



Result of pgmathsetmacro is lost in loop iterations


Increment loop variable in inner foreach loopHow to define macros in a foreach loop with effects between iterations and after the loop without using global?Why is there no pgfmath macro that expands directly to the result?Drawing iterations of a recursive structure in TikZ / using node names defined in a loopSetting result from pgfmathifthenelse as variableCombination Function with Factorial - Not correct result - Pgfplotspgfmathresult with unexpected resultTikZ: pgfmath operation doesn't give the expected result






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















Consider the following code.



%!TEX program = lualatex
documentclass[tikz]standalone

begindocument

pgfmathsetmacrosumk0

foreach k in 1,2,...,10
pgfmathsetmacrosumkk+sumk
directlua print(k,sumk)


enddocument


In this simple example, I want to sum recursively from 1 to 10. This is just done to reproduce my problem: accessing a variable which is set within the loop in next loop iteration, and possibly accessing it also after the loop ended.



My expected output would be (in the log, written with lua print)



1 1.0
2 3.0
3 6.0
4 10.0
5 15.0
6 21.0
7 28.0
8 36.0
9 45.0
10 55.0


However, it is



1 1.0
2 2.0
3 3.0
4 4.0
5 5.0
6 6.0
7 7.0
8 8.0
9 9.0
10 10.0


Can a macro defined within the loop be reused later?










share|improve this question
































    1















    Consider the following code.



    %!TEX program = lualatex
    documentclass[tikz]standalone

    begindocument

    pgfmathsetmacrosumk0

    foreach k in 1,2,...,10
    pgfmathsetmacrosumkk+sumk
    directlua print(k,sumk)


    enddocument


    In this simple example, I want to sum recursively from 1 to 10. This is just done to reproduce my problem: accessing a variable which is set within the loop in next loop iteration, and possibly accessing it also after the loop ended.



    My expected output would be (in the log, written with lua print)



    1 1.0
    2 3.0
    3 6.0
    4 10.0
    5 15.0
    6 21.0
    7 28.0
    8 36.0
    9 45.0
    10 55.0


    However, it is



    1 1.0
    2 2.0
    3 3.0
    4 4.0
    5 5.0
    6 6.0
    7 7.0
    8 8.0
    9 9.0
    10 10.0


    Can a macro defined within the loop be reused later?










    share|improve this question




























      1












      1








      1








      Consider the following code.



      %!TEX program = lualatex
      documentclass[tikz]standalone

      begindocument

      pgfmathsetmacrosumk0

      foreach k in 1,2,...,10
      pgfmathsetmacrosumkk+sumk
      directlua print(k,sumk)


      enddocument


      In this simple example, I want to sum recursively from 1 to 10. This is just done to reproduce my problem: accessing a variable which is set within the loop in next loop iteration, and possibly accessing it also after the loop ended.



      My expected output would be (in the log, written with lua print)



      1 1.0
      2 3.0
      3 6.0
      4 10.0
      5 15.0
      6 21.0
      7 28.0
      8 36.0
      9 45.0
      10 55.0


      However, it is



      1 1.0
      2 2.0
      3 3.0
      4 4.0
      5 5.0
      6 6.0
      7 7.0
      8 8.0
      9 9.0
      10 10.0


      Can a macro defined within the loop be reused later?










      share|improve this question
















      Consider the following code.



      %!TEX program = lualatex
      documentclass[tikz]standalone

      begindocument

      pgfmathsetmacrosumk0

      foreach k in 1,2,...,10
      pgfmathsetmacrosumkk+sumk
      directlua print(k,sumk)


      enddocument


      In this simple example, I want to sum recursively from 1 to 10. This is just done to reproduce my problem: accessing a variable which is set within the loop in next loop iteration, and possibly accessing it also after the loop ended.



      My expected output would be (in the log, written with lua print)



      1 1.0
      2 3.0
      3 6.0
      4 10.0
      5 15.0
      6 21.0
      7 28.0
      8 36.0
      9 45.0
      10 55.0


      However, it is



      1 1.0
      2 2.0
      3 3.0
      4 4.0
      5 5.0
      6 6.0
      7 7.0
      8 8.0
      9 9.0
      10 10.0


      Can a macro defined within the loop be reused later?







      tikz-pgf pgfmath






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 8 hours ago







      Faekynn

















      asked 8 hours ago









      FaekynnFaekynn

      1,2471 gold badge9 silver badges29 bronze badges




      1,2471 gold badge9 silver badges29 bronze badges























          3 Answers
          3






          active

          oldest

          votes


















          1















          Here is a way:



          documentclass[tikz]standalone 

          begindocument

          defsumk0
          deffinsumk0
          noindentforeach k in 1,2,...,10%
          pgfmathsetmacrosumkk+finsumk%
          xdeffinsumksumk%
          %directlua print(k,sumk)
          k, finsumk\


          enddocument


          PS: Add the lua code instead of my simple LaTeX way to print.



          Output: Your desired output.



          Reason of your failure: Try to change my xdef to def and you will realize that it doesn't work because been lost (gets 0) in every iteration. This is because sumk is a value that expands for one usage inside the code and xdef first expands it and then save it globally.






          share|improve this answer

























          • The explanation helps me to understand it, thanks! Tikz even has some built-in functionality, but your answer seems more general.

            – Faekynn
            7 hours ago












          • Thanks @Faekynn ... Happy TeXing.

            – koleygr
            7 hours ago


















          2















          Actually, TikZ/pgf offers a quite simple solution.
          For me, the standard behavior is completely unexpected.



          %!TEX program = lualatex
          documentclass[tikz]standalone

          begindocument

          pgfmathsetmacrosumk0

          foreach k [remember=sumk as sumk] in 1,2,...,10
          pgfmathsetmacrosumkk+sumk
          directlua print(k,sumk)


          enddocument





          share|improve this answer


































            1















            It is actually a common behavior of foreach compared to e.g. pgfplotsinvokeforeach provided by pgfplots. You can get more information about this behavior and some examples in pgf-tikz manual § 7 Utilities and pgfplots manual § 8.1 Utility Commands.
            I think it is because foreach introduces scopes, while pgfplotsinvokeforeach does not, so the sumk variable is not expanded.



            The following example produces the desired output



            %!TEX program = lualatex
            documentclass[tikz]standalone
            usepackagepgfplots
            begindocument

            pgfmathsetmacrosumk0

            pgfplotsinvokeforeach1,2,...,10
            pgfmathsetmacrosumk#1+sumk
            directlua print(#1,sumk)


            enddocument





            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%2f505421%2fresult-of-pgmathsetmacro-is-lost-in-loop-iterations%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1















              Here is a way:



              documentclass[tikz]standalone 

              begindocument

              defsumk0
              deffinsumk0
              noindentforeach k in 1,2,...,10%
              pgfmathsetmacrosumkk+finsumk%
              xdeffinsumksumk%
              %directlua print(k,sumk)
              k, finsumk\


              enddocument


              PS: Add the lua code instead of my simple LaTeX way to print.



              Output: Your desired output.



              Reason of your failure: Try to change my xdef to def and you will realize that it doesn't work because been lost (gets 0) in every iteration. This is because sumk is a value that expands for one usage inside the code and xdef first expands it and then save it globally.






              share|improve this answer

























              • The explanation helps me to understand it, thanks! Tikz even has some built-in functionality, but your answer seems more general.

                – Faekynn
                7 hours ago












              • Thanks @Faekynn ... Happy TeXing.

                – koleygr
                7 hours ago















              1















              Here is a way:



              documentclass[tikz]standalone 

              begindocument

              defsumk0
              deffinsumk0
              noindentforeach k in 1,2,...,10%
              pgfmathsetmacrosumkk+finsumk%
              xdeffinsumksumk%
              %directlua print(k,sumk)
              k, finsumk\


              enddocument


              PS: Add the lua code instead of my simple LaTeX way to print.



              Output: Your desired output.



              Reason of your failure: Try to change my xdef to def and you will realize that it doesn't work because been lost (gets 0) in every iteration. This is because sumk is a value that expands for one usage inside the code and xdef first expands it and then save it globally.






              share|improve this answer

























              • The explanation helps me to understand it, thanks! Tikz even has some built-in functionality, but your answer seems more general.

                – Faekynn
                7 hours ago












              • Thanks @Faekynn ... Happy TeXing.

                – koleygr
                7 hours ago













              1














              1










              1









              Here is a way:



              documentclass[tikz]standalone 

              begindocument

              defsumk0
              deffinsumk0
              noindentforeach k in 1,2,...,10%
              pgfmathsetmacrosumkk+finsumk%
              xdeffinsumksumk%
              %directlua print(k,sumk)
              k, finsumk\


              enddocument


              PS: Add the lua code instead of my simple LaTeX way to print.



              Output: Your desired output.



              Reason of your failure: Try to change my xdef to def and you will realize that it doesn't work because been lost (gets 0) in every iteration. This is because sumk is a value that expands for one usage inside the code and xdef first expands it and then save it globally.






              share|improve this answer













              Here is a way:



              documentclass[tikz]standalone 

              begindocument

              defsumk0
              deffinsumk0
              noindentforeach k in 1,2,...,10%
              pgfmathsetmacrosumkk+finsumk%
              xdeffinsumksumk%
              %directlua print(k,sumk)
              k, finsumk\


              enddocument


              PS: Add the lua code instead of my simple LaTeX way to print.



              Output: Your desired output.



              Reason of your failure: Try to change my xdef to def and you will realize that it doesn't work because been lost (gets 0) in every iteration. This is because sumk is a value that expands for one usage inside the code and xdef first expands it and then save it globally.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 7 hours ago









              koleygrkoleygr

              14.6k1 gold badge10 silver badges40 bronze badges




              14.6k1 gold badge10 silver badges40 bronze badges















              • The explanation helps me to understand it, thanks! Tikz even has some built-in functionality, but your answer seems more general.

                – Faekynn
                7 hours ago












              • Thanks @Faekynn ... Happy TeXing.

                – koleygr
                7 hours ago

















              • The explanation helps me to understand it, thanks! Tikz even has some built-in functionality, but your answer seems more general.

                – Faekynn
                7 hours ago












              • Thanks @Faekynn ... Happy TeXing.

                – koleygr
                7 hours ago
















              The explanation helps me to understand it, thanks! Tikz even has some built-in functionality, but your answer seems more general.

              – Faekynn
              7 hours ago






              The explanation helps me to understand it, thanks! Tikz even has some built-in functionality, but your answer seems more general.

              – Faekynn
              7 hours ago














              Thanks @Faekynn ... Happy TeXing.

              – koleygr
              7 hours ago





              Thanks @Faekynn ... Happy TeXing.

              – koleygr
              7 hours ago













              2















              Actually, TikZ/pgf offers a quite simple solution.
              For me, the standard behavior is completely unexpected.



              %!TEX program = lualatex
              documentclass[tikz]standalone

              begindocument

              pgfmathsetmacrosumk0

              foreach k [remember=sumk as sumk] in 1,2,...,10
              pgfmathsetmacrosumkk+sumk
              directlua print(k,sumk)


              enddocument





              share|improve this answer































                2















                Actually, TikZ/pgf offers a quite simple solution.
                For me, the standard behavior is completely unexpected.



                %!TEX program = lualatex
                documentclass[tikz]standalone

                begindocument

                pgfmathsetmacrosumk0

                foreach k [remember=sumk as sumk] in 1,2,...,10
                pgfmathsetmacrosumkk+sumk
                directlua print(k,sumk)


                enddocument





                share|improve this answer





























                  2














                  2










                  2









                  Actually, TikZ/pgf offers a quite simple solution.
                  For me, the standard behavior is completely unexpected.



                  %!TEX program = lualatex
                  documentclass[tikz]standalone

                  begindocument

                  pgfmathsetmacrosumk0

                  foreach k [remember=sumk as sumk] in 1,2,...,10
                  pgfmathsetmacrosumkk+sumk
                  directlua print(k,sumk)


                  enddocument





                  share|improve this answer















                  Actually, TikZ/pgf offers a quite simple solution.
                  For me, the standard behavior is completely unexpected.



                  %!TEX program = lualatex
                  documentclass[tikz]standalone

                  begindocument

                  pgfmathsetmacrosumk0

                  foreach k [remember=sumk as sumk] in 1,2,...,10
                  pgfmathsetmacrosumkk+sumk
                  directlua print(k,sumk)


                  enddocument






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 7 hours ago

























                  answered 7 hours ago









                  FaekynnFaekynn

                  1,2471 gold badge9 silver badges29 bronze badges




                  1,2471 gold badge9 silver badges29 bronze badges
























                      1















                      It is actually a common behavior of foreach compared to e.g. pgfplotsinvokeforeach provided by pgfplots. You can get more information about this behavior and some examples in pgf-tikz manual § 7 Utilities and pgfplots manual § 8.1 Utility Commands.
                      I think it is because foreach introduces scopes, while pgfplotsinvokeforeach does not, so the sumk variable is not expanded.



                      The following example produces the desired output



                      %!TEX program = lualatex
                      documentclass[tikz]standalone
                      usepackagepgfplots
                      begindocument

                      pgfmathsetmacrosumk0

                      pgfplotsinvokeforeach1,2,...,10
                      pgfmathsetmacrosumk#1+sumk
                      directlua print(#1,sumk)


                      enddocument





                      share|improve this answer





























                        1















                        It is actually a common behavior of foreach compared to e.g. pgfplotsinvokeforeach provided by pgfplots. You can get more information about this behavior and some examples in pgf-tikz manual § 7 Utilities and pgfplots manual § 8.1 Utility Commands.
                        I think it is because foreach introduces scopes, while pgfplotsinvokeforeach does not, so the sumk variable is not expanded.



                        The following example produces the desired output



                        %!TEX program = lualatex
                        documentclass[tikz]standalone
                        usepackagepgfplots
                        begindocument

                        pgfmathsetmacrosumk0

                        pgfplotsinvokeforeach1,2,...,10
                        pgfmathsetmacrosumk#1+sumk
                        directlua print(#1,sumk)


                        enddocument





                        share|improve this answer



























                          1














                          1










                          1









                          It is actually a common behavior of foreach compared to e.g. pgfplotsinvokeforeach provided by pgfplots. You can get more information about this behavior and some examples in pgf-tikz manual § 7 Utilities and pgfplots manual § 8.1 Utility Commands.
                          I think it is because foreach introduces scopes, while pgfplotsinvokeforeach does not, so the sumk variable is not expanded.



                          The following example produces the desired output



                          %!TEX program = lualatex
                          documentclass[tikz]standalone
                          usepackagepgfplots
                          begindocument

                          pgfmathsetmacrosumk0

                          pgfplotsinvokeforeach1,2,...,10
                          pgfmathsetmacrosumk#1+sumk
                          directlua print(#1,sumk)


                          enddocument





                          share|improve this answer













                          It is actually a common behavior of foreach compared to e.g. pgfplotsinvokeforeach provided by pgfplots. You can get more information about this behavior and some examples in pgf-tikz manual § 7 Utilities and pgfplots manual § 8.1 Utility Commands.
                          I think it is because foreach introduces scopes, while pgfplotsinvokeforeach does not, so the sumk variable is not expanded.



                          The following example produces the desired output



                          %!TEX program = lualatex
                          documentclass[tikz]standalone
                          usepackagepgfplots
                          begindocument

                          pgfmathsetmacrosumk0

                          pgfplotsinvokeforeach1,2,...,10
                          pgfmathsetmacrosumk#1+sumk
                          directlua print(#1,sumk)


                          enddocument






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 7 hours ago









                          BambOoBambOo

                          3,2121 gold badge5 silver badges29 bronze badges




                          3,2121 gold badge5 silver badges29 bronze badges






























                              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%2f505421%2fresult-of-pgmathsetmacro-is-lost-in-loop-iterations%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

                              Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                              Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                              François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480