TikZ - Edges not positioned properly when using foreach loop

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite












I want to draw a graph like this:



1



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:
2



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?










share|improve this question









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.























    up vote
    5
    down vote

    favorite












    I want to draw a graph like this:



    1



    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:
    2



    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?










    share|improve this question









    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.





















      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      I want to draw a graph like this:



      1



      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:
      2



      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?










      share|improve this question









      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:



      1



      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:
      2



      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






      share|improve this question









      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.











      share|improve this question









      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.









      share|improve this question




      share|improve this question








      edited yesterday









      siracusa

      4,32911127




      4,32911127






      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 yesterday









      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.




















          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


          enter image description here



          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.






          share|improve this answer
















          • 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










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



          );






          jgunter 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%2f459704%2ftikz-edges-not-positioned-properly-when-using-foreach-loop%23new-answer', 'question_page');

          );

          Post as a guest






























          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


          enter image description here



          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.






          share|improve this answer
















          • 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














          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


          enter image description here



          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.






          share|improve this answer
















          • 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












          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


          enter image description here



          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.






          share|improve this answer












          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


          enter image description here



          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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












          • 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










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









           

          draft saved


          draft discarded


















          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.













           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?