TikZ: How does global/local [node distance] work?











up vote
6
down vote

favorite
1












The expected output of this MWE is two overlaying nodes (i.e. B and C).



However, node [draw, right = 1cm of A] (B) {B}; successfully overrides the global setting of node distance=2cm, while node [draw, right = of A, node distance = 1cm] (C) {C}; doesn't.



So, why do both syntaxes result in different outputs?



documentclass[border=1cm]{standalone}
usepackage{tikz}
usetikzlibrary{shapes,arrows,positioning}

begin{document}
begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
node [draw] (A) {A};
node [draw, right = 1cm of A] (B) {B};
node [draw, right = of A, node distance = 1cm] (C) {C};
end{tikzpicture}
end{document}


enter image description here










share|improve this question




























    up vote
    6
    down vote

    favorite
    1












    The expected output of this MWE is two overlaying nodes (i.e. B and C).



    However, node [draw, right = 1cm of A] (B) {B}; successfully overrides the global setting of node distance=2cm, while node [draw, right = of A, node distance = 1cm] (C) {C}; doesn't.



    So, why do both syntaxes result in different outputs?



    documentclass[border=1cm]{standalone}
    usepackage{tikz}
    usetikzlibrary{shapes,arrows,positioning}

    begin{document}
    begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
    node [draw] (A) {A};
    node [draw, right = 1cm of A] (B) {B};
    node [draw, right = of A, node distance = 1cm] (C) {C};
    end{tikzpicture}
    end{document}


    enter image description here










    share|improve this question


























      up vote
      6
      down vote

      favorite
      1









      up vote
      6
      down vote

      favorite
      1






      1





      The expected output of this MWE is two overlaying nodes (i.e. B and C).



      However, node [draw, right = 1cm of A] (B) {B}; successfully overrides the global setting of node distance=2cm, while node [draw, right = of A, node distance = 1cm] (C) {C}; doesn't.



      So, why do both syntaxes result in different outputs?



      documentclass[border=1cm]{standalone}
      usepackage{tikz}
      usetikzlibrary{shapes,arrows,positioning}

      begin{document}
      begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
      node [draw] (A) {A};
      node [draw, right = 1cm of A] (B) {B};
      node [draw, right = of A, node distance = 1cm] (C) {C};
      end{tikzpicture}
      end{document}


      enter image description here










      share|improve this question















      The expected output of this MWE is two overlaying nodes (i.e. B and C).



      However, node [draw, right = 1cm of A] (B) {B}; successfully overrides the global setting of node distance=2cm, while node [draw, right = of A, node distance = 1cm] (C) {C}; doesn't.



      So, why do both syntaxes result in different outputs?



      documentclass[border=1cm]{standalone}
      usepackage{tikz}
      usetikzlibrary{shapes,arrows,positioning}

      begin{document}
      begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
      node [draw] (A) {A};
      node [draw, right = 1cm of A] (B) {B};
      node [draw, right = of A, node distance = 1cm] (C) {C};
      end{tikzpicture}
      end{document}


      enter image description here







      tikz-pgf






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 3 hours ago

























      asked 19 hours ago









      Diaa

      2,49311644




      2,49311644






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          The ordering is important. The TikZ parser parses from left to right. This means that you need to first set the node distance (locally) to 1cm by saying node distance = 1cm, and then let TikZ compute the actual coordinates of the C node by saying right = of A.



          documentclass[border=1cm]{standalone}
          usepackage{tikz}
          usetikzlibrary{shapes,arrows,positioning}

          begin{document}
          begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
          node [draw] (A) {A};
          node [draw, right = 1cm of A] (B) {B};
          node [draw, node distance = 1cm, right = of A] (C) {C};
          end{tikzpicture}
          end{document}


          enter image description here



          As you can see, now the B and C nodes are on top of each other.






          share|improve this answer























          • I didn't know my question is that naive
            – Diaa
            19 hours ago










          • I really appreciate your answer, but I think it is better to delete this question
            – Diaa
            19 hours ago








          • 3




            @Diaa I do not think your question is naive. Did I misread it? (Actually it took me a while to figure out how that works, and actually you can find some posts by high reputation users on this site who get it wrong IMHO, so I do not think this is a trivial question. But the decision of whether or not to delete is yours.)
            – marmot
            18 hours ago












          • I mean it is a stupid question XD that doesn't deserve to be asked :)
            – Diaa
            18 hours ago












          • @Diaa The question became stupid only after seeing the answer. So please don't delete the question. Instead, downvote for making the question look stupid ;)
            – nidhin
            15 hours ago


















          up vote
          3
          down vote













          The operation of the node distance key is particular and far from obvious. It only works if and only if there is an of part but no shift part.



          I quote p 231 of 3.0.1a manual:




          /tikz/node distance=<shifting part> (no default, initially 1cm and
          1cm) The value of this key is used as is used if and
          only if
          a <of-part> is present, but no <shifting part>.




          Look at this example.




          • Nodes colored blue do not have a shifting part in their code, so the node distance key is active.

          • The red colored nodes have a shifting part and therefore the node distance key is inactive.


          examples



          documentclass{book}
          usepackage{tikz}
          usetikzlibrary{positioning}
          begin{document}

          foreach i in {8,13,17}{
          begin{tikzpicture}[noeud/.style={draw,node distance=i mm},
          entre/.style={midway,draw=none,fill=white,inner sep =1pt}]
          draw[fill=green!10] (-1,-.1) rectangle (3,5);
          draw[help lines] (-1,-.1) grid (3,5);
          % No shifting part
          begin{scope}[every node/.append style={fill=blue!20,font=scriptsize}]
          node[noeud,align=center] (a1) at (0,0) {node distance\ actived};
          node[noeud] (b1) [above=of a1] {1};
          node[noeud] (c1) [above=of b1] {2};
          draw [<->](a1)--(b1)node[entre]{i mm};
          draw [<->](b1)--(c1)node[entre]{i mm};
          end{scope}
          % Shifting part
          begin{scope}[every node/.append style={fill=red!20,font=scriptsize}]
          node[noeud,align=center] (a2) at (2,0) {node distance\ inactived};
          node[noeud] (b2) [above=1cm of a2] {1};
          node[noeud] (c2) [above=1cm of b2] {2};
          draw [<->](a2)--(b2)node[entre]{1 cm};
          draw [<->](b2)--(c2)node[entre]{1 cm};
          end{scope}
          end{tikzpicture}
          }
          end{document}


          Your code is



          right = 1cm of A


          The shifting part is present and equal to 1 cm. So the key distance node=2cm is disabled. Point B is therefore located 1 cm from A as you have specified.



          Then for point C, you write this:



           right = of A, node distance = 1cm


          As there is no shifting part in this code, the distance node=2cm key is active and therefore the node is placed at 2cm. Then you specify this distance again, but it has already been computed and so pgf does nothing more, as you can see with a 10 cm distance node key.



          code



          documentclass{article}
          usepackage{tikz}
          usetikzlibrary{shapes,arrows,positioning}

          begin{document}
          begin{tikzpicture}[auto, node distance=2cm]
          node [draw] (A) {A};
          node [draw, right = 1cm of A] (B) {B};
          node [draw, right =of A,circle,fill=red!40,opacity=.5,inner sep =10pt] (B) {B};
          node [draw, right = of A, node distance = 10cm,fill=blue!40,opacity=.5,inner sep =10pt] (C) {C};
          node [draw, node distance = 3cm, right = of A,fill=violet,opacity=.5,inner sep =10pt] (D) {D};
          end{tikzpicture}
          end{document}


          Translated with www.DeepL.com/Translator






          share|improve this answer



















          • 1




            Many thanks for the comprehensive answer.
            – Diaa
            15 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
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f460154%2ftikz-how-does-global-local-node-distance-work%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
          6
          down vote



          accepted










          The ordering is important. The TikZ parser parses from left to right. This means that you need to first set the node distance (locally) to 1cm by saying node distance = 1cm, and then let TikZ compute the actual coordinates of the C node by saying right = of A.



          documentclass[border=1cm]{standalone}
          usepackage{tikz}
          usetikzlibrary{shapes,arrows,positioning}

          begin{document}
          begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
          node [draw] (A) {A};
          node [draw, right = 1cm of A] (B) {B};
          node [draw, node distance = 1cm, right = of A] (C) {C};
          end{tikzpicture}
          end{document}


          enter image description here



          As you can see, now the B and C nodes are on top of each other.






          share|improve this answer























          • I didn't know my question is that naive
            – Diaa
            19 hours ago










          • I really appreciate your answer, but I think it is better to delete this question
            – Diaa
            19 hours ago








          • 3




            @Diaa I do not think your question is naive. Did I misread it? (Actually it took me a while to figure out how that works, and actually you can find some posts by high reputation users on this site who get it wrong IMHO, so I do not think this is a trivial question. But the decision of whether or not to delete is yours.)
            – marmot
            18 hours ago












          • I mean it is a stupid question XD that doesn't deserve to be asked :)
            – Diaa
            18 hours ago












          • @Diaa The question became stupid only after seeing the answer. So please don't delete the question. Instead, downvote for making the question look stupid ;)
            – nidhin
            15 hours ago















          up vote
          6
          down vote



          accepted










          The ordering is important. The TikZ parser parses from left to right. This means that you need to first set the node distance (locally) to 1cm by saying node distance = 1cm, and then let TikZ compute the actual coordinates of the C node by saying right = of A.



          documentclass[border=1cm]{standalone}
          usepackage{tikz}
          usetikzlibrary{shapes,arrows,positioning}

          begin{document}
          begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
          node [draw] (A) {A};
          node [draw, right = 1cm of A] (B) {B};
          node [draw, node distance = 1cm, right = of A] (C) {C};
          end{tikzpicture}
          end{document}


          enter image description here



          As you can see, now the B and C nodes are on top of each other.






          share|improve this answer























          • I didn't know my question is that naive
            – Diaa
            19 hours ago










          • I really appreciate your answer, but I think it is better to delete this question
            – Diaa
            19 hours ago








          • 3




            @Diaa I do not think your question is naive. Did I misread it? (Actually it took me a while to figure out how that works, and actually you can find some posts by high reputation users on this site who get it wrong IMHO, so I do not think this is a trivial question. But the decision of whether or not to delete is yours.)
            – marmot
            18 hours ago












          • I mean it is a stupid question XD that doesn't deserve to be asked :)
            – Diaa
            18 hours ago












          • @Diaa The question became stupid only after seeing the answer. So please don't delete the question. Instead, downvote for making the question look stupid ;)
            – nidhin
            15 hours ago













          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          The ordering is important. The TikZ parser parses from left to right. This means that you need to first set the node distance (locally) to 1cm by saying node distance = 1cm, and then let TikZ compute the actual coordinates of the C node by saying right = of A.



          documentclass[border=1cm]{standalone}
          usepackage{tikz}
          usetikzlibrary{shapes,arrows,positioning}

          begin{document}
          begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
          node [draw] (A) {A};
          node [draw, right = 1cm of A] (B) {B};
          node [draw, node distance = 1cm, right = of A] (C) {C};
          end{tikzpicture}
          end{document}


          enter image description here



          As you can see, now the B and C nodes are on top of each other.






          share|improve this answer














          The ordering is important. The TikZ parser parses from left to right. This means that you need to first set the node distance (locally) to 1cm by saying node distance = 1cm, and then let TikZ compute the actual coordinates of the C node by saying right = of A.



          documentclass[border=1cm]{standalone}
          usepackage{tikz}
          usetikzlibrary{shapes,arrows,positioning}

          begin{document}
          begin{tikzpicture}[auto, node distance=2cm,>=latex',align=center]
          node [draw] (A) {A};
          node [draw, right = 1cm of A] (B) {B};
          node [draw, node distance = 1cm, right = of A] (C) {C};
          end{tikzpicture}
          end{document}


          enter image description here



          As you can see, now the B and C nodes are on top of each other.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 19 hours ago

























          answered 19 hours ago









          marmot

          75.2k485159




          75.2k485159












          • I didn't know my question is that naive
            – Diaa
            19 hours ago










          • I really appreciate your answer, but I think it is better to delete this question
            – Diaa
            19 hours ago








          • 3




            @Diaa I do not think your question is naive. Did I misread it? (Actually it took me a while to figure out how that works, and actually you can find some posts by high reputation users on this site who get it wrong IMHO, so I do not think this is a trivial question. But the decision of whether or not to delete is yours.)
            – marmot
            18 hours ago












          • I mean it is a stupid question XD that doesn't deserve to be asked :)
            – Diaa
            18 hours ago












          • @Diaa The question became stupid only after seeing the answer. So please don't delete the question. Instead, downvote for making the question look stupid ;)
            – nidhin
            15 hours ago


















          • I didn't know my question is that naive
            – Diaa
            19 hours ago










          • I really appreciate your answer, but I think it is better to delete this question
            – Diaa
            19 hours ago








          • 3




            @Diaa I do not think your question is naive. Did I misread it? (Actually it took me a while to figure out how that works, and actually you can find some posts by high reputation users on this site who get it wrong IMHO, so I do not think this is a trivial question. But the decision of whether or not to delete is yours.)
            – marmot
            18 hours ago












          • I mean it is a stupid question XD that doesn't deserve to be asked :)
            – Diaa
            18 hours ago












          • @Diaa The question became stupid only after seeing the answer. So please don't delete the question. Instead, downvote for making the question look stupid ;)
            – nidhin
            15 hours ago
















          I didn't know my question is that naive
          – Diaa
          19 hours ago




          I didn't know my question is that naive
          – Diaa
          19 hours ago












          I really appreciate your answer, but I think it is better to delete this question
          – Diaa
          19 hours ago






          I really appreciate your answer, but I think it is better to delete this question
          – Diaa
          19 hours ago






          3




          3




          @Diaa I do not think your question is naive. Did I misread it? (Actually it took me a while to figure out how that works, and actually you can find some posts by high reputation users on this site who get it wrong IMHO, so I do not think this is a trivial question. But the decision of whether or not to delete is yours.)
          – marmot
          18 hours ago






          @Diaa I do not think your question is naive. Did I misread it? (Actually it took me a while to figure out how that works, and actually you can find some posts by high reputation users on this site who get it wrong IMHO, so I do not think this is a trivial question. But the decision of whether or not to delete is yours.)
          – marmot
          18 hours ago














          I mean it is a stupid question XD that doesn't deserve to be asked :)
          – Diaa
          18 hours ago






          I mean it is a stupid question XD that doesn't deserve to be asked :)
          – Diaa
          18 hours ago














          @Diaa The question became stupid only after seeing the answer. So please don't delete the question. Instead, downvote for making the question look stupid ;)
          – nidhin
          15 hours ago




          @Diaa The question became stupid only after seeing the answer. So please don't delete the question. Instead, downvote for making the question look stupid ;)
          – nidhin
          15 hours ago










          up vote
          3
          down vote













          The operation of the node distance key is particular and far from obvious. It only works if and only if there is an of part but no shift part.



          I quote p 231 of 3.0.1a manual:




          /tikz/node distance=<shifting part> (no default, initially 1cm and
          1cm) The value of this key is used as is used if and
          only if
          a <of-part> is present, but no <shifting part>.




          Look at this example.




          • Nodes colored blue do not have a shifting part in their code, so the node distance key is active.

          • The red colored nodes have a shifting part and therefore the node distance key is inactive.


          examples



          documentclass{book}
          usepackage{tikz}
          usetikzlibrary{positioning}
          begin{document}

          foreach i in {8,13,17}{
          begin{tikzpicture}[noeud/.style={draw,node distance=i mm},
          entre/.style={midway,draw=none,fill=white,inner sep =1pt}]
          draw[fill=green!10] (-1,-.1) rectangle (3,5);
          draw[help lines] (-1,-.1) grid (3,5);
          % No shifting part
          begin{scope}[every node/.append style={fill=blue!20,font=scriptsize}]
          node[noeud,align=center] (a1) at (0,0) {node distance\ actived};
          node[noeud] (b1) [above=of a1] {1};
          node[noeud] (c1) [above=of b1] {2};
          draw [<->](a1)--(b1)node[entre]{i mm};
          draw [<->](b1)--(c1)node[entre]{i mm};
          end{scope}
          % Shifting part
          begin{scope}[every node/.append style={fill=red!20,font=scriptsize}]
          node[noeud,align=center] (a2) at (2,0) {node distance\ inactived};
          node[noeud] (b2) [above=1cm of a2] {1};
          node[noeud] (c2) [above=1cm of b2] {2};
          draw [<->](a2)--(b2)node[entre]{1 cm};
          draw [<->](b2)--(c2)node[entre]{1 cm};
          end{scope}
          end{tikzpicture}
          }
          end{document}


          Your code is



          right = 1cm of A


          The shifting part is present and equal to 1 cm. So the key distance node=2cm is disabled. Point B is therefore located 1 cm from A as you have specified.



          Then for point C, you write this:



           right = of A, node distance = 1cm


          As there is no shifting part in this code, the distance node=2cm key is active and therefore the node is placed at 2cm. Then you specify this distance again, but it has already been computed and so pgf does nothing more, as you can see with a 10 cm distance node key.



          code



          documentclass{article}
          usepackage{tikz}
          usetikzlibrary{shapes,arrows,positioning}

          begin{document}
          begin{tikzpicture}[auto, node distance=2cm]
          node [draw] (A) {A};
          node [draw, right = 1cm of A] (B) {B};
          node [draw, right =of A,circle,fill=red!40,opacity=.5,inner sep =10pt] (B) {B};
          node [draw, right = of A, node distance = 10cm,fill=blue!40,opacity=.5,inner sep =10pt] (C) {C};
          node [draw, node distance = 3cm, right = of A,fill=violet,opacity=.5,inner sep =10pt] (D) {D};
          end{tikzpicture}
          end{document}


          Translated with www.DeepL.com/Translator






          share|improve this answer



















          • 1




            Many thanks for the comprehensive answer.
            – Diaa
            15 hours ago















          up vote
          3
          down vote













          The operation of the node distance key is particular and far from obvious. It only works if and only if there is an of part but no shift part.



          I quote p 231 of 3.0.1a manual:




          /tikz/node distance=<shifting part> (no default, initially 1cm and
          1cm) The value of this key is used as is used if and
          only if
          a <of-part> is present, but no <shifting part>.




          Look at this example.




          • Nodes colored blue do not have a shifting part in their code, so the node distance key is active.

          • The red colored nodes have a shifting part and therefore the node distance key is inactive.


          examples



          documentclass{book}
          usepackage{tikz}
          usetikzlibrary{positioning}
          begin{document}

          foreach i in {8,13,17}{
          begin{tikzpicture}[noeud/.style={draw,node distance=i mm},
          entre/.style={midway,draw=none,fill=white,inner sep =1pt}]
          draw[fill=green!10] (-1,-.1) rectangle (3,5);
          draw[help lines] (-1,-.1) grid (3,5);
          % No shifting part
          begin{scope}[every node/.append style={fill=blue!20,font=scriptsize}]
          node[noeud,align=center] (a1) at (0,0) {node distance\ actived};
          node[noeud] (b1) [above=of a1] {1};
          node[noeud] (c1) [above=of b1] {2};
          draw [<->](a1)--(b1)node[entre]{i mm};
          draw [<->](b1)--(c1)node[entre]{i mm};
          end{scope}
          % Shifting part
          begin{scope}[every node/.append style={fill=red!20,font=scriptsize}]
          node[noeud,align=center] (a2) at (2,0) {node distance\ inactived};
          node[noeud] (b2) [above=1cm of a2] {1};
          node[noeud] (c2) [above=1cm of b2] {2};
          draw [<->](a2)--(b2)node[entre]{1 cm};
          draw [<->](b2)--(c2)node[entre]{1 cm};
          end{scope}
          end{tikzpicture}
          }
          end{document}


          Your code is



          right = 1cm of A


          The shifting part is present and equal to 1 cm. So the key distance node=2cm is disabled. Point B is therefore located 1 cm from A as you have specified.



          Then for point C, you write this:



           right = of A, node distance = 1cm


          As there is no shifting part in this code, the distance node=2cm key is active and therefore the node is placed at 2cm. Then you specify this distance again, but it has already been computed and so pgf does nothing more, as you can see with a 10 cm distance node key.



          code



          documentclass{article}
          usepackage{tikz}
          usetikzlibrary{shapes,arrows,positioning}

          begin{document}
          begin{tikzpicture}[auto, node distance=2cm]
          node [draw] (A) {A};
          node [draw, right = 1cm of A] (B) {B};
          node [draw, right =of A,circle,fill=red!40,opacity=.5,inner sep =10pt] (B) {B};
          node [draw, right = of A, node distance = 10cm,fill=blue!40,opacity=.5,inner sep =10pt] (C) {C};
          node [draw, node distance = 3cm, right = of A,fill=violet,opacity=.5,inner sep =10pt] (D) {D};
          end{tikzpicture}
          end{document}


          Translated with www.DeepL.com/Translator






          share|improve this answer



















          • 1




            Many thanks for the comprehensive answer.
            – Diaa
            15 hours ago













          up vote
          3
          down vote










          up vote
          3
          down vote









          The operation of the node distance key is particular and far from obvious. It only works if and only if there is an of part but no shift part.



          I quote p 231 of 3.0.1a manual:




          /tikz/node distance=<shifting part> (no default, initially 1cm and
          1cm) The value of this key is used as is used if and
          only if
          a <of-part> is present, but no <shifting part>.




          Look at this example.




          • Nodes colored blue do not have a shifting part in their code, so the node distance key is active.

          • The red colored nodes have a shifting part and therefore the node distance key is inactive.


          examples



          documentclass{book}
          usepackage{tikz}
          usetikzlibrary{positioning}
          begin{document}

          foreach i in {8,13,17}{
          begin{tikzpicture}[noeud/.style={draw,node distance=i mm},
          entre/.style={midway,draw=none,fill=white,inner sep =1pt}]
          draw[fill=green!10] (-1,-.1) rectangle (3,5);
          draw[help lines] (-1,-.1) grid (3,5);
          % No shifting part
          begin{scope}[every node/.append style={fill=blue!20,font=scriptsize}]
          node[noeud,align=center] (a1) at (0,0) {node distance\ actived};
          node[noeud] (b1) [above=of a1] {1};
          node[noeud] (c1) [above=of b1] {2};
          draw [<->](a1)--(b1)node[entre]{i mm};
          draw [<->](b1)--(c1)node[entre]{i mm};
          end{scope}
          % Shifting part
          begin{scope}[every node/.append style={fill=red!20,font=scriptsize}]
          node[noeud,align=center] (a2) at (2,0) {node distance\ inactived};
          node[noeud] (b2) [above=1cm of a2] {1};
          node[noeud] (c2) [above=1cm of b2] {2};
          draw [<->](a2)--(b2)node[entre]{1 cm};
          draw [<->](b2)--(c2)node[entre]{1 cm};
          end{scope}
          end{tikzpicture}
          }
          end{document}


          Your code is



          right = 1cm of A


          The shifting part is present and equal to 1 cm. So the key distance node=2cm is disabled. Point B is therefore located 1 cm from A as you have specified.



          Then for point C, you write this:



           right = of A, node distance = 1cm


          As there is no shifting part in this code, the distance node=2cm key is active and therefore the node is placed at 2cm. Then you specify this distance again, but it has already been computed and so pgf does nothing more, as you can see with a 10 cm distance node key.



          code



          documentclass{article}
          usepackage{tikz}
          usetikzlibrary{shapes,arrows,positioning}

          begin{document}
          begin{tikzpicture}[auto, node distance=2cm]
          node [draw] (A) {A};
          node [draw, right = 1cm of A] (B) {B};
          node [draw, right =of A,circle,fill=red!40,opacity=.5,inner sep =10pt] (B) {B};
          node [draw, right = of A, node distance = 10cm,fill=blue!40,opacity=.5,inner sep =10pt] (C) {C};
          node [draw, node distance = 3cm, right = of A,fill=violet,opacity=.5,inner sep =10pt] (D) {D};
          end{tikzpicture}
          end{document}


          Translated with www.DeepL.com/Translator






          share|improve this answer














          The operation of the node distance key is particular and far from obvious. It only works if and only if there is an of part but no shift part.



          I quote p 231 of 3.0.1a manual:




          /tikz/node distance=<shifting part> (no default, initially 1cm and
          1cm) The value of this key is used as is used if and
          only if
          a <of-part> is present, but no <shifting part>.




          Look at this example.




          • Nodes colored blue do not have a shifting part in their code, so the node distance key is active.

          • The red colored nodes have a shifting part and therefore the node distance key is inactive.


          examples



          documentclass{book}
          usepackage{tikz}
          usetikzlibrary{positioning}
          begin{document}

          foreach i in {8,13,17}{
          begin{tikzpicture}[noeud/.style={draw,node distance=i mm},
          entre/.style={midway,draw=none,fill=white,inner sep =1pt}]
          draw[fill=green!10] (-1,-.1) rectangle (3,5);
          draw[help lines] (-1,-.1) grid (3,5);
          % No shifting part
          begin{scope}[every node/.append style={fill=blue!20,font=scriptsize}]
          node[noeud,align=center] (a1) at (0,0) {node distance\ actived};
          node[noeud] (b1) [above=of a1] {1};
          node[noeud] (c1) [above=of b1] {2};
          draw [<->](a1)--(b1)node[entre]{i mm};
          draw [<->](b1)--(c1)node[entre]{i mm};
          end{scope}
          % Shifting part
          begin{scope}[every node/.append style={fill=red!20,font=scriptsize}]
          node[noeud,align=center] (a2) at (2,0) {node distance\ inactived};
          node[noeud] (b2) [above=1cm of a2] {1};
          node[noeud] (c2) [above=1cm of b2] {2};
          draw [<->](a2)--(b2)node[entre]{1 cm};
          draw [<->](b2)--(c2)node[entre]{1 cm};
          end{scope}
          end{tikzpicture}
          }
          end{document}


          Your code is



          right = 1cm of A


          The shifting part is present and equal to 1 cm. So the key distance node=2cm is disabled. Point B is therefore located 1 cm from A as you have specified.



          Then for point C, you write this:



           right = of A, node distance = 1cm


          As there is no shifting part in this code, the distance node=2cm key is active and therefore the node is placed at 2cm. Then you specify this distance again, but it has already been computed and so pgf does nothing more, as you can see with a 10 cm distance node key.



          code



          documentclass{article}
          usepackage{tikz}
          usetikzlibrary{shapes,arrows,positioning}

          begin{document}
          begin{tikzpicture}[auto, node distance=2cm]
          node [draw] (A) {A};
          node [draw, right = 1cm of A] (B) {B};
          node [draw, right =of A,circle,fill=red!40,opacity=.5,inner sep =10pt] (B) {B};
          node [draw, right = of A, node distance = 10cm,fill=blue!40,opacity=.5,inner sep =10pt] (C) {C};
          node [draw, node distance = 3cm, right = of A,fill=violet,opacity=.5,inner sep =10pt] (D) {D};
          end{tikzpicture}
          end{document}


          Translated with www.DeepL.com/Translator







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 15 hours ago

























          answered 16 hours ago









          AndréC

          5,6271937




          5,6271937








          • 1




            Many thanks for the comprehensive answer.
            – Diaa
            15 hours ago














          • 1




            Many thanks for the comprehensive answer.
            – Diaa
            15 hours ago








          1




          1




          Many thanks for the comprehensive answer.
          – Diaa
          15 hours ago




          Many thanks for the comprehensive answer.
          – Diaa
          15 hours ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f460154%2ftikz-how-does-global-local-node-distance-work%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

          サソリ

          広島県道265号伴広島線

          Accessing regular linux commands in Huawei's Dopra Linux