15

Based on this tutorial, I understand symbols -| and |- are used to draw perpendicular lines.

But I wish to understand the difference between the two.

There are the following related questions:

  1. Insertion of perpendicular symbol at intersection of two perpendicular lines
  2. How to add perpendicular symbol at desired location
  3. How to Mark Right Angle in Tikz

But none of them seem to use the approach of -| and |-.

subham soni
  • 9,673

3 Answers3

27

Understand it as it looks like:

  • -| is "horizontal line → vertical line":

    \documentclass[tikz]{standalone}
    \begin{document}
    \begin{tikzpicture}
    \draw (0,0) coordinate (1) node[below] {$(0,0)$};
    \draw (2,2) coordinate (2) node[above] {$(2,2)$};
    \draw (1) -| (2);
    % -------------
    \draw (4,2) coordinate (x) node[above] {$(4,2)$};
    \draw (6,0) coordinate (y) node[below] {$(6,0)$};
    \draw (x) -| (y);
    \end{tikzpicture}
    \end{document}
    

    enter image description here

    Mathematically, (x,y) -| (a,b) and (x,y) -- (a,y) -- (a,b) are the same.

  • |- is "vertical line → horizontal line":

    \documentclass[tikz]{standalone}
    \begin{document}
    \begin{tikzpicture}
    \draw (0,0) coordinate (1) node[below] {$(0,0)$};
    \draw (2,2) coordinate (2) node[above] {$(2,2)$};
    \draw (1) |- (2);
    % -------------
    \draw (4,2) coordinate (x) node[above] {$(4,2)$};
    \draw (6,0) coordinate (y) node[below] {$(6,0)$};
    \draw (x) |- (y);
    \end{tikzpicture}
    \end{document}
    

    enter image description here

    Mathematically, (x,y) |- (a,b) and (x,y) -- (x,b) -- (a,b) are the same.

They are clearly very different.

21

I'd like to add to JouleV's answer another use of -| and |-.

Given two nodes, A and B:

  • if you use (A |- B) you have a point with the x coordinate of A and the y coordinate of B
  • if you use (A -| B) you have a point with the x coordinate of B and the y coordinate of A.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}
\node[draw] (A) {A};
\node[draw, above right =4cm of A] (B) {B};
\node[draw] at (A |- B) {$x$ of A, $y$ of B};
\node[draw] at (A -| B) {$x$ of B, $y$ of A};
\end{tikzpicture}
\end{document}

enter image description here

CarLaTeX
  • 62,716
6

PSTricks version for @CarLaTeX's explanation:

  • (A|-B) (TikZ) = (A|B) (PSTricks)
  • (A-|B) (TikZ) = (B|A) (PSTricks)
Display Name
  • 46,933