# Different horizontal and vertical spacing between nodes in a Graph using tikz

I am drawing a simple graph and I would like to have more spacing in the x direction than in y. However, using "node distance=4cm" I can only control the distance.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit,automata}

\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=4cm,
semithick]
\tikzstyle{every state}=[circle, black, draw=red, very thick]
\node[state]          (1)       [label=above:$k_1 {=} 10$]              {$1$};
\node[state]         (2) [below of=1]   [label=below:$k_2 {=} -4$]   {$2$};
\node[state]         (3) [ right of=1] [label=above:$k_3 {=} -8$]{$3$};
\node[state]         (4) [below of=3][label=below:$k_4 {=} 6$] {$4$};

\path (1) edge        node [sloped,above] {2, 8, 1} (2)
(1) edge              node[sloped,above,near start]  {0, 2, 7} (4)
(1) edge              node[sloped,above]  {4, 12, 3} (3)

(2) edge              node[sloped,above,near start]  {2, 4} (3)
(2) edge              node[sloped,above ]  {3, 3} (4)

(3) edge              node [sloped,above] {3,2} (4)
;
\end{tikzpicture}
\end{document}



With the positioning tikzlibrary syntax (below=of 1), and using node distance=2cm and 6cm (vertical and horizontal shift):

MWE

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, matrix,shapes,arrows,fit,automata}

\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2cm and 6cm,
semithick]
\tikzstyle{every state}=[circle, black, draw=red, very thick]
\node[state]          (1)       [label=above:$k_1 {=} 10$]              {$1$};
\node[state]         (2) [below=of 1]   [label=below:$k_2 {=} -4$]   {$2$};
\node[state]         (3) [ right=of 1] [label=above:$k_3 {=} -8$]{$3$};
\node[state]         (4) [below=of 3][label=below:$k_4 {=} 6$] {$4$};

\path (1) edge        node [sloped,above] {2, 8, 1} (2)
(1) edge              node[sloped,above,near start]  {0, 2, 7} (4)
(1) edge              node[sloped,above]  {4, 12, 3} (3)

(2) edge              node[sloped,above,near start]  {2, 4} (3)
(2) edge              node[sloped,above ]  {3, 3} (4)

(3) edge              node [sloped,above] {3,2} (4)
;
\end{tikzpicture}
\end{document}



Manual p 243 illustrates the difference between the two node distance specifications, in terms of how the shift difference is calculated.

• Very elegant. Thanks! May 30, 2020 at 18:57

You can use the yshift-command as follows:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit,automata}

\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=4cm,
semithick]
\tikzstyle{every state}=[circle, black, draw=red, very thick]
\node[state]          (1)       [label=above:$k_1 {=} 10$]              {$1$};
\node[state]         (2) [below of=1,yshift=-1cm]   [label=below:$k_2 {=} -4$]   {$2$};
\node[state]         (3) [ right of=1] [label=above:$k_3 {=} -8$]{$3$};
\node[state]         (4) [below of=3,yshift = -1cm][label=below:$k_4 {=} 6$] {$4$};

\path (1) edge        node [sloped,above] {2, 8, 1} (2)
(1) edge              node[sloped,above,near start]  {0, 2, 7} (4)
(1) edge              node[sloped,above]  {4, 12, 3} (3)

(2) edge              node[sloped,above,near start]  {2, 4} (3)
(2) edge              node[sloped,above ]  {3, 3} (4)

(3) edge              node [sloped,above] {3,2} (4)
;
\end{tikzpicture}
\end{document}

• Thanks for the quick answer. You have been most helpful! May 29, 2020 at 12:18