11

Use positioning can control the individual vertical position of nodes. But is there any way to control the default vertical spacing?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}

\begin{document}
\pagestyle{empty}

\tikzstyle{decision} = [diamond, draw, text width=4.5em, text badly centered, node distance=3.5cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, text width=6em, text centered, rounded corners, minimum height=4em, node distance=3.5cm,minimum height=2em]
\tikzstyle{cloud} = [draw, ellipse, node distance=3.5cm, minimum height=2em]
\tikzstyle{line} = [draw, -latex']

\begin{tikzpicture}[node distance = 1cm, auto]
  \node [block] (1st) {1st};
  \node [block, right of=1st]  (2nd1) {2nd1};
  \node [block, above=0.7cm of 2nd1] (2nd2) {2nd2};
  \node [block, below of=2nd1] (2nd3) {2nd3};
  \node [block, right of=2nd1] (3rd1) {3rd1};
  \node [block, above of=3rd1] (3rd2) {3rd2};
  \node [block, above of=3rd2] (3rd3) {3rd3};
  \node [block, below of=3rd1] (3rd4) {3rd4};
  \node [block, below of=3rd4] (3rd5) {3rd5};

  \path [line] (1st) -- (2nd1);
  \path [line] (2nd1) -- (2nd2);
  \path [line] (2nd1) -- (2nd3);
  \path [line] (2nd2) -- (3rd3);
  \path [line] (2nd1) -- (3rd1);
  \path [line] (1st) -- (2nd1);
\end{tikzpicture}

\end{document}

Like this. I changed the vertical position of 2nd2 but others remains. Do I have to add individual vertical spacing control of every nodes?

  • 1
    You can say node distance=1cm and 5mm instead of a single length for both. But please include full MWEs your code is not compilable because it doesn't have block and line styles – percusse May 20 '13 at 12:22
  • Please read the description of a minimal working example (MWE). – Marco Daniel May 20 '13 at 13:43
  • 1
    I have included the full MWE. Thank you for your patience. – Cheng Zhou May 20 '13 at 14:37
  • 2
    OMG, it took me several hours, but the difference (the problem) is that you have to write above=of and not above of=, the = is placed differently and that changes the behaviour... – Trylks Mar 14 '14 at 14:18
14

I don't really get the question so I hope this is what you wanted. If you include a full document (such that we copy paste and see the problem on our systems) things are much more easier.

Here, you can change the default setting within a scope but your block style had a node distance which was resetting every time it is issued. I've made it 2mm such that we can see the difference easier.

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows,shapes.geometric,positioning}

\begin{document}
\begin{tikzpicture}[decision/.style={diamond, draw, text width=4.5em, text badly centered, node distance=3.5cm, inner sep=0pt},
                    block/.style   ={rectangle, draw, text width=6em, text centered, rounded corners, minimum height=4em, minimum height=2em},
                    cloud/.style   ={draw, ellipse, minimum height=2em},
                    line/.style    ={draw,-latex'},
                    node distance = 1cm, 
                    auto]
    \node [block] (1st) {1st};
    \node [block, right= of 1st]  (2nd1) {2nd1};
\begin{scope}[node distance=2mm and 10mm]%Here we change it for everything inside this scope
    \node [block, above= of 2nd1] (2nd2) {2nd2};
    \node [block, below= of 2nd1] (2nd3) {2nd3};
    \node [block, right= of 2nd1] (3rd1) {3rd1};
    \node [block, above= of 3rd1] (3rd2) {3rd2};
    \node [block, above= of 3rd2] (3rd3) {3rd3};
\end{scope}
    \node [block, below= of 3rd1] (3rd4) {3rd4};
    \node [block, below= of 3rd4] (3rd5) {3rd5};

    \path [line] (1st) -- (2nd1);
    \path [line] (2nd1) -- (2nd2);
    \path [line] (2nd1) -- (2nd3);
    \path [line] (2nd2) -- (3rd3);
    \path [line] (2nd1) -- (3rd1);
    \path [line] (1st) -- (2nd1);
\end{tikzpicture}
\end{document}

enter image description here

  • 6
    OMG, it took me several hours, but the difference (the problem) is that you have to write above=of and not above of=, the = is placed differently and that changes the behaviour... – Trylks Mar 14 '14 at 14:25
10

You can change the default by putting

\tikzset{node distance = 1cm and 2cm}

where the first parameter is the vertical node distance and the second is the horizontal node distance. If you only put one parameter, both the vertical and horizontal node distances will be set to that distance.

As Trylks pointed out in the comments, in the node properties, use something like above= of 2nd1 instead of above of= 2nd1.

  • This does nothing for me, even with \usetikzlibrary{positioning}... Edit: forget it. I was using above of= instead of above=of. – Ricardo Cruz Sep 1 '17 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.