99

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.

3 Answers 3

107

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.

0
81

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

3
  • (+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
    Commented Feb 18, 2015 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
    Commented Feb 18, 2015 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
    Commented Feb 18, 2015 at 0:15
32

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

18
  • This is definitely the simplest solution; thank you for the help!
    – Michael A
    Commented Feb 18, 2015 at 0:09
  • 6
    There's a simpler one: (a) edge[bend left] (b)
    – yo'
    Commented Feb 18, 2015 at 0:29
  • 1
    No, apart from it not recognizing the "stealth" arrowtip, everything else works perfectly fine. That's just because I recently found out that in order to use the increased functionality of arrows.meta, one must capitalize the first letter of the old arrows keyword names: tex.stackexchange.com/questions/717716/… Everything else ran perfectly fine. I'm sorry I didn't test-run your code before asking. I just assumed there could've been perhaps significant reworks required for it to be compatible with the new package.
    – Atex
    Commented May 12 at 8:23
  • 1
    @Atex OK, good. Yes, you'd have to capitalise it to use Stealth. I don't know if all the names are the same, but the meta ones are always capitalised. Glad it still works, at least! Note that with arrows.meta you can also use the bending library, if need be. If you have dramatic curves, you can then put -Stealth[bend], for example.
    – cfr
    Commented May 12 at 16:07
  • 1
    I see, thanks for the clarifications, it's been a very beneficial conversation in helping me understand the tikz environment better!
    – Atex
    Commented May 13 at 8:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .