First index is not integer using foreach loop from 0 The Next CEO of Stack OverflowRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?Numerical conditional within tikz keys?Why do I get an extra white page before my TikZ picture?Input/Output Nodes - Specification and Description LanguageTikZ: Drawing an arc from an intersection to an intersectionHow to prevent rounded and duplicated tick labels in pgfplots with fixed precision?Drawing rectilinear curves in Tikz, aka an Etch-a-Sketch drawingHow to draw a square and its diagonals with arrows?Compound Pathway Problem with holes

Horror movie/show or scene where a horse creature opens its mouth really wide and devours a man in a stables

Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?

How to make a software documentation "officially" citable?

Should I tutor a student who I know has cheated on their homework?

WOW air has ceased operation, can I get my tickets refunded?

Trouble understanding the speech of overseas colleagues

Opposite of a diet

Why here is plural "We went to the movies last night."

How long to clear the 'suck zone' of a turbofan after start is initiated?

How do I go from 300 unfinished/half written blog posts, to published posts?

How can I get through very long and very dry, but also very useful technical documents when learning a new tool?

How do I construct this japanese bowl?

Natural language into sentence logic

Anatomically Correct Strange Women In Ponds Distributing Swords

Can a caster that cast Polymorph on themselves stop concentrating at any point even if their Int is low?

Increase performance creating Mandelbrot set in python

How to write the block matrix in LaTex?

Why does C# sound extremely flat when saxophone is tuned to G?

Whats the best way to handle refactoring a big file?

Why didn't Theresa May consult with Parliament before negotiating a deal with the EU?

% symbol leads to superlong (forever?) compilations

How can I open an app using Terminal?

Why do professional authors make "consistency" mistakes? And how to avoid them?

Fastest way to shutdown Ubuntu Mate 18.10



First index is not integer using foreach loop from 0



The Next CEO of Stack OverflowRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?Numerical conditional within tikz keys?Why do I get an extra white page before my TikZ picture?Input/Output Nodes - Specification and Description LanguageTikZ: Drawing an arc from an intersection to an intersectionHow to prevent rounded and duplicated tick labels in pgfplots with fixed precision?Drawing rectilinear curves in Tikz, aka an Etch-a-Sketch drawingHow to draw a square and its diagonals with arrows?Compound Pathway Problem with holes










7















I am trying to draw a tree using tikzpicture like this:



documentclassarticle
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture
tikzstylenode=[circle, fill=blue!25, minimum size=0.1 cm];
foreach ilayer in 0,...,3
tikzmath nnodes = 3 ^ ilayer;
tikzmath leftnum = 1 - floor(nnodes / 2) - 1;
tikzmath rightnum = nnodes - floor(nnodes / 2) - 1;
foreach isibling in leftnum,...,rightnum
tikzmath d = 3 ^ (- ilayer) * 15;
tikzmath x = isibling * d;
tikzmath y = - ilayer * 2;
node[node] (node_ilayer_isibling) at (x cm, y cm) isibling;


endtikzpicture
enddocument


I get the tree like the following picture. The texts are nodes' isibling within each layer. Most nodes are integers, but all leftmost nodes are not.



enter image description here










share|improve this question









New contributor




landings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1





    Welcome to TeX.SX. When you post a question, please provide a "Minimal Working Example" (MWE) that starts with documentclass, includes all relevant usepackage commands, ends with enddocument and compiles without errors, even if it does not produce your desired output.

    – Sandy G
    yesterday















7















I am trying to draw a tree using tikzpicture like this:



documentclassarticle
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture
tikzstylenode=[circle, fill=blue!25, minimum size=0.1 cm];
foreach ilayer in 0,...,3
tikzmath nnodes = 3 ^ ilayer;
tikzmath leftnum = 1 - floor(nnodes / 2) - 1;
tikzmath rightnum = nnodes - floor(nnodes / 2) - 1;
foreach isibling in leftnum,...,rightnum
tikzmath d = 3 ^ (- ilayer) * 15;
tikzmath x = isibling * d;
tikzmath y = - ilayer * 2;
node[node] (node_ilayer_isibling) at (x cm, y cm) isibling;


endtikzpicture
enddocument


