Use a foreach statement to simplify TikZ drawing











up vote
7
down vote

favorite












Below, the two diagrams should be the same, but actually they are not!



documentclass[margin=2pt]{standalone}
usepackage{tikz}
usetikzlibrary{positioning,fit}
newcommand{PL}[3]{
node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){};
draw[thick,#1] (#2.south west) -- (#2.north east);
}
begin{document}
begin{tikzpicture}
PL{L1}{1}
PL[below=0.1 of L1]{L2}{2}
PL[below=0.1 of L2]{L3}{3}
PL[below=0.1 of L3]{L4}{4}
PL[below=0.1 of L4]{L5}{5}
PL[below=0.1 of L5]{L6}{6}
PL[below=0.1 of L6]{L7}{7}
node[fit=(L1)(L7), draw] {};
end{tikzpicture}
begin{tikzpicture}
PL{L1}{1}
foreach i [evaluate=i as j using i - 1] in {2,...,7} {
PL[below=0.1cm of Lj]{Li}{i}
}
node[fit=(L1)(L7), draw] {};
end{tikzpicture}
end{document}


How do I fix the foreach statement diagram?



Enter image description here










share|improve this question




























    up vote
    7
    down vote

    favorite












    Below, the two diagrams should be the same, but actually they are not!



    documentclass[margin=2pt]{standalone}
    usepackage{tikz}
    usetikzlibrary{positioning,fit}
    newcommand{PL}[3]{
    node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){};
    draw[thick,#1] (#2.south west) -- (#2.north east);
    }
    begin{document}
    begin{tikzpicture}
    PL{L1}{1}
    PL[below=0.1 of L1]{L2}{2}
    PL[below=0.1 of L2]{L3}{3}
    PL[below=0.1 of L3]{L4}{4}
    PL[below=0.1 of L4]{L5}{5}
    PL[below=0.1 of L5]{L6}{6}
    PL[below=0.1 of L6]{L7}{7}
    node[fit=(L1)(L7), draw] {};
    end{tikzpicture}
    begin{tikzpicture}
    PL{L1}{1}
    foreach i [evaluate=i as j using i - 1] in {2,...,7} {
    PL[below=0.1cm of Lj]{Li}{i}
    }
    node[fit=(L1)(L7), draw] {};
    end{tikzpicture}
    end{document}


    How do I fix the foreach statement diagram?



    Enter image description here










    share|improve this question


























      up vote
      7
      down vote

      favorite









      up vote
      7
      down vote

      favorite











      Below, the two diagrams should be the same, but actually they are not!



      documentclass[margin=2pt]{standalone}
      usepackage{tikz}
      usetikzlibrary{positioning,fit}
      newcommand{PL}[3]{
      node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){};
      draw[thick,#1] (#2.south west) -- (#2.north east);
      }
      begin{document}
      begin{tikzpicture}
      PL{L1}{1}
      PL[below=0.1 of L1]{L2}{2}
      PL[below=0.1 of L2]{L3}{3}
      PL[below=0.1 of L3]{L4}{4}
      PL[below=0.1 of L4]{L5}{5}
      PL[below=0.1 of L5]{L6}{6}
      PL[below=0.1 of L6]{L7}{7}
      node[fit=(L1)(L7), draw] {};
      end{tikzpicture}
      begin{tikzpicture}
      PL{L1}{1}
      foreach i [evaluate=i as j using i - 1] in {2,...,7} {
      PL[below=0.1cm of Lj]{Li}{i}
      }
      node[fit=(L1)(L7), draw] {};
      end{tikzpicture}
      end{document}


      How do I fix the foreach statement diagram?



      Enter image description here










      share|improve this question















      Below, the two diagrams should be the same, but actually they are not!



      documentclass[margin=2pt]{standalone}
      usepackage{tikz}
      usetikzlibrary{positioning,fit}
      newcommand{PL}[3]{
      node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){};
      draw[thick,#1] (#2.south west) -- (#2.north east);
      }
      begin{document}
      begin{tikzpicture}
      PL{L1}{1}
      PL[below=0.1 of L1]{L2}{2}
      PL[below=0.1 of L2]{L3}{3}
      PL[below=0.1 of L3]{L4}{4}
      PL[below=0.1 of L4]{L5}{5}
      PL[below=0.1 of L5]{L6}{6}
      PL[below=0.1 of L6]{L7}{7}
      node[fit=(L1)(L7), draw] {};
      end{tikzpicture}
      begin{tikzpicture}
      PL{L1}{1}
      foreach i [evaluate=i as j using i - 1] in {2,...,7} {
      PL[below=0.1cm of Lj]{Li}{i}
      }
      node[fit=(L1)(L7), draw] {};
      end{tikzpicture}
      end{document}


      How do I fix the foreach statement diagram?



      Enter image description here







      tikz-pgf






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 at 11:33









      Stefan Pinnow

      19.2k83174




      19.2k83174










      asked Nov 24 at 20:17









      lucky1928

      1,1191716




      1,1191716






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote













          If you do not evaluate j to an integer, you get numbers like 1.0, in which .0 is interpreted as the east anchor. So all I did was to replace [evaluate=i as j using i - 1] by [evaluate=i as j using {int(i - 1)}] to get



          documentclass[margin=2pt]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          newcommand{PL}[3]{
          node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){};
          draw[thick,#1] (#2.south west) -- (#2.north east);
          }
          begin{document}
          begin{tikzpicture}
          PL{L1}{1}
          PL[below=0.1 of L1]{L2}{2}
          PL[below=0.1 of L2]{L3}{3}
          PL[below=0.1 of L3]{L4}{C}
          PL[below=0.1 of L4]{L5}{R}
          PL[below=0.1 of L5]{L6}{RC}
          PL[below=0.1 of L6]{L7}{RH}
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          begin{tikzpicture}
          PL{L1}{1}
          foreach i [evaluate=i as j using {int(i - 1)}] in {2,...,7} {
          PL[below=0.1cm of Lj]{Li}{i}
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          enter image description here



          ADDENDUM: Since @AndréC added an answer which is IMHO not really to the point of the original question, I add something that is to the point of the original question as well as some sort of a response to AndréC.




          1. You can avoid all this by using remember.... And you can, of course, attach the labels of your left diagram, just declare i to be the count.

          2. Using a path picture to strike out a node is one way, what you are doing is IMHO at least equally good, and one can also use strike out that comes with shapes.misc. None of this, however, is IMHO at the heart of the question.


          So here is an example (but if I was you I would keep your way of striking the node out, probably using a path picture is more elegant than strike through, though).



          documentclass[tikz,border=3.14mm]{standalone}
          usetikzlibrary{positioning,fit,shapes.misc}
          newcommand{PL}[3]{
          node[thick,strike out,
          draw,minimum size=0.35cm,inner sep=0,outer sep=0,#1](#2-inner){};
          node[draw,circle,inner sep=0,fit=(#2-inner),outer sep=1pt,label=left:#3](#2){};
          }
          begin{document}
          begin{tikzpicture}
          PL{L1}{1}
          foreach X [count=i starting from 2,remember=i as j (initially 1)]
          in {2,3,C,R,RC,RH} {
          PL[below=0.1cm of Lj]{Li}{X}
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          enter image description here






          share|improve this answer























          • Thanks, It's so hard to figure it out by me :-).
            – lucky1928
            Nov 24 at 20:23










          • @lucky1928 There are several variants of this question on this site, meaning that this is somewhat tricky and has been overlooked by several. (One way to figure out what's going on is to add typeouts to your code, so if you add typeout{j} to your code you will see 1.0, 2.0 and so on, which may help to get on track.)
            – marmot
            Nov 24 at 20:26










          • Your explanation of the problem is wonderful.I never thought it was just a problem with the interpretation of writing 1.0. Congratulations!
            – AndréC
            Nov 24 at 21:31


















          up vote
          5
          down vote













          It is not necessary to use a LaTeX command to trace nodes. All you have to do is declare tikz styles.
          To draw the diagonal bar, I used the notion of path picture bounding box described on page 173 of the 3.0.1a manual.



          style



          documentclass[margin=5mm]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          %newcommand{PL}[3]{
          % node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){};
          % draw[thick,#1] (#2.south west) -- (#2.north east);
          %}

          begin{document}
          % begin{tikzpicture}
          % PL{L1}{1}
          % PL[below=0.1 of L1]{L2}{2}
          % PL[below=0.1 of L2]{L3}{3}
          % PL[below=0.1 of L3]{L4}{4}
          % PL[below=0.1 of L4]{L5}{5}
          % PL[below=0.1 of L5]{L6}{6}
          % PL[below=0.1 of L6]{L7}{7}
          % node[fit=(L1)(L7), draw] {};
          %end{tikzpicture}

          begin{tikzpicture}
          [slash/.style={
          draw,thick,circle,minimum size=.5cm,
          inner sep =0pt,outer sep=0pt,
          label={left:#1},
          path picture={
          draw(path picture bounding box.south west)--(path picture bounding box.north east);}
          }]
          node[slash=1](L1){};
          foreach i [evaluate=i as j using int(i - 1)] in {2,...,7} {
          node[below=0.1 of Lj,slash=i](Li){};
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          Update (to answer to marmot):



          @marmot: I know for a fact that my answer is not the heart of the question. And that was not my point.



          To simplify even more the loop:



          documentclass[margin=5mm]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          begin{document}

          begin{tikzpicture}
          [slash/.style={
          draw,thick,circle,minimum size=.5cm,
          inner sep =0pt,outer sep=0pt,
          label={left:#1},
          path picture={
          draw(path picture bounding box.south west)--(path picture bounding box.north east);}
          }]
          node[slash=1](L1){};
          foreach j [count=i] in {2,...,7} {
          node[below=0.1 of Li,slash=j](Lj){};
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}





          share|improve this answer























          • +1 for the use of path picture (even though I do not think that this is at the heart of the question).
            – marmot
            Nov 24 at 21:44











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


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f461612%2fuse-a-foreach-statement-to-simplify-tikz-drawing%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote













          If you do not evaluate j to an integer, you get numbers like 1.0, in which .0 is interpreted as the east anchor. So all I did was to replace [evaluate=i as j using i - 1] by [evaluate=i as j using {int(i - 1)}] to get



          documentclass[margin=2pt]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          newcommand{PL}[3]{
          node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){};
          draw[thick,#1] (#2.south west) -- (#2.north east);
          }
          begin{document}
          begin{tikzpicture}
          PL{L1}{1}
          PL[below=0.1 of L1]{L2}{2}
          PL[below=0.1 of L2]{L3}{3}
          PL[below=0.1 of L3]{L4}{C}
          PL[below=0.1 of L4]{L5}{R}
          PL[below=0.1 of L5]{L6}{RC}
          PL[below=0.1 of L6]{L7}{RH}
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          begin{tikzpicture}
          PL{L1}{1}
          foreach i [evaluate=i as j using {int(i - 1)}] in {2,...,7} {
          PL[below=0.1cm of Lj]{Li}{i}
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          enter image description here



          ADDENDUM: Since @AndréC added an answer which is IMHO not really to the point of the original question, I add something that is to the point of the original question as well as some sort of a response to AndréC.




          1. You can avoid all this by using remember.... And you can, of course, attach the labels of your left diagram, just declare i to be the count.

          2. Using a path picture to strike out a node is one way, what you are doing is IMHO at least equally good, and one can also use strike out that comes with shapes.misc. None of this, however, is IMHO at the heart of the question.


          So here is an example (but if I was you I would keep your way of striking the node out, probably using a path picture is more elegant than strike through, though).



          documentclass[tikz,border=3.14mm]{standalone}
          usetikzlibrary{positioning,fit,shapes.misc}
          newcommand{PL}[3]{
          node[thick,strike out,
          draw,minimum size=0.35cm,inner sep=0,outer sep=0,#1](#2-inner){};
          node[draw,circle,inner sep=0,fit=(#2-inner),outer sep=1pt,label=left:#3](#2){};
          }
          begin{document}
          begin{tikzpicture}
          PL{L1}{1}
          foreach X [count=i starting from 2,remember=i as j (initially 1)]
          in {2,3,C,R,RC,RH} {
          PL[below=0.1cm of Lj]{Li}{X}
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          enter image description here






          share|improve this answer























          • Thanks, It's so hard to figure it out by me :-).
            – lucky1928
            Nov 24 at 20:23










          • @lucky1928 There are several variants of this question on this site, meaning that this is somewhat tricky and has been overlooked by several. (One way to figure out what's going on is to add typeouts to your code, so if you add typeout{j} to your code you will see 1.0, 2.0 and so on, which may help to get on track.)
            – marmot
            Nov 24 at 20:26










          • Your explanation of the problem is wonderful.I never thought it was just a problem with the interpretation of writing 1.0. Congratulations!
            – AndréC
            Nov 24 at 21:31















          up vote
          5
          down vote













          If you do not evaluate j to an integer, you get numbers like 1.0, in which .0 is interpreted as the east anchor. So all I did was to replace [evaluate=i as j using i - 1] by [evaluate=i as j using {int(i - 1)}] to get



          documentclass[margin=2pt]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          newcommand{PL}[3]{
          node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){};
          draw[thick,#1] (#2.south west) -- (#2.north east);
          }
          begin{document}
          begin{tikzpicture}
          PL{L1}{1}
          PL[below=0.1 of L1]{L2}{2}
          PL[below=0.1 of L2]{L3}{3}
          PL[below=0.1 of L3]{L4}{C}
          PL[below=0.1 of L4]{L5}{R}
          PL[below=0.1 of L5]{L6}{RC}
          PL[below=0.1 of L6]{L7}{RH}
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          begin{tikzpicture}
          PL{L1}{1}
          foreach i [evaluate=i as j using {int(i - 1)}] in {2,...,7} {
          PL[below=0.1cm of Lj]{Li}{i}
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          enter image description here



          ADDENDUM: Since @AndréC added an answer which is IMHO not really to the point of the original question, I add something that is to the point of the original question as well as some sort of a response to AndréC.




          1. You can avoid all this by using remember.... And you can, of course, attach the labels of your left diagram, just declare i to be the count.

          2. Using a path picture to strike out a node is one way, what you are doing is IMHO at least equally good, and one can also use strike out that comes with shapes.misc. None of this, however, is IMHO at the heart of the question.


          So here is an example (but if I was you I would keep your way of striking the node out, probably using a path picture is more elegant than strike through, though).



          documentclass[tikz,border=3.14mm]{standalone}
          usetikzlibrary{positioning,fit,shapes.misc}
          newcommand{PL}[3]{
          node[thick,strike out,
          draw,minimum size=0.35cm,inner sep=0,outer sep=0,#1](#2-inner){};
          node[draw,circle,inner sep=0,fit=(#2-inner),outer sep=1pt,label=left:#3](#2){};
          }
          begin{document}
          begin{tikzpicture}
          PL{L1}{1}
          foreach X [count=i starting from 2,remember=i as j (initially 1)]
          in {2,3,C,R,RC,RH} {
          PL[below=0.1cm of Lj]{Li}{X}
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          enter image description here






          share|improve this answer























          • Thanks, It's so hard to figure it out by me :-).
            – lucky1928
            Nov 24 at 20:23










          • @lucky1928 There are several variants of this question on this site, meaning that this is somewhat tricky and has been overlooked by several. (One way to figure out what's going on is to add typeouts to your code, so if you add typeout{j} to your code you will see 1.0, 2.0 and so on, which may help to get on track.)
            – marmot
            Nov 24 at 20:26










          • Your explanation of the problem is wonderful.I never thought it was just a problem with the interpretation of writing 1.0. Congratulations!
            – AndréC
            Nov 24 at 21:31













          up vote
          5
          down vote










          up vote
          5
          down vote









          If you do not evaluate j to an integer, you get numbers like 1.0, in which .0 is interpreted as the east anchor. So all I did was to replace [evaluate=i as j using i - 1] by [evaluate=i as j using {int(i - 1)}] to get



          documentclass[margin=2pt]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          newcommand{PL}[3]{
          node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){};
          draw[thick,#1] (#2.south west) -- (#2.north east);
          }
          begin{document}
          begin{tikzpicture}
          PL{L1}{1}
          PL[below=0.1 of L1]{L2}{2}
          PL[below=0.1 of L2]{L3}{3}
          PL[below=0.1 of L3]{L4}{C}
          PL[below=0.1 of L4]{L5}{R}
          PL[below=0.1 of L5]{L6}{RC}
          PL[below=0.1 of L6]{L7}{RH}
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          begin{tikzpicture}
          PL{L1}{1}
          foreach i [evaluate=i as j using {int(i - 1)}] in {2,...,7} {
          PL[below=0.1cm of Lj]{Li}{i}
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          enter image description here



          ADDENDUM: Since @AndréC added an answer which is IMHO not really to the point of the original question, I add something that is to the point of the original question as well as some sort of a response to AndréC.




          1. You can avoid all this by using remember.... And you can, of course, attach the labels of your left diagram, just declare i to be the count.

          2. Using a path picture to strike out a node is one way, what you are doing is IMHO at least equally good, and one can also use strike out that comes with shapes.misc. None of this, however, is IMHO at the heart of the question.


          So here is an example (but if I was you I would keep your way of striking the node out, probably using a path picture is more elegant than strike through, though).



          documentclass[tikz,border=3.14mm]{standalone}
          usetikzlibrary{positioning,fit,shapes.misc}
          newcommand{PL}[3]{
          node[thick,strike out,
          draw,minimum size=0.35cm,inner sep=0,outer sep=0,#1](#2-inner){};
          node[draw,circle,inner sep=0,fit=(#2-inner),outer sep=1pt,label=left:#3](#2){};
          }
          begin{document}
          begin{tikzpicture}
          PL{L1}{1}
          foreach X [count=i starting from 2,remember=i as j (initially 1)]
          in {2,3,C,R,RC,RH} {
          PL[below=0.1cm of Lj]{Li}{X}
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          enter image description here






          share|improve this answer














          If you do not evaluate j to an integer, you get numbers like 1.0, in which .0 is interpreted as the east anchor. So all I did was to replace [evaluate=i as j using i - 1] by [evaluate=i as j using {int(i - 1)}] to get



          documentclass[margin=2pt]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          newcommand{PL}[3]{
          node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){};
          draw[thick,#1] (#2.south west) -- (#2.north east);
          }
          begin{document}
          begin{tikzpicture}
          PL{L1}{1}
          PL[below=0.1 of L1]{L2}{2}
          PL[below=0.1 of L2]{L3}{3}
          PL[below=0.1 of L3]{L4}{C}
          PL[below=0.1 of L4]{L5}{R}
          PL[below=0.1 of L5]{L6}{RC}
          PL[below=0.1 of L6]{L7}{RH}
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          begin{tikzpicture}
          PL{L1}{1}
          foreach i [evaluate=i as j using {int(i - 1)}] in {2,...,7} {
          PL[below=0.1cm of Lj]{Li}{i}
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          enter image description here



          ADDENDUM: Since @AndréC added an answer which is IMHO not really to the point of the original question, I add something that is to the point of the original question as well as some sort of a response to AndréC.




          1. You can avoid all this by using remember.... And you can, of course, attach the labels of your left diagram, just declare i to be the count.

          2. Using a path picture to strike out a node is one way, what you are doing is IMHO at least equally good, and one can also use strike out that comes with shapes.misc. None of this, however, is IMHO at the heart of the question.


          So here is an example (but if I was you I would keep your way of striking the node out, probably using a path picture is more elegant than strike through, though).



          documentclass[tikz,border=3.14mm]{standalone}
          usetikzlibrary{positioning,fit,shapes.misc}
          newcommand{PL}[3]{
          node[thick,strike out,
          draw,minimum size=0.35cm,inner sep=0,outer sep=0,#1](#2-inner){};
          node[draw,circle,inner sep=0,fit=(#2-inner),outer sep=1pt,label=left:#3](#2){};
          }
          begin{document}
          begin{tikzpicture}
          PL{L1}{1}
          foreach X [count=i starting from 2,remember=i as j (initially 1)]
          in {2,3,C,R,RC,RH} {
          PL[below=0.1cm of Lj]{Li}{X}
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 at 21:38

























          answered Nov 24 at 20:20









          marmot

          79.9k491171




          79.9k491171












          • Thanks, It's so hard to figure it out by me :-).
            – lucky1928
            Nov 24 at 20:23










          • @lucky1928 There are several variants of this question on this site, meaning that this is somewhat tricky and has been overlooked by several. (One way to figure out what's going on is to add typeouts to your code, so if you add typeout{j} to your code you will see 1.0, 2.0 and so on, which may help to get on track.)
            – marmot
            Nov 24 at 20:26










          • Your explanation of the problem is wonderful.I never thought it was just a problem with the interpretation of writing 1.0. Congratulations!
            – AndréC
            Nov 24 at 21:31


















          • Thanks, It's so hard to figure it out by me :-).
            – lucky1928
            Nov 24 at 20:23










          • @lucky1928 There are several variants of this question on this site, meaning that this is somewhat tricky and has been overlooked by several. (One way to figure out what's going on is to add typeouts to your code, so if you add typeout{j} to your code you will see 1.0, 2.0 and so on, which may help to get on track.)
            – marmot
            Nov 24 at 20:26










          • Your explanation of the problem is wonderful.I never thought it was just a problem with the interpretation of writing 1.0. Congratulations!
            – AndréC
            Nov 24 at 21:31
















          Thanks, It's so hard to figure it out by me :-).
          – lucky1928
          Nov 24 at 20:23




          Thanks, It's so hard to figure it out by me :-).
          – lucky1928
          Nov 24 at 20:23












          @lucky1928 There are several variants of this question on this site, meaning that this is somewhat tricky and has been overlooked by several. (One way to figure out what's going on is to add typeouts to your code, so if you add typeout{j} to your code you will see 1.0, 2.0 and so on, which may help to get on track.)
          – marmot
          Nov 24 at 20:26




          @lucky1928 There are several variants of this question on this site, meaning that this is somewhat tricky and has been overlooked by several. (One way to figure out what's going on is to add typeouts to your code, so if you add typeout{j} to your code you will see 1.0, 2.0 and so on, which may help to get on track.)
          – marmot
          Nov 24 at 20:26












          Your explanation of the problem is wonderful.I never thought it was just a problem with the interpretation of writing 1.0. Congratulations!
          – AndréC
          Nov 24 at 21:31




          Your explanation of the problem is wonderful.I never thought it was just a problem with the interpretation of writing 1.0. Congratulations!
          – AndréC
          Nov 24 at 21:31










          up vote
          5
          down vote













          It is not necessary to use a LaTeX command to trace nodes. All you have to do is declare tikz styles.
          To draw the diagonal bar, I used the notion of path picture bounding box described on page 173 of the 3.0.1a manual.



          style



          documentclass[margin=5mm]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          %newcommand{PL}[3]{
          % node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){};
          % draw[thick,#1] (#2.south west) -- (#2.north east);
          %}

          begin{document}
          % begin{tikzpicture}
          % PL{L1}{1}
          % PL[below=0.1 of L1]{L2}{2}
          % PL[below=0.1 of L2]{L3}{3}
          % PL[below=0.1 of L3]{L4}{4}
          % PL[below=0.1 of L4]{L5}{5}
          % PL[below=0.1 of L5]{L6}{6}
          % PL[below=0.1 of L6]{L7}{7}
          % node[fit=(L1)(L7), draw] {};
          %end{tikzpicture}

          begin{tikzpicture}
          [slash/.style={
          draw,thick,circle,minimum size=.5cm,
          inner sep =0pt,outer sep=0pt,
          label={left:#1},
          path picture={
          draw(path picture bounding box.south west)--(path picture bounding box.north east);}
          }]
          node[slash=1](L1){};
          foreach i [evaluate=i as j using int(i - 1)] in {2,...,7} {
          node[below=0.1 of Lj,slash=i](Li){};
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          Update (to answer to marmot):



          @marmot: I know for a fact that my answer is not the heart of the question. And that was not my point.



          To simplify even more the loop:



          documentclass[margin=5mm]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          begin{document}

          begin{tikzpicture}
          [slash/.style={
          draw,thick,circle,minimum size=.5cm,
          inner sep =0pt,outer sep=0pt,
          label={left:#1},
          path picture={
          draw(path picture bounding box.south west)--(path picture bounding box.north east);}
          }]
          node[slash=1](L1){};
          foreach j [count=i] in {2,...,7} {
          node[below=0.1 of Li,slash=j](Lj){};
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}





          share|improve this answer























          • +1 for the use of path picture (even though I do not think that this is at the heart of the question).
            – marmot
            Nov 24 at 21:44















          up vote
          5
          down vote













          It is not necessary to use a LaTeX command to trace nodes. All you have to do is declare tikz styles.
          To draw the diagonal bar, I used the notion of path picture bounding box described on page 173 of the 3.0.1a manual.



          style



          documentclass[margin=5mm]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          %newcommand{PL}[3]{
          % node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){};
          % draw[thick,#1] (#2.south west) -- (#2.north east);
          %}

          begin{document}
          % begin{tikzpicture}
          % PL{L1}{1}
          % PL[below=0.1 of L1]{L2}{2}
          % PL[below=0.1 of L2]{L3}{3}
          % PL[below=0.1 of L3]{L4}{4}
          % PL[below=0.1 of L4]{L5}{5}
          % PL[below=0.1 of L5]{L6}{6}
          % PL[below=0.1 of L6]{L7}{7}
          % node[fit=(L1)(L7), draw] {};
          %end{tikzpicture}

          begin{tikzpicture}
          [slash/.style={
          draw,thick,circle,minimum size=.5cm,
          inner sep =0pt,outer sep=0pt,
          label={left:#1},
          path picture={
          draw(path picture bounding box.south west)--(path picture bounding box.north east);}
          }]
          node[slash=1](L1){};
          foreach i [evaluate=i as j using int(i - 1)] in {2,...,7} {
          node[below=0.1 of Lj,slash=i](Li){};
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          Update (to answer to marmot):



          @marmot: I know for a fact that my answer is not the heart of the question. And that was not my point.



          To simplify even more the loop:



          documentclass[margin=5mm]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          begin{document}

          begin{tikzpicture}
          [slash/.style={
          draw,thick,circle,minimum size=.5cm,
          inner sep =0pt,outer sep=0pt,
          label={left:#1},
          path picture={
          draw(path picture bounding box.south west)--(path picture bounding box.north east);}
          }]
          node[slash=1](L1){};
          foreach j [count=i] in {2,...,7} {
          node[below=0.1 of Li,slash=j](Lj){};
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}





          share|improve this answer























          • +1 for the use of path picture (even though I do not think that this is at the heart of the question).
            – marmot
            Nov 24 at 21:44













          up vote
          5
          down vote










          up vote
          5
          down vote









          It is not necessary to use a LaTeX command to trace nodes. All you have to do is declare tikz styles.
          To draw the diagonal bar, I used the notion of path picture bounding box described on page 173 of the 3.0.1a manual.



          style



          documentclass[margin=5mm]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          %newcommand{PL}[3]{
          % node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){};
          % draw[thick,#1] (#2.south west) -- (#2.north east);
          %}

          begin{document}
          % begin{tikzpicture}
          % PL{L1}{1}
          % PL[below=0.1 of L1]{L2}{2}
          % PL[below=0.1 of L2]{L3}{3}
          % PL[below=0.1 of L3]{L4}{4}
          % PL[below=0.1 of L4]{L5}{5}
          % PL[below=0.1 of L5]{L6}{6}
          % PL[below=0.1 of L6]{L7}{7}
          % node[fit=(L1)(L7), draw] {};
          %end{tikzpicture}

          begin{tikzpicture}
          [slash/.style={
          draw,thick,circle,minimum size=.5cm,
          inner sep =0pt,outer sep=0pt,
          label={left:#1},
          path picture={
          draw(path picture bounding box.south west)--(path picture bounding box.north east);}
          }]
          node[slash=1](L1){};
          foreach i [evaluate=i as j using int(i - 1)] in {2,...,7} {
          node[below=0.1 of Lj,slash=i](Li){};
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          Update (to answer to marmot):



          @marmot: I know for a fact that my answer is not the heart of the question. And that was not my point.



          To simplify even more the loop:



          documentclass[margin=5mm]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          begin{document}

          begin{tikzpicture}
          [slash/.style={
          draw,thick,circle,minimum size=.5cm,
          inner sep =0pt,outer sep=0pt,
          label={left:#1},
          path picture={
          draw(path picture bounding box.south west)--(path picture bounding box.north east);}
          }]
          node[slash=1](L1){};
          foreach j [count=i] in {2,...,7} {
          node[below=0.1 of Li,slash=j](Lj){};
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}





          share|improve this answer














          It is not necessary to use a LaTeX command to trace nodes. All you have to do is declare tikz styles.
          To draw the diagonal bar, I used the notion of path picture bounding box described on page 173 of the 3.0.1a manual.



          style



          documentclass[margin=5mm]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          %newcommand{PL}[3]{
          % node[thick,circle,draw,minimum size=0.5cm,inner sep=0,outer sep=0,label=left:#3,#1](#2){};
          % draw[thick,#1] (#2.south west) -- (#2.north east);
          %}

          begin{document}
          % begin{tikzpicture}
          % PL{L1}{1}
          % PL[below=0.1 of L1]{L2}{2}
          % PL[below=0.1 of L2]{L3}{3}
          % PL[below=0.1 of L3]{L4}{4}
          % PL[below=0.1 of L4]{L5}{5}
          % PL[below=0.1 of L5]{L6}{6}
          % PL[below=0.1 of L6]{L7}{7}
          % node[fit=(L1)(L7), draw] {};
          %end{tikzpicture}

          begin{tikzpicture}
          [slash/.style={
          draw,thick,circle,minimum size=.5cm,
          inner sep =0pt,outer sep=0pt,
          label={left:#1},
          path picture={
          draw(path picture bounding box.south west)--(path picture bounding box.north east);}
          }]
          node[slash=1](L1){};
          foreach i [evaluate=i as j using int(i - 1)] in {2,...,7} {
          node[below=0.1 of Lj,slash=i](Li){};
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}


          Update (to answer to marmot):



          @marmot: I know for a fact that my answer is not the heart of the question. And that was not my point.



          To simplify even more the loop:



          documentclass[margin=5mm]{standalone}
          usepackage{tikz}
          usetikzlibrary{positioning,fit}
          begin{document}

          begin{tikzpicture}
          [slash/.style={
          draw,thick,circle,minimum size=.5cm,
          inner sep =0pt,outer sep=0pt,
          label={left:#1},
          path picture={
          draw(path picture bounding box.south west)--(path picture bounding box.north east);}
          }]
          node[slash=1](L1){};
          foreach j [count=i] in {2,...,7} {
          node[below=0.1 of Li,slash=j](Lj){};
          }
          node[fit=(L1)(L7), draw] {};
          end{tikzpicture}
          end{document}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 at 21:51

























          answered Nov 24 at 21:12









          AndréC

          6,53711140




          6,53711140












          • +1 for the use of path picture (even though I do not think that this is at the heart of the question).
            – marmot
            Nov 24 at 21:44


















          • +1 for the use of path picture (even though I do not think that this is at the heart of the question).
            – marmot
            Nov 24 at 21:44
















          +1 for the use of path picture (even though I do not think that this is at the heart of the question).
          – marmot
          Nov 24 at 21:44




          +1 for the use of path picture (even though I do not think that this is at the heart of the question).
          – marmot
          Nov 24 at 21:44


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f461612%2fuse-a-foreach-statement-to-simplify-tikz-drawing%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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?