4

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?

  • Did you try \draw (a) |-node{$a$} (b);? – Sigur Jan 26 '17 at 22:59
  • @Sigur Yep- that does the same thing. – Chris Jan 26 '17 at 23:02
7

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.