55

I have this code

\documentclass[10pt]{article}

\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}

\begin{tikzpicture}[->,>=stealth',auto,node distance=3cm,
  thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries}]

  \node[main node] (1) {a};
  \node[main node] (2) [right of=1] {b};
  \node[main node] (3) [right of=2] {c};
  \node[main node] (4) [right of=3] {d};

  \path[every node/.style={font=\sffamily\small}]
    (1) edge node [right] {} (2)
    (2) edge node [right] {} (3)
    (3) edge node [right] {} (4)
    (4) edge node [left] {} (1);
\end{tikzpicture}
\end{document}

that produces this graph:

bad graph

My goal is to produce a graph like this:

good graph

Pardon the bad drawing. I tried various combinations of (4) edge node [bend left] {} (1); and (4) edge node [loop left] {} (1); to no avail.

60

Just to show another approach, this use case is exactly what the keys bend left and bend right were created for:

\documentclass[10pt]{article}

\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}

\begin{tikzpicture}[->,>=stealth',auto,node distance=3cm,
  thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries}]

  \node[main node] (1) {a};
  \node[main node] (2) [right of=1] {b};
  \node[main node] (3) [right of=2] {c};
  \node[main node] (4) [right of=3] {d};

  \path[every node/.style={font=\sffamily\small}]
    (1) edge node [right] {} (2)
    (2) edge node [right] {} (3)
    (3) edge node [right] {} (4)
    (4) edge[bend right] node [left] {} (1);
\end{tikzpicture}
\end{document}

enter image description here

These keys also accept an optional <angle> value to simultaneously set the in and out keys symmetrically, so writing

(4) edge[bend right=90] node [left] {} (1);

will result in

enter image description here

If asymmetric setting of the in and out keys is required, cfr's solution is the way to go.

  • 1
    Thank you; you're right that this is much simpler, and in hindsight, it's quite intuitive. – Michael A Feb 18 '15 at 0:53
36

One approach is to use \draw and specify the incoming and outgoing angles. Simply specifying the node names will construct the path with respect to the node center (although not draw from there). You can also specify node anchors. For example, the red line connects (4.north) to (1.north).

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}[->,>=stealth',auto,node distance=3cm,
  thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries}]
\node[main node] (1) {a};
\node[main node] (2) [right of=1] {b};
\node[main node] (3) [right of=2] {c};
\node[main node] (4) [right of=3] {d};

\draw [->] (1) -- (2);
\draw [->] (2) -- (3);
\draw [->] (3) -- (4);
\draw [->] (4) to [out=150,in=30] (1);
\draw [->,red] (4.north) to [out=150,in=30] (1.north);
\end{tikzpicture}
\end{document}

enter image description here

  • (+1) But it won't start from the centre of the node if you omit the explicit anchor. It will try to do something sensible instead. (See my example below which doesn't specify an explicit anchor but where the lines drawn still start from the edge. Indeed, your straight lines show the same phenomenon. Perhaps I didn't understand your point?) – cfr Feb 18 '15 at 0:10
  • I do see now what you are trying to say but it is a bit confusing, because the path is created with \draw but, clearly, it is not drawn from the center! – cfr Feb 18 '15 at 0:14
  • @cfr I meant the path is calculated with respect to the node center, but is not necessarily drawn all the way to the center. The arrow in the OP's figure was from the top of one node to the top of another, so I was just trying to illustrate how using different anchors can affect placement. – erik Feb 18 '15 at 0:15
18

There are several ways to draw curves, if that's the question. Here's one:

\documentclass[tikz]{standalone}

\usetikzlibrary{arrows}

\begin{document}

\begin{tikzpicture}
  [
    ->,
    >=stealth',
    auto,node distance=3cm,
    thick,
    main node/.style={circle, draw, font=\sffamily\Large\bfseries}
    ]

  \node[main node] (1) {a};
  \node[main node] (2) [right of=1] {b};
  \node[main node] (3) [right of=2] {c};
  \node[main node] (4) [right of=3] {d};

  \path[every node/.style={font=\sffamily\small}]
    (1) edge node [right] {} (2)
    (2) edge node [right] {} (3)
    (3) edge node [right] {} (4);
  \draw
    (4) [out=150, in=20] to  (1);
\end{tikzpicture}
\end{document}

curve back

Here's a way to make it part of the original \path using edge (and varying the angles a bit):

  \path[every node/.style={font=\sffamily\small}]
    (1) edge node [right] {} (2)
    (2) edge node [right] {} (3)
    (3) edge node [right] {} (4)
    (4) edge [out=150, in=90]  (1);

edge in path curves

  • This is definitely the simplest solution; thank you for the help! – Michael A Feb 18 '15 at 0:09
  • 3
    There's a simpler one: (a) edge[bend left] (b) – yo' Feb 18 '15 at 0:29
  • @yo' But that gives you less control e.g. if the intermediate notes are bigger and you need a steeper angle to go around them or whatever. So it is not really simpler because you need a second method for the cases where it isn't suitable. – cfr Feb 18 '15 at 1:41
  • edge[bend left] is immediate and intuitive, whereas it's not immediate what in and out control and what would be, say, the values of in and out that would recreate the effect of bend left, let alone some other effect. (full disclosure: I haven't read the manual) – PatrickT Sep 27 '17 at 18:26

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.