Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Qrrbrbirlbel Sep 3 '13 at 18:00
add comment

1 Answer

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

share|improve this answer
    
Another possibility: \path[draw,thick] (0,0) -- (0,0 |- 1.5,1.5) -- node{Hi} (1.5,1.5); –  Paul Gaborit Feb 8 '13 at 22:33
    
@PaulGaborit Of course! I have added your possibility to my answer (while bringing in my own thoughts on the subject). –  Qrrbrbirlbel Feb 8 '13 at 23:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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