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;
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
add a comment |
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
add a comment |
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
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
tikz-pgf pgfmath
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
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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.
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
add a comment |
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
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
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
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered 7 hours ago
BambOoBambOo
3,2121 gold badge5 silver badges29 bronze badges
3,2121 gold badge5 silver badges29 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f505421%2fresult-of-pgmathsetmacro-is-lost-in-loop-iterations%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown