[Tex/LaTex] Vertical and horizontal lines in pgf-tikz

tikz-pgf

It is easy to produce a path between two nodes consisting of a single horizontal and a single vertical line (from the manual):

\begin{tikzpicture}
  \draw (0,0) node(a) [draw] {A}  (1,1) node(b) [draw] {B};
  \draw (a.north) |- (b.west);
  \draw[color=red] (a.east) -| (2,1.5) -| (b.north);
\end{tikzpicture}

However, what if I wanted the same short syntax to produce a path between (a) and (b) using a horizontal line, a vertical line, and a second horizontal line. It isn't to difficult to calculate the intermediate points, but how could the following syntax be defined?

\begin{tikzpicture}
  \draw (0,0) node(a) [draw] {A}  (3,1) node(b) [draw] {B};
  \draw (a.east) -|- (b.west);
\end{tikzpicture}

Best Answer

This answer extends Marc van Dongen's anwser.

The two styles are -|- and |-| with default value 0.5 to position the intermediate point (so, default is middle point).

enter image description here

\documentclass[margin=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{
  -|-/.style={
    to path={
      (\tikztostart) -| ($(\tikztostart)!#1!(\tikztotarget)$) |- (\tikztotarget)
      \tikztonodes
    }
  },
  -|-/.default=0.5,
  |-|/.style={
    to path={
      (\tikztostart) |- ($(\tikztostart)!#1!(\tikztotarget)$) -| (\tikztotarget)
      \tikztonodes
    }
  },
  |-|/.default=0.5,
}
\begin{document}
\begin{tikzpicture}[thick]
  \draw[->,blue] (2.1,1.1) to[|-|] (1.1,0.1);
  \draw[->] (2,1) to[-|-] (1,0);

  \draw[->,blue] (3.1,0.1) to[|-|] (4.1,1.1);
  \draw[->] (3,0) to[-|-] (4,1);

  \begin{scope}[yshift=-1.5cm]
    \draw[->,red] (2.1,1.1) to[|-|=.2] (1.1,0.1);
    \draw[->,orange] (2,1) to[-|-=.2] (1,0);

    \draw[->,red] (3.1,0.1) to[|-|=.8] (4.1,1.1);
    \draw[->,orange] (3,0) to[-|-=.8] (4,1);
  \end{scope}
\end{tikzpicture}
\end{document}