2

I am using the following codes to get my graph,

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}

\begin{tikzpicture}[->,auto, node distance=3cm, 
                    thick,main node/.style={font=\sffamily\large\bfseries$\bullet$}]

  \node[main node] (1) {1};
  \node[main node] (2) [below left of=1] {2};
  \node[main node] (3) [below  of=1] {3};
  \node[main node] (4) [below right of=1] {4};
  \node[main node] (5) [below of =3] {5}; 

  \path[every node/.style={font=\sffamily\small}]
    (1) edge node [right] {$e_2$} (3)
    (2) edge node [left] {$e_1$} (1)
    (3) edge node [right] {$e_5$} (5)
    (4) edge [left] node[right] {$e_3$} (1)
    (5) edge node[left] {$e_4$} (2)
        edge node[right] {$e_6$} (4);
\end{tikzpicture}

\end{document}

It gave the result like this,

enter image description here

But I want the figure like,

enter image description here

Here, the nodes are properly organised. How to do that?

David
  • 1,964
  • See also http://tex.stackexchange.com/questions/302149/why-positioning-above-is-at-a-different-height-than-above-right – Rmano Apr 03 '16 at 08:16

1 Answers1

2

With help of TikZ library positioning is this easy to achieve:

enter image description here

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows,quotes,positioning}

\begin{document}
    \begin{tikzpicture}[->,auto, 
    node distance=2cm and 3cm, % changed
    thick,
    main node/.style={font=\sffamily\large\bfseries$\bullet$},
every edge node/.style={font=\sffamily\small}
                    ]
  \node[main node] (1) {1};
  \node[main node] (2) [below  left=of 1] {2};
  \node[main node] (3) [below=of 1] {3};
  \node[main node] (4) [below right=of 1] {4};
  \node[main node] (5) [below= of 3] {5};

  \draw
    (1) edge["$e_2$"] (3)
    (2) edge["$e_1$"] (1)
    (3) edge["$e_5$"] (5)
    (4) edge["$e_3$" '] (1)
    (5) edge["$e_4$"] (2)
        edge["$e_6$" '] (4);
    \end{tikzpicture}
\end{document}

Main differences in positioning is define node distance=<vertical distance> and <horizontal distance>, in your case node distance=2cm and 3cm. To youse positioning library you also need to change syntax for node placement. Correct one is for example below left=of 1 (see different position of =!).

I also take liberty and change a way to labeled path. With quotes library is more shorter.

Of course, this diagram you can draw on different, probably more efficient way as automaton with help of automata library. For instruction see section 41 Automata Drawing Library, page 513 of TikZ manual (version 3.0.1a)

Zarko
  • 296,517