I get the tree like the following picture. The texts are nodes' isibling within each layer. Most nodes are integers, but all leftmost nodes are not.



enter image description here










share|improve this question









New contributor




landings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1





    Welcome to TeX.SX. When you post a question, please provide a "Minimal Working Example" (MWE) that starts with documentclass, includes all relevant usepackage commands, ends with enddocument and compiles without errors, even if it does not produce your desired output.

    – Sandy G
    yesterday













7












7








7


1






I am trying to draw a tree using tikzpicture like this:



documentclassarticle
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture
tikzstylenode=[circle, fill=blue!25, minimum size=0.1 cm];
foreach ilayer in 0,...,3
tikzmath nnodes = 3 ^ ilayer;
tikzmath leftnum = 1 - floor(nnodes / 2) - 1;
tikzmath rightnum = nnodes - floor(nnodes / 2) - 1;
foreach isibling in leftnum,...,rightnum
tikzmath d = 3 ^ (- ilayer) * 15;
tikzmath x = isibling * d;
tikzmath y = - ilayer * 2;
node[node] (node_ilayer_isibling) at (x cm, y cm) isibling;


endtikzpicture
enddocument


I get the tree like the following picture. The texts are nodes' isibling within each layer. Most nodes are integers, but all leftmost nodes are not.



enter image description here










share|improve this question









New contributor




landings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I am trying to draw a tree using tikzpicture like this:



documentclassarticle
usepackagetikz
usetikzlibrarymath
begindocument
begintikzpicture
tikzstylenode=[circle, fill=blue!25, minimum size=0.1 cm];
foreach ilayer in 0,...,3
tikzmath nnodes = 3 ^ ilayer;
tikzmath leftnum = 1 - floor(nnodes / 2) - 1;
tikzmath rightnum = nnodes - floor(nnodes / 2) - 1;
foreach isibling in leftnum,...,rightnum
tikzmath d = 3 ^ (- ilayer) * 15;
tikzmath x = isibling * d;
tikzmath y = - ilayer * 2;
node[node] (node_ilayer_isibling) at (x cm, y cm) isibling;


endtikzpicture
enddocument


I get the tree like the following picture. The texts are nodes' isibling within each layer. Most nodes are integers, but all leftmost nodes are not.



enter image description here







tikz-pgf foreach






share|improve this question









New contributor




landings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




landings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 22 hours ago







landings













New contributor




landings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









landingslandings

414




414




New contributor




landings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





landings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






landings is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1





    Welcome to TeX.SX. When you post a question, please provide a "Minimal Working Example" (MWE) that starts with documentclass, includes all relevant usepackage commands, ends with enddocument and compiles without errors, even if it does not produce your desired output.

    – Sandy G
    yesterday












  • 1





    Welcome to TeX.SX. When you post a question, please provide a "Minimal Working Example" (MWE) that starts with documentclass, includes all relevant usepackage commands, ends with enddocument and compiles without errors, even if it does not produce your desired output.

    – Sandy G
    yesterday







1




1





Welcome to TeX.SX. When you post a question, please provide a "Minimal Working Example" (MWE) that starts with documentclass, includes all relevant usepackage commands, ends with enddocument and compiles without errors, even if it does not produce your desired output.

– Sandy G
yesterday





Welcome to TeX.SX. When you post a question, please provide a "Minimal Working Example" (MWE) that starts with documentclass, includes all relevant usepackage commands, ends with enddocument and compiles without errors, even if it does not produce your desired output.

– Sandy G
yesterday










2 Answers
2






active

oldest

votes


















10














You could just tell TikZ explicitly that you want an integer.



documentclass[tikz,border=3.14mm]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer in 0,...,3
tikzmath nnodes = 3 ^ ilayer;
tikzmath leftnum = int(1 - floor(nnodes / 2) - 1);
tikzmath rightnum = nnodes - floor(nnodes / 2) - 1;
foreach isibling in leftnum,...,rightnum
tikzmath d = 3 ^ (- ilayer) * 15;
tikzmath x = isibling * d;
tikzmath y = - ilayer * 2;
node[mynode] (node_ilayer_isibling) at (x cm, y cm) isibling;


