First index is not integer using foreach loop from 0
I am trying to draw a tree using tikzpicture like this:
begin{tikzpicture}
tikzstyle{node}=[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};
}
}
end{tikzpicture}
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
New contributor
add a comment |
I am trying to draw a tree using tikzpicture like this:
begin{tikzpicture}
tikzstyle{node}=[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};
}
}
end{tikzpicture}
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
New contributor
add a comment |
I am trying to draw a tree using tikzpicture like this:
begin{tikzpicture}
tikzstyle{node}=[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};
}
}
end{tikzpicture}
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
New contributor
I am trying to draw a tree using tikzpicture like this:
begin{tikzpicture}
tikzstyle{node}=[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};
}
}
end{tikzpicture}
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
New contributor
New contributor
New contributor
asked 1 hour ago
landingslandings
81
81
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You could just tell TikZ explicitly that you want an integer.
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{math}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
Or
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{math}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
In principle you do not need the math library here.
documentclass[tikz,border=3.14mm]{standalone}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
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
45 mins ago
@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
.
– marmot
39 mins ago
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
});
}
});
landings is a new contributor. Be nice, and check out our Code of Conduct.
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
1 Answer
1
active
oldest
votes
1 Answer
1
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}
usetikzlibrary{math}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
Or
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{math}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
In principle you do not need the math library here.
documentclass[tikz,border=3.14mm]{standalone}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
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
45 mins ago
@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
.
– marmot
39 mins ago
add a comment |
You could just tell TikZ explicitly that you want an integer.
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{math}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
Or
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{math}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
In principle you do not need the math library here.
documentclass[tikz,border=3.14mm]{standalone}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
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
45 mins ago
@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
.
– marmot
39 mins ago
add a comment |
You could just tell TikZ explicitly that you want an integer.
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{math}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
Or
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{math}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
In principle you do not need the math library here.
documentclass[tikz,border=3.14mm]{standalone}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
You could just tell TikZ explicitly that you want an integer.
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{math}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
Or
documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{math}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
In principle you do not need the math library here.
documentclass[tikz,border=3.14mm]{standalone}
begin{document}
begin{tikzpicture}
tikzset{mynode/.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};
}
}
end{tikzpicture}
end{document}
edited 43 mins ago
answered 56 mins ago
marmotmarmot
112k5141267
112k5141267
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
45 mins ago
@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
.
– marmot
39 mins ago
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
45 mins ago
@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
.
– marmot
39 mins ago
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
45 mins ago
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
45 mins ago
@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
39 mins ago
@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
39 mins ago
add a comment |
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.
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.
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