TikZ - Edges not positioned properly when using foreach loop
up vote
5
down vote
favorite
I want to draw a graph like this:

Which I can do well enough with the following code:
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
draw (u_1) -- (u_2);
draw (u_2) -- (u_3);
draw (u_3) -- (u_4);
draw (u_4) -- (u_5);
draw (u_5) -- (u_6);
draw (u_6) -- (u_1);
But I want to be able to draw it with a foreach loop for all of the outer edges instead of doing them one by one, like this:
foreach i in {1,...,6}{
draw let n{j} = {Mod(i,6)+1}
in (u_i) -- (u_n{j});
}
However, when I try and do it this way, I end up with a graph looking like this:

MWE:
documentclass{article}
usepackage{tikz}
usetikzlibrary{calc}
begin{document}
tikz{
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
foreach i in {1,...,6}{
draw let n{j} = {Mod(i,6)+1}
in (u_i) -- (u_n{j});
}
}
end{document}
Is there any way to do this with a foreach loop, without all the edges getting messed up?
tikz-pgf graphs loops
New contributor
jgunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
5
down vote
favorite
I want to draw a graph like this:

Which I can do well enough with the following code:
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
draw (u_1) -- (u_2);
draw (u_2) -- (u_3);
draw (u_3) -- (u_4);
draw (u_4) -- (u_5);
draw (u_5) -- (u_6);
draw (u_6) -- (u_1);
But I want to be able to draw it with a foreach loop for all of the outer edges instead of doing them one by one, like this:
foreach i in {1,...,6}{
draw let n{j} = {Mod(i,6)+1}
in (u_i) -- (u_n{j});
}
However, when I try and do it this way, I end up with a graph looking like this:

MWE:
documentclass{article}
usepackage{tikz}
usetikzlibrary{calc}
begin{document}
tikz{
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
foreach i in {1,...,6}{
draw let n{j} = {Mod(i,6)+1}
in (u_i) -- (u_n{j});
}
}
end{document}
Is there any way to do this with a foreach loop, without all the edges getting messed up?
tikz-pgf graphs loops
New contributor
jgunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I want to draw a graph like this:

Which I can do well enough with the following code:
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
draw (u_1) -- (u_2);
draw (u_2) -- (u_3);
draw (u_3) -- (u_4);
draw (u_4) -- (u_5);
draw (u_5) -- (u_6);
draw (u_6) -- (u_1);
But I want to be able to draw it with a foreach loop for all of the outer edges instead of doing them one by one, like this:
foreach i in {1,...,6}{
draw let n{j} = {Mod(i,6)+1}
in (u_i) -- (u_n{j});
}
However, when I try and do it this way, I end up with a graph looking like this:

MWE:
documentclass{article}
usepackage{tikz}
usetikzlibrary{calc}
begin{document}
tikz{
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
foreach i in {1,...,6}{
draw let n{j} = {Mod(i,6)+1}
in (u_i) -- (u_n{j});
}
}
end{document}
Is there any way to do this with a foreach loop, without all the edges getting messed up?
tikz-pgf graphs loops
New contributor
jgunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I want to draw a graph like this:

Which I can do well enough with the following code:
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
draw (u_1) -- (u_2);
draw (u_2) -- (u_3);
draw (u_3) -- (u_4);
draw (u_4) -- (u_5);
draw (u_5) -- (u_6);
draw (u_6) -- (u_1);
But I want to be able to draw it with a foreach loop for all of the outer edges instead of doing them one by one, like this:
foreach i in {1,...,6}{
draw let n{j} = {Mod(i,6)+1}
in (u_i) -- (u_n{j});
}
However, when I try and do it this way, I end up with a graph looking like this:

MWE:
documentclass{article}
usepackage{tikz}
usetikzlibrary{calc}
begin{document}
tikz{
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
foreach i in {1,...,6}{
draw let n{j} = {Mod(i,6)+1}
in (u_i) -- (u_n{j});
}
}
end{document}
Is there any way to do this with a foreach loop, without all the edges getting messed up?
tikz-pgf graphs loops
tikz-pgf graphs loops
New contributor
jgunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
jgunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Nov 12 at 23:31
siracusa
4,41411127
4,41411127
New contributor
jgunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Nov 12 at 22:57
jgunter
283
283
New contributor
jgunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
jgunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
jgunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
Welcome to TeX.SE! You get floating point numbers like 5.0 from your computation, where .0 gets interpreted as the east anchor. So you basically need to wrap the result in int, and you can do this without calc like this.
documentclass[tikz,border=3.14mm]{standalone}
begin{document}
begin{tikzpicture}
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
foreach i [evaluate=i as j using {int(mod(i,6)+1)}] in {1,...,6}
{
draw (u_i) -- (u_j);
}
end{tikzpicture}
end{document}