endtikzpicture
enddocument


enter image description here



Or



documentclass[tikz,border=3.14mm]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer in 0,...,3
tikzmath int nnodes,leftnum,rightnum;
nnodes = 3 ^ ilayer;
leftnum = 1 - floor(nnodes / 2) - 1;
rightnum = nnodes - floor(nnodes / 2) - 1;
foreach isibling in leftnum,...,rightnum
tikzmath d = 3 ^ (- ilayer) * 15;
x = isibling * d;
y = - ilayer * 2;
node[mynode] (node_ilayer_isibling) at (x cm, y cm) isibling;


endtikzpicture
enddocument


In principle you do not need the math library here.



documentclass[tikz,border=3.14mm]standalone
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer [evaluate=ilayer as nnodes using int(3 ^ ilayer),
evaluate=nnodes as leftnum using int(1 - floor(nnodes / 2) - 1),
evaluate=nnodes as rightnum using int(nnodes - floor(nnodes / 2) - 1)]
in 0,...,3
foreach isibling
[evaluate=ilayer as d using 3 ^ (- ilayer) * 15,
evaluate=isibling as x using isibling * d,
evaluate=ilayer as y using - ilayer * 2]
in leftnum,...,rightnum
node[mynode] (node_ilayer_isibling) at (x cm, y cm)
isibling;


endtikzpicture
enddocument


enter image description here






share|improve this answer

























  • Thanks a lot. I finally get where the problem starts. Why 1 - floor(nnodes / 2) - 1 can be non-integer? Even 1 - int(nnodes / 2) - 1 is problematic.

    – landings
    yesterday






  • 1





    @landings It is due to the way foreach is implemented, internally TikZ computes with dimensions and this can lead to slight inconsistencies. So it is better to wrap the full expression into int.

    – marmot
    yesterday


















8














As @marmot said you do not need tikzmath here, but if you use it you can do it in more efficient way :



  • You can have a single tikzmath command with loops inside it.

  • You can declare your integer variables as int so you do not need to do int() afterward.

  • As nnodes is odd you do not need separate rightnum and leftnum as rightnum = - leftnum;

  • Why you use 1-floor(nnodes/2)-1 in place of -floor(nnodes/2) ?

  • The value d can be calculated in the outer loop.

  • Instead of using x=isibling*d you can say [x=d cm] and then use isibling asx. And in the same way y can be replaced by ilayer using [y=-2cm].

So here is my proposal :



documentclass[tikz,border=7pt]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzstylenode=[circle, fill=blue!25, minimum size=0.1 cm];
tikzmath
int ilayer,nnodes,rightnum,isibling;
nnodes = 1;
for ilayer in 0,...,3
rightnum = (nnodes-1)/2;
d = 15/nnodes;
for isibling in -rightnum,...,rightnum

path[x=d cm,y=-2cm]
node[node] (node_ilayer_isibling) at (isibling, ilayer) isibling;
;
;
nnodes = 3*nnodes;
;

endtikzpicture
enddocument


enter image description here






share|improve this answer




















  • 1





    Thanks for your help. I am new to LaTeX and need more practice. I didn't know I could loop and draw things just inside tikzmath code.

    – landings
    yesterday






  • 2





    Don't worry, even some experts don't know how to use tikzmath ;)

    – Kpym
    yesterday











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



);






landings is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f481622%2ffirst-index-is-not-integer-using-foreach-loop-from-0%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









10














You could just tell TikZ explicitly that you want an integer.



documentclass[tikz,border=3.14mm]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer in 0,...,3
tikzmath nnodes = 3 ^ ilayer;
tikzmath leftnum = int(1 - floor(nnodes / 2) - 1);
tikzmath rightnum = nnodes - floor(nnodes / 2) - 1;
foreach isibling in leftnum,...,rightnum
tikzmath d = 3 ^ (- ilayer) * 15;
tikzmath x = isibling * d;
tikzmath y = - ilayer * 2;
node[mynode] (node_ilayer_isibling) at (x cm, y cm) isibling;


endtikzpicture
enddocument


enter image description here



Or



