40

I am currently trying to work out how the perpendicular arrows work, and I fail.

Taking:

\draw [arrow] (node1) -- node[sloped, above] {Caption} (node1 |- node2.north west);

I suspected that the notation meant:
Draw an arrow from node 1, to the north-west corner of node 2.
Make the arrow perpendicular to node 1.

Further attempts to draw arrows told me: No, that's not it. I have sadly failed to figure out what exactly the notation is supposed to be doing though! Is there any "explanation for dummies" that will lead me to understand what exactly

node1 |-

Is supposed to do?

Thanks!

Layna
  • 547
  • 1
    This is clearly explained in section "13.3.1 Intersections of Perpendicular Lines" in the TikZ and PGF manual (page 141 in v 3.1.4a). – Zarko Jul 30 '19 at 20:09

2 Answers2

57

There are two different places to use -|/|-:

  1. In a coordinate specification.

    This is what you have used, the general form is

    (a -| b)
    

    where a and b are named nodes or coordinates. This means the coordinate that is at the y-coordinate of a, and x-coordinate of b. Similarly, (a |- b) has the x-coordinate of a and y-coordinate of b.

    For example, the following code draws a horizontal arrow from a at (0,0) to (1,0).

    \documentclass[border=10pt]{standalone}
    \usepackage{tikz}
    \begin{document}
    \begin{tikzpicture}
    \coordinate (a) at (0,0);
    \coordinate (b) at (1,1);
    
    \draw [->] (a) -- (a -| b);
    \end{tikzpicture}
    \end{document}
    

    You can also use this as

    \coordinate (c) at (a -| b);
    

    and then \draw [->] (a) -- (c); does the same as the above.

  2. As a path specification.

    This is used between two coordinates, in place of --.

    With \draw [->] (a) -| (b); the arrow goes horizontally from a, then vertically up to b. (And with |- it would be vertically first, then horizontally.)

    output of code

    \documentclass[border=10pt]{standalone}
    \usepackage{tikz}
    \begin{document}
    \begin{tikzpicture}
    \coordinate (a) at (0,0);
    \coordinate (b) at (1,1);
    
    \draw [->] (a) -| (b);
    \end{tikzpicture}
    \end{document}
    
Torbjørn T.
  • 206,688
  • 15
    The analogy between the coordinate and path specifications can be seen by noting that \draw (a) -| (b); is equivalent to \draw (a) -- (a-|b) -- (b) ;. – And R Nov 15 '17 at 22:11
  • 1
    For anyone else who has issues deducing what exactly - and | mean: I define | as the vertical (y)-axis, and - as the horizontal (x)-axis. The notation now means "ignores the value of", i.e. (a |- b) means that "the y-axis of a and the x-axis of b is ignored", which in turn leads to the x value of a and the y value of b for that particular coordinate. – Lennart Aug 20 '22 at 11:47
11

If a is a coordinate/node at position (a_x, a_y) and
b is at (b_x, b_y) then

a |- b := (a_x, b_y) 

a -| b := (b_x, a_y) 

They're just shorthands for combining the x/y coordinates of two points.

CarLaTeX
  • 62,716