6

I make frequent use of |- and -| with computed points in diagrams, like so:

\coordinate (a) at (0,0);
\foreach \x in (1,...,10) {
    \coordinate (b) at ({whateverFunction(\x)},{whateverOtherFunction(\x)});
    \draw (a) |- (b); %Straight line segment
    \draw (b) -| (a); %Another straight line segment
    \coordinate (a) at (b);
}

To get plots like: enter image description here

I would like to be able to label the midpoint of each line segment, but doing something like node[midway,above] {a} does not act like expected:

enter image description here

I want something like:

enter image description here

Why does midway not work, and what is a good workaround here?

Chris
  • 163

1 Answers1

8

With -| and |-, midway (or pos=.5) is defined as the point where the two legs meet, regardless of how uneven the two legs are.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\coordinate (a) at (0,0);
\coordinate (b) at (3,1);
\draw (a) |- node[pos=.25,left]{A}
  node[pos=.5,above left]{B}
  node[pos=.75,above]{C} (b);
\end{tikzpicture}
\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120