documentclass[tikz,border=3.14mm]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer in 0,...,3
tikzmath int nnodes,leftnum,rightnum;
nnodes = 3 ^ ilayer;
leftnum = 1 - floor(nnodes / 2) - 1;
rightnum = nnodes - floor(nnodes / 2) - 1;
foreach isibling in leftnum,...,rightnum
tikzmath d = 3 ^ (- ilayer) * 15;
x = isibling * d;
y = - ilayer * 2;
node[mynode] (node_ilayer_isibling) at (x cm, y cm) isibling;


endtikzpicture
enddocument


In principle you do not need the math library here.



documentclass[tikz,border=3.14mm]standalone
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer [evaluate=ilayer as nnodes using int(3 ^ ilayer),
evaluate=nnodes as leftnum using int(1 - floor(nnodes / 2) - 1),
evaluate=nnodes as rightnum using int(nnodes - floor(nnodes / 2) - 1)]
in 0,...,3
foreach isibling
[evaluate=ilayer as d using 3 ^ (- ilayer) * 15,
evaluate=isibling as x using isibling * d,
evaluate=ilayer as y using - ilayer * 2]
in leftnum,...,rightnum
node[mynode] (node_ilayer_isibling) at (x cm, y cm)
isibling;


endtikzpicture
enddocument


enter image description here






share|improve this answer

























  • Thanks a lot. I finally get where the problem starts. Why 1 - floor(nnodes / 2) - 1 can be non-integer? Even 1 - int(nnodes / 2) - 1 is problematic.

    – landings
    yesterday






  • 1





    @landings It is due to the way foreach is implemented, internally TikZ computes with dimensions and this can lead to slight inconsistencies. So it is better to wrap the full expression into int.

    – marmot
    yesterday















10














You could just tell TikZ explicitly that you want an integer.



documentclass[tikz,border=3.14mm]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer in 0,...,3
tikzmath nnodes = 3 ^ ilayer;
tikzmath leftnum = int(1 - floor(nnodes / 2) - 1);
tikzmath rightnum = nnodes - floor(nnodes / 2) - 1;
foreach isibling in leftnum,...,rightnum
tikzmath d = 3 ^ (- ilayer) * 15;
tikzmath x = isibling * d;
tikzmath y = - ilayer * 2;
node[mynode] (node_ilayer_isibling) at (x cm, y cm) isibling;


endtikzpicture
enddocument


enter image description here



Or



documentclass[tikz,border=3.14mm]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer in 0,...,3
tikzmath int nnodes,leftnum,rightnum;
nnodes = 3 ^ ilayer;
leftnum = 1 - floor(nnodes / 2) - 1;
rightnum = nnodes - floor(nnodes / 2) - 1;
foreach isibling in leftnum,...,rightnum
tikzmath d = 3 ^ (- ilayer) * 15;
x = isibling * d;
y = - ilayer * 2;
node[mynode] (node_ilayer_isibling) at (x cm, y cm) isibling;


endtikzpicture
enddocument


In principle you do not need the math library here.



documentclass[tikz,border=3.14mm]standalone
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer [evaluate=ilayer as nnodes using int(3 ^ ilayer),
evaluate=nnodes as leftnum using int(1 - floor(nnodes / 2) - 1),
evaluate=nnodes as rightnum using int(nnodes - floor(nnodes / 2) - 1)]
in 0,...,3
foreach isibling
[evaluate=ilayer as d using 3 ^ (- ilayer) * 15,
evaluate=isibling as x using isibling * d,
evaluate=ilayer as y using - ilayer * 2]
in leftnum,...,rightnum
node[mynode] (node_ilayer_isibling) at (x cm, y cm)
isibling;


endtikzpicture
enddocument


enter image description here






share|improve this answer

























  • Thanks a lot. I finally get where the problem starts. Why 1 - floor(nnodes / 2) - 1 can be non-integer? Even 1 - int(nnodes / 2) - 1 is problematic.

    – landings
    yesterday






  • 1





    @landings It is due to the way foreach is implemented, internally TikZ computes with dimensions and this can lead to slight inconsistencies. So it is better to wrap the full expression into int.

    – marmot
    yesterday













10












10








10







You could just tell TikZ explicitly that you want an integer.



