First index is not integer using foreach loop from 0Rotate 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
How does this kind of structure made?
English - Acceptable use of parentheses in an author's name
What does "sardine box" mean?
How can I solve for the intersection points of two ellipses?
Can a fight scene, component-wise, be too complex and complicated?
What are the advantages and disadvantages of Wand of Cure Light Wounds and Wand of Infernal Healing compared to each other?
A tool to replace all words with antonyms
How should an administrative assistant reply to student addressing them as "Professor" or "Doctor"?
Write an interpreter for *
Is it incorrect to write "I rate this book a 3 out of 4 stars?"
Help evaluating integral (anything simple that I am missing?)
Best gun to modify into a monsterhunter weapon?
How to mark beverage cans in a cooler for a blind person?
Why are Gatwick's runways too close together?
During the Space Shuttle Columbia Disaster of 2003, Why Did The Flight Director Say, "Lock the doors."?
Is Calculus necessary for computer science student?
How can you evade tax by getting employment income just in equity, then using this equity as collateral to take out loan?
Y2K... in 2019?
Dropdowns & Chevrons for Right to Left languages
Visa National - No Exit Stamp From France on Return to the UK
How does 'AND' distribute over 'OR' (Set Theory)?
Christian apologetics regarding the killing of innocent children during the Genesis flood
Was the 2019 Lion King film made through motion capture?
Russian equivalents of "We would never hear the end of it"
First index is not integer using foreach loop from 0
Rotate 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
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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.
tikz-pgf foreach
add a comment |
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.
tikz-pgf foreach
1
Welcome to TeX.SX. When you post a question, please provide a "Minimal Working Example" (MWE) that starts withdocumentclass
, includes all relevantusepackage
commands, ends withenddocument
and compiles without errors, even if it does not produce your desired output.
– Sandy G
Mar 27 at 14:32
add a comment |
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.
tikz-pgf foreach
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.
tikz-pgf foreach
tikz-pgf foreach
edited Mar 28 at 4:47
landings
asked Mar 27 at 4:10
landingslandings
364 bronze badges
364 bronze badges
1
Welcome to TeX.SX. When you post a question, please provide a "Minimal Working Example" (MWE) that starts withdocumentclass
, includes all relevantusepackage
commands, ends withenddocument
and compiles without errors, even if it does not produce your desired output.
– Sandy G
Mar 27 at 14:32
add a comment |
1
Welcome to TeX.SX. When you post a question, please provide a "Minimal Working Example" (MWE) that starts withdocumentclass
, includes all relevantusepackage
commands, ends withenddocument
and compiles without errors, even if it does not produce your desired output.
– Sandy G
Mar 27 at 14:32
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
Mar 27 at 14:32
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
Mar 27 at 14:32
add a comment |
2 Answers
2
active
oldest
votes
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
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
Thanks a lot. I finally get where the problem starts. Why1 - floor(nnodes / 2) - 1
can be non-integer? Even1 - int(nnodes / 2) - 1
is problematic.
– landings
Mar 27 at 4:27
1
@landings It is due to the wayforeach
is implemented, internally TikZ computes with dimensions and this can lead to slight inconsistencies. So it is better to wrap the full expression intoint
.
– user121799
Mar 27 at 4:33
add a comment |
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 doint()
afterward. - As
nnodes
is odd you do not need separaterightnum
andleftnum
asrightnum = - 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 useisibling
asx
. And in the same wayy
can be replaced byilayer
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
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
Mar 27 at 8:03
2
Don't worry, even some experts don't know how to usetikzmath
;)
– Kpym
Mar 27 at 9:34
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%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
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
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
Thanks a lot. I finally get where the problem starts. Why1 - floor(nnodes / 2) - 1
can be non-integer? Even1 - int(nnodes / 2) - 1
is problematic.
– landings
Mar 27 at 4:27
1
@landings It is due to the wayforeach
is implemented, internally TikZ computes with dimensions and this can lead to slight inconsistencies. So it is better to wrap the full expression intoint
.
– user121799
Mar 27 at 4:33
add a comment |
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
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
Thanks a lot. I finally get where the problem starts. Why1 - floor(nnodes / 2) - 1
can be non-integer? Even1 - int(nnodes / 2) - 1
is problematic.
– landings
Mar 27 at 4:27
1
@landings It is due to the wayforeach
is implemented, internally TikZ computes with dimensions and this can lead to slight inconsistencies. So it is better to wrap the full expression intoint
.
– user121799
Mar 27 at 4:33
add a comment |
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
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
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
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
edited Mar 27 at 4:29
answered Mar 27 at 4:16
user121799user121799
123k6 gold badges192 silver badges360 bronze badges
123k6 gold badges192 silver badges360 bronze badges
Thanks a lot. I finally get where the problem starts. Why1 - floor(nnodes / 2) - 1
can be non-integer? Even1 - int(nnodes / 2) - 1
is problematic.
– landings
Mar 27 at 4:27
1
@landings It is due to the wayforeach
is implemented, internally TikZ computes with dimensions and this can lead to slight inconsistencies. So it is better to wrap the full expression intoint
.
– user121799
Mar 27 at 4:33
add a comment |
Thanks a lot. I finally get where the problem starts. Why1 - floor(nnodes / 2) - 1
can be non-integer? Even1 - int(nnodes / 2) - 1
is problematic.
– landings
Mar 27 at 4:27
1
@landings It is due to the wayforeach
is implemented, internally TikZ computes with dimensions and this can lead to slight inconsistencies. So it is better to wrap the full expression intoint
.
– user121799
Mar 27 at 4:33
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
Mar 27 at 4:27
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
Mar 27 at 4:27
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
.– user121799
Mar 27 at 4:33
@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
.– user121799
Mar 27 at 4:33
add a comment |
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 doint()
afterward. - As
nnodes
is odd you do not need separaterightnum
andleftnum
asrightnum = - 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 useisibling
asx
. And in the same wayy
can be replaced byilayer
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
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
Mar 27 at 8:03
2
Don't worry, even some experts don't know how to usetikzmath
;)
– Kpym
Mar 27 at 9:34
add a comment |
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 doint()
afterward. - As
nnodes
is odd you do not need separaterightnum
andleftnum
asrightnum = - 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 useisibling
asx
. And in the same wayy
can be replaced byilayer
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
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
Mar 27 at 8:03
2
Don't worry, even some experts don't know how to usetikzmath
;)
– Kpym
Mar 27 at 9:34
add a comment |
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 doint()
afterward. - As
nnodes
is odd you do not need separaterightnum
andleftnum
asrightnum = - 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 useisibling
asx
. And in the same wayy
can be replaced byilayer
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
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 doint()
afterward. - As
nnodes
is odd you do not need separaterightnum
andleftnum
asrightnum = - 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 useisibling
asx
. And in the same wayy
can be replaced byilayer
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
edited Mar 27 at 10:51
answered Mar 27 at 7:04
KpymKpym
18.6k2 gold badges43 silver badges93 bronze badges
18.6k2 gold badges43 silver badges93 bronze badges
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
Mar 27 at 8:03
2
Don't worry, even some experts don't know how to usetikzmath
;)
– Kpym
Mar 27 at 9:34
add a comment |
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
Mar 27 at 8:03
2
Don't worry, even some experts don't know how to usetikzmath
;)
– Kpym
Mar 27 at 9:34
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
Mar 27 at 8:03
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
Mar 27 at 8:03
2
2
Don't worry, even some experts don't know how to use
tikzmath
;)– Kpym
Mar 27 at 9:34
Don't worry, even some experts don't know how to use
tikzmath
;)– Kpym
Mar 27 at 9:34
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%2f481622%2ffirst-index-is-not-integer-using-foreach-loop-from-0%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
1
Welcome to TeX.SX. When you post a question, please provide a "Minimal Working Example" (MWE) that starts with
documentclass
, includes all relevantusepackage
commands, ends withenddocument
and compiles without errors, even if it does not produce your desired output.– Sandy G
Mar 27 at 14:32