This also results from you MWE if you add int:
documentclass{article}
usepackage{tikz}
usetikzlibrary{calc}
begin{document}
tikz{
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
foreach i in {1,...,6}{
draw let n{j} = {int(Mod(i,6)+1)}
in (u_i) -- (u_n{j});
}
}
end{document}
However, IMHO this is slightly more complicated than the version without calc.
1
Another solution for the second loop without calc:foreach i [remember=i as lasti (initially 6)] in {1,...,6} draw (u_lasti) -- (u_i);
– Ignasi
Nov 13 at 9:58
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Welcome to TeX.SE! You get floating point numbers like 5.0 from your computation, where .0 gets interpreted as the east anchor. So you basically need to wrap the result in int, and you can do this without calc like this.
documentclass[tikz,border=3.14mm]{standalone}
begin{document}
begin{tikzpicture}
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
foreach i [evaluate=i as j using {int(mod(i,6)+1)}] in {1,...,6}
{
draw (u_i) -- (u_j);
}
end{tikzpicture}
end{document}

This also results from you MWE if you add int:
documentclass{article}
usepackage{tikz}
usetikzlibrary{calc}
begin{document}
tikz{
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
foreach i in {1,...,6}{
draw let n{j} = {int(Mod(i,6)+1)}
in (u_i) -- (u_n{j});
}
}
end{document}
However, IMHO this is slightly more complicated than the version without calc.
1
Another solution for the second loop without calc:foreach i [remember=i as lasti (initially 6)] in {1,...,6} draw (u_lasti) -- (u_i);
– Ignasi
Nov 13 at 9:58
add a comment |
up vote
4
down vote
accepted
Welcome to TeX.SE! You get floating point numbers like 5.0 from your computation, where .0 gets interpreted as the east anchor. So you basically need to wrap the result in int, and you can do this without calc like this.
documentclass[tikz,border=3.14mm]{standalone}
begin{document}
begin{tikzpicture}
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
foreach i [evaluate=i as j using {int(mod(i,6)+1)}] in {1,...,6}
{
draw (u_i) -- (u_j);
}
end{tikzpicture}
end{document}

This also results from you MWE if you add int:
documentclass{article}
usepackage{tikz}
usetikzlibrary{calc}
begin{document}
tikz{
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
foreach i in {1,...,6}{
draw let n{j} = {int(Mod(i,6)+1)}
in (u_i) -- (u_n{j});
}
}
end{document}
However, IMHO this is slightly more complicated than the version without calc.
1
Another solution for the second loop without calc:foreach i [remember=i as lasti (initially 6)] in {1,...,6} draw (u_lasti) -- (u_i);
– Ignasi
Nov 13 at 9:58
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Welcome to TeX.SE! You get floating point numbers like 5.0 from your computation, where .0 gets interpreted as the east anchor. So you basically need to wrap the result in int, and you can do this without calc like this.
documentclass[tikz,border=3.14mm]{standalone}
begin{document}
begin{tikzpicture}
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
foreach i [evaluate=i as j using {int(mod(i,6)+1)}] in {1,...,6}
{
draw (u_i) -- (u_j);
}
end{tikzpicture}
end{document}

This also results from you MWE if you add int:
documentclass{article}
usepackage{tikz}
usetikzlibrary{calc}
begin{document}
tikz{
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
foreach i in {1,...,6}{
draw let n{j} = {int(Mod(i,6)+1)}
in (u_i) -- (u_n{j});
}
}
end{document}
However, IMHO this is slightly more complicated than the version without calc.
Welcome to TeX.SE! You get floating point numbers like 5.0 from your computation, where .0 gets interpreted as the east anchor. So you basically need to wrap the result in int, and you can do this without calc like this.
documentclass[tikz,border=3.14mm]{standalone}
begin{document}
begin{tikzpicture}
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
foreach i [evaluate=i as j using {int(mod(i,6)+1)}] in {1,...,6}
{
draw (u_i) -- (u_j);
}
end{tikzpicture}
end{document}

This also results from you MWE if you add int:
documentclass{article}
usepackage{tikz}
usetikzlibrary{calc}
begin{document}
tikz{
node[circle,draw] (v) at (0,0) {$v$};
foreach i in {1,...,6}{
node[circle,draw] (u_i) at (180 - 360/6 * i:2cm) {$u_i$};
draw (u_i) -- (v);
}
foreach i in {1,...,6}{
draw let n{j} = {int(Mod(i,6)+1)}
in (u_i) -- (u_n{j});
}
}
end{document}
However, IMHO this is slightly more complicated than the version without calc.
answered Nov 12 at 23:32
marmot
76k486160
76k486160
1
Another solution for the second loop without calc:foreach i [remember=i as lasti (initially 6)] in {1,...,6} draw (u_lasti) -- (u_i);
– Ignasi
Nov 13 at 9:58
add a comment |
1
Another solution for the second loop without calc:foreach i [remember=i as lasti (initially 6)] in {1,...,6} draw (u_lasti) -- (u_i);
– Ignasi
Nov 13 at 9:58
1
1
Another solution for the second loop without calc:
foreach i [remember=i as lasti (initially 6)] in {1,...,6} draw (u_lasti) -- (u_i);– Ignasi
Nov 13 at 9:58
Another solution for the second loop without calc:
foreach i [remember=i as lasti (initially 6)] in {1,...,6} draw (u_lasti) -- (u_i);– Ignasi
Nov 13 at 9:58
add a comment |
jgunter is a new contributor. Be nice, and check out our Code of Conduct.
jgunter is a new contributor. Be nice, and check out our Code of Conduct.
jgunter is a new contributor. Be nice, and check out our Code of Conduct.
jgunter 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%2f459704%2ftikz-edges-not-positioned-properly-when-using-foreach-loop%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