documentclass[tikz,border=3.14mm]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer in 0,...,3
tikzmath nnodes = 3 ^ ilayer;
tikzmath leftnum = int(1 - floor(nnodes / 2) - 1);
tikzmath rightnum = nnodes - floor(nnodes / 2) - 1;
foreach isibling in leftnum,...,rightnum
tikzmath d = 3 ^ (- ilayer) * 15;
tikzmath x = isibling * d;
tikzmath y = - ilayer * 2;
node[mynode] (node_ilayer_isibling) at (x cm, y cm) isibling;


endtikzpicture
enddocument


enter image description here



Or



documentclass[tikz,border=3.14mm]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer in 0,...,3
tikzmath int nnodes,leftnum,rightnum;
nnodes = 3 ^ ilayer;
leftnum = 1 - floor(nnodes / 2) - 1;
rightnum = nnodes - floor(nnodes / 2) - 1;
foreach isibling in leftnum,...,rightnum
tikzmath d = 3 ^ (- ilayer) * 15;
x = isibling * d;
y = - ilayer * 2;
node[mynode] (node_ilayer_isibling) at (x cm, y cm) isibling;


endtikzpicture
enddocument


In principle you do not need the math library here.



documentclass[tikz,border=3.14mm]standalone
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer [evaluate=ilayer as nnodes using int(3 ^ ilayer),
evaluate=nnodes as leftnum using int(1 - floor(nnodes / 2) - 1),
evaluate=nnodes as rightnum using int(nnodes - floor(nnodes / 2) - 1)]
in 0,...,3
foreach isibling
[evaluate=ilayer as d using 3 ^ (- ilayer) * 15,
evaluate=isibling as x using isibling * d,
evaluate=ilayer as y using - ilayer * 2]
in leftnum,...,rightnum
node[mynode] (node_ilayer_isibling) at (x cm, y cm)
isibling;


endtikzpicture
enddocument


enter image description here






share|improve this answer















You could just tell TikZ explicitly that you want an integer.



documentclass[tikz,border=3.14mm]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer in 0,...,3
tikzmath nnodes = 3 ^ ilayer;
tikzmath leftnum = int(1 - floor(nnodes / 2) - 1);
tikzmath rightnum = nnodes - floor(nnodes / 2) - 1;
foreach isibling in leftnum,...,rightnum
tikzmath d = 3 ^ (- ilayer) * 15;
tikzmath x = isibling * d;
tikzmath y = - ilayer * 2;
node[mynode] (node_ilayer_isibling) at (x cm, y cm) isibling;


endtikzpicture
enddocument


enter image description here



Or



documentclass[tikz,border=3.14mm]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer in 0,...,3
tikzmath int nnodes,leftnum,rightnum;
nnodes = 3 ^ ilayer;
leftnum = 1 - floor(nnodes / 2) - 1;
rightnum = nnodes - floor(nnodes / 2) - 1;
foreach isibling in leftnum,...,rightnum
tikzmath d = 3 ^ (- ilayer) * 15;
x = isibling * d;
y = - ilayer * 2;
node[mynode] (node_ilayer_isibling) at (x cm, y cm) isibling;


endtikzpicture
enddocument


In principle you do not need the math library here.



documentclass[tikz,border=3.14mm]standalone
begindocument
begintikzpicture
tikzsetmynode/.style=circle, fill=blue!25, minimum size=0.1 cm
foreach ilayer [evaluate=ilayer as nnodes using int(3 ^ ilayer),
evaluate=nnodes as leftnum using int(1 - floor(nnodes / 2) - 1),
evaluate=nnodes as rightnum using int(nnodes - floor(nnodes / 2) - 1)]
in 0,...,3
foreach isibling
[evaluate=ilayer as d using 3 ^ (- ilayer) * 15,
evaluate=isibling as x using isibling * d,
evaluate=ilayer as y using - ilayer * 2]
in leftnum,...,rightnum
node[mynode] (node_ilayer_isibling) at (x cm, y cm)
isibling;


endtikzpicture
enddocument


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









marmotmarmot

112k5144270




