19

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

enter image description here

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.

dineshdileep
  • 1,429

2 Answers2

20

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}
Jake
  • 232,450
  • +1, useful answer, but I had to go through the pain of figuring out how to install those packages. – dineshdileep Jun 03 '13 at 05: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 03 '13 at 05: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 03 '13 at 05: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 04 '13 at 04: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 04 '13 at 11:57
  • what do you mean by edges? are A.south and A.west edges or anchors? that is what I didn't understand. – dineshdileep Jun 04 '13 at 13:28
  • @dineshdileep: Sorry, maybe "edge" isn't the right word: I mean the border of the node (the rectangles around the words "Node A" and "Node B", in this case). A.south is an anchor, in this case a particular point on the border of the node. – Jake Jun 04 '13 at 13:30
  • Thus, as I understand, A.south and A.west both lie on the border of the node, why didn't tikz select A.west? Is it kind of a shortest distance rule? – dineshdileep Jun 04 '13 at 13:41
  • @dineshdileep: It's determined by the direction of the line: In the case of \draw (A) -- (B), TikZ will use the point on the border of node A in the direction of node B. If you use \draw (A) |- (B), it will use either A.north or A.south (depending on whether node B is above or below node A). – Jake Jun 04 '13 at 13:48
19

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:

Sample output

\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]

Sample stealth arrow

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:

Sample decoration

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.

Andrew Swann
  • 95,762