8

I have the following code

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw (-3,-3) rectangle (3,3);
  \path[draw,thick] (0,0) |- (1.5,1.5);
  \node at (0.75,1.5) [above] {Hi};
\end{tikzpicture}
\end{document}

which results in:

Right-angled path shown with the word "Hi" mid-way along top branch

In my mind, I can achieve the same effect by doing this:

\path[draw,thick] (0,0) |- node{Hi} (1.5,1.5);

But, when I try, I get:

Right-angled path shown with the word "Hi" at the corner

Is there a way to achieve what I want?

1

1 Answer 1

10

In

\path[draw,thick] (0,0) |- node{Hi} (1.5,1.5);

the node is placed at the middle of the path (midway, pos=.5). The |- and -| operators are special because their middle lies exactly on the sharp kink.

The middle of second part of your path is therefore at .75 of your whole path, thus

node[pos=.75,above]

is needed.

Example

  \path[draw] (0,0) -| (5,-1);
  \foreach \x in {0.0,0.25,0.5,0.75,1.0}{
    \path[draw] (0,0) -| node[pos=\x] {\x} (5,-1);
  }

Output

enter image description here

Code

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw (-3,-3) rectangle (3,3);
  \path[draw,thick] (0,0) |- node[pos=.75,above] {Hi} (1.5,1.5);
\end{tikzpicture}
\end{document}

Output

enter image description here

A different approach

As Paul Gaborit has mentioned in a comment there is also the possibility to avoid the specialties of |- and -| and actually use two path parts with the coordinate specification |- and -| (which are in fact only short-cuts for the perpendicular coordinate system).

In a path like

\path (<c1>) -- (<c1> |- <c2>) -- node {} (<c2>);

you do have two path parts:

  • one from <c1> to the coordinate vertically through <c1> and horizontally through <c2>, and
  • one from that point to <c2> where we want to place the node (at the middle of that part, see above).

If you want to use these two-parters repeatedly, I advise you to use custom to paths so that you do not need to repeat both coordinates. (The number stands for the path part where the node shall be placed at.)

Code

\documentclass[tikz]{standalone}
\tikzset{
 |- 1/.style={to path={-- (\tikztostart |- \tikztotarget) \tikztonodes -- (\tikztotarget)}},
 |- 2/.style={to path={-- (\tikztostart |- \tikztotarget) -- (\tikztotarget) \tikztonodes}},
 -| 1/.style={to path={-- (\tikztostart -| \tikztotarget) \tikztonodes -- (\tikztotarget)}},
 -| 2/.style={to path={-- (\tikztostart -| \tikztotarget) -- (\tikztotarget) \tikztonodes}},
}
\begin{document}
\begin{tikzpicture}[draw, thick]
\draw (0,0) to[|- 2] node{Hi} (1.5,1.5);
\draw (0,-1) to[-| 1] node{Hi} ++ (1.5,-1.5);
\end{tikzpicture}
\end{document}

Output

enter image description here

2
  • Another possibility: \path[draw,thick] (0,0) -- (0,0 |- 1.5,1.5) -- node{Hi} (1.5,1.5); Feb 8, 2013 at 22:33
  • @PaulGaborit Of course! I have added your possibility to my answer (while bringing in my own thoughts on the subject). Feb 8, 2013 at 23:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .