# Drawing a polyline between two nodes in tikz

I am a beginner with tikz and finding the following really difficult to do.

I have defined and drawn node1 and node2 in tikz. You can figure it out easily from the picture. What I intend to do is

• Draw a dashed line from (node1.south) to P1.

• Draw a dashed line from P1 to P2.

• Draw a pointed dashed line from P2 to (node2.north).

• where the point P1 is (node1.south)+(0,-y). y is a positive integer which I am free to choose depending on y-axis distance between node1 and node2.

• and I define the point P2 as the point where a horizontal left-ward line from P1 meets the vertical upward line from (node2.north).
This thing roughly accomplished in google drawing is shown.

Qrrbrbirlbel's great paths.ortho library is perfect for this. Using the library, you don't have to calculate the support points manually, but instead you can simply write

\draw [dashed, -latex] (A) |-| (B);


to get

or

\draw [dashed, -latex, hvvh/ratio=0.7] (A) |-| (B);


to get

Together with Luigi's awesome arrows.new library, which allows you to scale arrow tips without changing the line widths or resorting to decorations, you can write

\draw [dashed, -latex new, arrow head=3mm, hvvh/ratio=0.7] (A) |-| (B);


to get

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{paths.ortho, arrows.new}
\begin{document}
\begin{tikzpicture}
\node [draw] (A) {Node A};
\node [draw] (B) at (-1,-2) {Node B};
\draw [dashed, -latex new, arrow head=3mm, hvvh/ratio=0.7] (A) |-| (B);
\end{tikzpicture}
\end{document}

• +1, useful answer, but I had to go through the pain of figuring out how to install those packages. – dineshdileep Jun 3 '13 at 5:40
• Also, what does hvvh/ratio do ? and when you write "(A) |-| (B);", how does it know to start from nodeA.south to nodeB.north , as opposed to an option like nodeA.west to nodeB.north. – dineshdileep Jun 3 '13 at 5:42
• @dineshdileep: Sorry, I should have mentioned how to use the packages (you don't actually need to install them, it's enough to have the files in the same directory as your .tex file). hvvh/ratio determines the vertical position of the horizontal line: A value of 0.5 places it in the middle, 0 and 1 places it at the level of the endpoints. The line always starts at the edge of the node, like it would when you say (A) -- (B) or (A) |- (B) (those are standard TikZ commands). – Jake Jun 3 '13 at 5:45
• What do you mean by edge of the node? I am sorry I have to ask this much, I am just learning tikz. – dineshdileep Jun 4 '13 at 4:16
• @dineshdileep: In my example, there are two nodes, (A) and (B). When you say \draw (A.center) -- (B.center), TikZ draws a line that connects the centers of the two nodes. If, however, you simply say \draw (A) -- (B), without specifying a particular anchor (like .center or .west), the line will not connect the centers of the nodes, but their edges. The same thing happens in my answer. Does that clear things up a little bit? See "Connecting Nodes: Using Nodes as Coordinates" in the PGF manual for a more detailed explanation. – Jake Jun 4 '13 at 11:57

General points can be computed using the calc library of tikz. An expression of the form

($x!t!y$)


will find the point a fraction t along the line from x to y. Combining this with drawing directives |- and -| for "vertical then horizontal" and vice versa gives:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}
\node[rectangle,fill=blue!30,draw] (A) at (6,4) {Node 1};
\node[rectangle,fill=blue!30,draw] (B) at (0,0) {Node 2};
\draw[dashed,->] (A.south) |- ($(A)!.7!(B)$) -| (B.north);
\end{tikzpicture}

\end{document}


There are different ways to change the arrow. Firstly, just writing

\draw[dashed,->,>=stealth]


will give another shape. Loading the tikz library arrows gives plenty to choose between. Making the line thicker will also make the arrow more prominent, e.g. the combination

\draw[dashed,->,>=stealth,very thick]


Note that you could start your picture with \begin{tikzpicture}[>=stealth,very thick] to make all arrowheads of stealth type, and all lines very thick.

For more control over the arrow, you can use the decorations mechanism. In your case this could look like:

from the following code

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc,arrows,decorations.markings}

\begin{document}

\begin{tikzpicture}[>=stealth,decoration={markings,mark=at position 1 with
{\arrow[scale=3]{>}}}]
\node[rectangle,fill=blue!30,draw] (A) at (6,4) {Node 1};
\node[rectangle,fill=blue!30,draw] (B) at (0,0) {Node 2};
\draw[dashed,postaction={decorate}]
(A.south) |- ($(A)!.7!(B)$) -| (B.north);
\end{tikzpicture}

\end{document}


See the pgf manual for further details and constructions. Another illustration of arrow tips via decorations may be found at TikZ: Large arrow tips at the end of smooth curves.

• +1, nice!, also can you please tell me how to make the arrow head more pronounced and thick. – dineshdileep Jun 2 '13 at 15:06
• Usefull answer and your help is appreciated, I really wish I could accept your answer along with the other one. – dineshdileep Jun 4 '13 at 4:17