TikZ - Edges not positioned properly when using foreach loop












5















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





























    5















    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



























      5












      5








      5








      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
















      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















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 23:31









      siracusa

      4,97511428




      4,97511428










      asked Nov 12 '18 at 22:57









      jgunterjgunter

      283




      283






















          1 Answer
          1






          active

          oldest

          votes


















          4














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


          }
          });














          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









          4














          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 '18 at 9:58
















          4














          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 '18 at 9:58














          4












          4








          4







          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 '18 at 23:32









          marmotmarmot

          93.5k4109208




          93.5k4109208








          • 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 '18 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 '18 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 '18 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 '18 at 9:58


















          draft saved

          draft discarded




















































          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.




          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

          Full-time equivalent

          さくらももこ

          13 indicted, 8 arrested in Calif. drug cartel investigation