112k5144270












  • Thanks a lot. I finally get where the problem starts. Why 1 - floor(nnodes / 2) - 1 can be non-integer? Even 1 - int(nnodes / 2) - 1 is problematic.

    – landings
    yesterday






  • 1





    @landings It is due to the way foreach is implemented, internally TikZ computes with dimensions and this can lead to slight inconsistencies. So it is better to wrap the full expression into int.

    – marmot
    yesterday

















  • Thanks a lot. I finally get where the problem starts. Why 1 - floor(nnodes / 2) - 1 can be non-integer? Even 1 - int(nnodes / 2) - 1 is problematic.

    – landings
    yesterday






  • 1





    @landings It is due to the way foreach is implemented, internally TikZ computes with dimensions and this can lead to slight inconsistencies. So it is better to wrap the full expression into int.

    – marmot
    yesterday
















Thanks a lot. I finally get where the problem starts. Why 1 - floor(nnodes / 2) - 1 can be non-integer? Even 1 - int(nnodes / 2) - 1 is problematic.

– landings
yesterday





Thanks a lot. I finally get where the problem starts. Why 1 - floor(nnodes / 2) - 1 can be non-integer? Even 1 - int(nnodes / 2) - 1 is problematic.

– landings
yesterday




1




1





@landings It is due to the way foreach is implemented, internally TikZ computes with dimensions and this can lead to slight inconsistencies. So it is better to wrap the full expression into int.

– marmot
yesterday





@landings It is due to the way foreach is implemented, internally TikZ computes with dimensions and this can lead to slight inconsistencies. So it is better to wrap the full expression into int.

– marmot
yesterday











8














As @marmot said you do not need tikzmath here, but if you use it you can do it in more efficient way :



  • You can have a single tikzmath command with loops inside it.

  • You can declare your integer variables as int so you do not need to do int() afterward.

  • As nnodes is odd you do not need separate rightnum and leftnum as rightnum = - leftnum;

  • Why you use 1-floor(nnodes/2)-1 in place of -floor(nnodes/2) ?

  • The value d can be calculated in the outer loop.

  • Instead of using x=isibling*d you can say [x=d cm] and then use isibling asx. And in the same way y can be replaced by ilayer using [y=-2cm].

So here is my proposal :



documentclass[tikz,border=7pt]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzstylenode=[circle, fill=blue!25, minimum size=0.1 cm];
tikzmath
int ilayer,nnodes,rightnum,isibling;
nnodes = 1;
for ilayer in 0,...,3
rightnum = (nnodes-1)/2;
d = 15/nnodes;
for isibling in -rightnum,...,rightnum

path[x=d cm,y=-2cm]
node[node] (node_ilayer_isibling) at (isibling, ilayer) isibling;
;
;
nnodes = 3*nnodes;
;

endtikzpicture
enddocument


enter image description here






share|improve this answer




















  • 1





    Thanks for your help. I am new to LaTeX and need more practice. I didn't know I could loop and draw things just inside tikzmath code.

    – landings
    yesterday






  • 2





    Don't worry, even some experts don't know how to use tikzmath ;)

    – Kpym
    yesterday















8














As @marmot said you do not need tikzmath here, but if you use it you can do it in more efficient way :



  • You can have a single tikzmath command with loops inside it.

  • You can declare your integer variables as int so you do not need to do int() afterward.

  • As nnodes is odd you do not need separate rightnum and leftnum as rightnum = - leftnum;

  • Why you use 1-floor(nnodes/2)-1 in place of -floor(nnodes/2) ?

  • The value d can be calculated in the outer loop.

  • Instead of using x=isibling*d you can say [x=d cm] and then use isibling asx. And in the same way y can be replaced by ilayer using [y=-2cm].

So here is my proposal :



documentclass[tikz,border=7pt]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzstylenode=[circle, fill=blue!25, minimum size=0.1 cm];
tikzmath
int ilayer,nnodes,rightnum,isibling;
nnodes = 1;
for ilayer in 0,...,3
rightnum = (nnodes-1)/2;
d = 15/nnodes;
for isibling in -rightnum,...,rightnum

path[x=d cm,y=-2cm]
node[node] (node_ilayer_isibling) at (isibling, ilayer) isibling;
;
;
nnodes = 3*nnodes;
;

endtikzpicture
enddocument


enter image description here






