6

I want to connect two nodes with an arrow, but I want the arrow to follow a "customized" path.

Instead of the simple way: enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning,mindmap,trees,automata}

\begin{document}
\begin{tikzpicture}
   \node[state,initial,initial text=G] (1)   {$1$}; 
   \node[state] (2) [right=of 1] {$2$}; 

    \draw[->] (1) edge  node[sloped, above] {a} (2);
\end{tikzpicture}
\end{document}

I want the arrow to be like this: enter image description here

Not the prettiest drawing but I hope you understand what I want. Thank you very much in advance! :)

  • 1
    While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. This is especially important for tikz as there are numerous libraries. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. – Peter Grill Jan 20 '16 at 8:58
  • Thank you for the comment, I have now included all of my code for this :) – Maggie Jan 20 '16 at 9:04
10

There are several ways of solving this, here is one using the |- path specification (meaning go first vertically, then horizontally, to the next coordinate) and one relative coordinate.

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,automata}

\begin{document}
\begin{tikzpicture}
   \node[state,initial,initial text=G] (1)   {$1$}; 
   \node[state] (2) [right=of 1] {$2$}; 
   \draw[->,rounded corners] (1) |- node[pos=0.75,fill=white,inner sep=2pt]{a} ++(1,1) |- (2);
\end{tikzpicture}
\end{document}
  • Thank you very much for your help! :) Is there also a way to make the edges rounded? – Maggie Jan 20 '16 at 19:12
  • 3
    @Maggie Sure, add rounded corners to the \draw options, see updated answer. You can also specify a radius with rounded corners=<radius>, e.g. rounded corners=5pt. Note that with a large radius there were some artifacts near the edge of the a node, which is why I also increased the inner sep a bit. – Torbjørn T. Jan 20 '16 at 21:12
4

One could solve it - rather unelegant - with control points inbetween. I don't know why the last line is not perfectly horizontal, though.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,automata}

\begin{document}
\begin{tikzpicture}
    \node[state,initial,initial text=G] (1) {$1$}; 
    \node (ctrl1) [above=of 1, anchor=north] {}; 
    \node (ctrl2) [right=of ctrl1, anchor=east] {}; 
    \node (ctrl3) [below=of ctrl2, anchor=north] {}; 
    \node[state] (2) [right=of 1] {$2$}; 
    \draw (1) -- (ctrl1.south) -- node [fill=white] {a} (ctrl2.south) -- (ctrl3.south) -- (2);
\end{tikzpicture}
\end{document}

Screenshot

  • 3
    because you go from ctrl3.south to 2.west, I think. Replace \node(ctrl3) by \coordinate(ctrl3) and ctrl3.south by ctrl3 – pluton Jan 20 '16 at 21: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.