TikZ - Edges not positioned properly when using foreach loop
Clash Royale CLAN TAG#URR8PPP
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 nj = Mod(i,6)+1
in (u_i) -- (u_nj);
However, when I try and do it this way, I end up with a graph looking like this:
MWE:
documentclassarticle
usepackagetikz
usetikzlibrarycalc
begindocument
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 nj = Mod(i,6)+1
in (u_i) -- (u_nj);
enddocument
Is there any way to do this with a foreach loop, without all the edges getting messed up?
tikz-pgf graphs loops
New contributor
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 nj = Mod(i,6)+1
in (u_i) -- (u_nj);
However, when I try and do it this way, I end up with a graph looking like this:
MWE:
documentclassarticle
usepackagetikz
usetikzlibrarycalc
begindocument
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 nj = Mod(i,6)+1
in (u_i) -- (u_nj);
enddocument
Is there any way to do this with a foreach loop, without all the edges getting messed up?
tikz-pgf graphs loops
New contributor
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 nj = Mod(i,6)+1
in (u_i) -- (u_nj);
However, when I try and do it this way, I end up with a graph looking like this:
MWE:
documentclassarticle
usepackagetikz
usetikzlibrarycalc
begindocument
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 nj = Mod(i,6)+1
in (u_i) -- (u_nj);
enddocument
Is there any way to do this with a foreach loop, without all the edges getting messed up?
tikz-pgf graphs loops
New contributor
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 nj = Mod(i,6)+1
in (u_i) -- (u_nj);
However, when I try and do it this way, I end up with a graph looking like this:
MWE:
documentclassarticle
usepackagetikz
usetikzlibrarycalc
begindocument
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 nj = Mod(i,6)+1
in (u_i) -- (u_nj);
enddocument
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
New contributor
edited yesterday
siracusa
4,32911127
4,32911127
New contributor
asked yesterday
jgunter
283
283
New contributor
New contributor
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
begindocument
begintikzpicture
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);
endtikzpicture
enddocument
This also results from you MWE if you add int
:
documentclassarticle
usepackagetikz
usetikzlibrarycalc
begindocument
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 nj = int(Mod(i,6)+1)
in (u_i) -- (u_nj);
enddocument
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
14 hours ago
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
begindocument
begintikzpicture
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);
endtikzpicture
enddocument
This also results from you MWE if you add int
:
documentclassarticle
usepackagetikz
usetikzlibrarycalc
begindocument
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 nj = int(Mod(i,6)+1)
in (u_i) -- (u_nj);
enddocument
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
14 hours ago
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
begindocument
begintikzpicture
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);
endtikzpicture
enddocument
This also results from you MWE if you add int
:
documentclassarticle
usepackagetikz
usetikzlibrarycalc
begindocument
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 nj = int(Mod(i,6)+1)
in (u_i) -- (u_nj);
enddocument
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
14 hours ago
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
begindocument
begintikzpicture
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);
endtikzpicture
enddocument
This also results from you MWE if you add int
:
documentclassarticle
usepackagetikz
usetikzlibrarycalc
begindocument
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 nj = int(Mod(i,6)+1)
in (u_i) -- (u_nj);
enddocument
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
begindocument
begintikzpicture
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);
endtikzpicture
enddocument
This also results from you MWE if you add int
:
documentclassarticle
usepackagetikz
usetikzlibrarycalc
begindocument
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 nj = int(Mod(i,6)+1)
in (u_i) -- (u_nj);
enddocument
However, IMHO this is slightly more complicated than the version without calc.
answered yesterday
marmot
74.6k482157
74.6k482157
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
14 hours ago
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
14 hours ago
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
14 hours ago
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
14 hours ago
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
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
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
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
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