share|improve this answer




















  • 1





    Thanks for your help. I am new to LaTeX and need more practice. I didn't know I could loop and draw things just inside tikzmath code.

    – landings
    yesterday






  • 2





    Don't worry, even some experts don't know how to use tikzmath ;)

    – Kpym
    yesterday













8












8








8







As @marmot said you do not need tikzmath here, but if you use it you can do it in more efficient way :



  • You can have a single tikzmath command with loops inside it.

  • You can declare your integer variables as int so you do not need to do int() afterward.

  • As nnodes is odd you do not need separate rightnum and leftnum as rightnum = - leftnum;

  • Why you use 1-floor(nnodes/2)-1 in place of -floor(nnodes/2) ?

  • The value d can be calculated in the outer loop.

  • Instead of using x=isibling*d you can say [x=d cm] and then use isibling asx. And in the same way y can be replaced by ilayer using [y=-2cm].

So here is my proposal :



documentclass[tikz,border=7pt]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzstylenode=[circle, fill=blue!25, minimum size=0.1 cm];
tikzmath
int ilayer,nnodes,rightnum,isibling;
nnodes = 1;
for ilayer in 0,...,3
rightnum = (nnodes-1)/2;
d = 15/nnodes;
for isibling in -rightnum,...,rightnum

path[x=d cm,y=-2cm]
node[node] (node_ilayer_isibling) at (isibling, ilayer) isibling;
;
;
nnodes = 3*nnodes;
;

endtikzpicture
enddocument


enter image description here






share|improve this answer















As @marmot said you do not need tikzmath here, but if you use it you can do it in more efficient way :



  • You can have a single tikzmath command with loops inside it.

  • You can declare your integer variables as int so you do not need to do int() afterward.

  • As nnodes is odd you do not need separate rightnum and leftnum as rightnum = - leftnum;

  • Why you use 1-floor(nnodes/2)-1 in place of -floor(nnodes/2) ?

  • The value d can be calculated in the outer loop.

  • Instead of using x=isibling*d you can say [x=d cm] and then use isibling asx. And in the same way y can be replaced by ilayer using [y=-2cm].

So here is my proposal :



documentclass[tikz,border=7pt]standalone
usetikzlibrarymath
begindocument
begintikzpicture
tikzstylenode=[circle, fill=blue!25, minimum size=0.1 cm];
tikzmath
int ilayer,nnodes,rightnum,isibling;
nnodes = 1;
for ilayer in 0,...,3
rightnum = (nnodes-1)/2;
d = 15/nnodes;
for isibling in -rightnum,...,rightnum

path[x=d cm,y=-2cm]
node[node] (node_ilayer_isibling) at (isibling, ilayer) isibling;
;
;
nnodes = 3*nnodes;
;

endtikzpicture
enddocument


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









KpymKpym

17.2k24090




17.2k24090







  • 1





    Thanks for your help. I am new to LaTeX and need more practice. I didn't know I could loop and draw things just inside tikzmath code.

    – landings
    yesterday






  • 2





    Don't worry, even some experts don't know how to use tikzmath ;)

    – Kpym
    yesterday












  • 1





    Thanks for your help. I am new to LaTeX and need more practice. I didn't know I could loop and draw things just inside tikzmath code.

    – landings
    yesterday






  • 2





    Don't worry, even some experts don't know how to use tikzmath ;)

    – Kpym
    yesterday







1




1





Thanks for your help. I am new to LaTeX and need more practice. I didn't know I could loop and draw things just inside tikzmath code.

– landings
yesterday





Thanks for your help. I am new to LaTeX and need more practice. I didn't know I could loop and draw things just inside tikzmath code.

– landings
yesterday




2




2





Don't worry, even some experts don't know how to use tikzmath ;)

– Kpym
yesterday





Don't worry, even some experts don't know how to use tikzmath ;)

– Kpym
yesterday










landings is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















landings is a new contributor. Be nice, and check out our Code of Conduct.












landings is a new contributor. Be nice, and check out our Code of Conduct.











landings is a new contributor. Be nice, and check out our Code of Conduct.














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%2f481622%2ffirst-index-is-not-integer-using-foreach-loop-from-0%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

getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.