TikZ - Edges not positioned properly when using foreach loop











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



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?










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



    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?










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



      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?










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



      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






      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 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.






















          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}


          enter image description here



          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.






          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
            Nov 13 at 9:58











          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















          Required, but never shown

























          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}


          enter image description here



          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.






          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
            Nov 13 at 9:58















          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}


          enter image description here



          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.






          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
            Nov 13 at 9:58













          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}


          enter image description here



          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.






          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}
          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}


          enter image description here



          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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














          • 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










          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















          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







          Popular posts from this blog

          How to send String Array data to Server using php in android

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Is anime1.com a legal site for watching anime?