3

I am trying to put a node above a specific segment of a path that is "bent" using "|-" (kind of broken into segments).

A minimal example follows here:

\begin{tikzpicture}
  \node[rectangle,draw] (N1) at (0,0) {N1};
  \node[rectangle,draw] (N2) at (5,1) {N2};

  \path[draw,blue,rounded corners] (N1) -- ++(0,2) -| node[above] {text} (N2);
  \path[draw,red] (N1) -- node[above] {works} (N2); 
\end{tikzpicture}

I would like to be able to control the position of the node in the first \path command, being able to place it on either of the three path segments, possibly shifting it more to the beginning/end as I wish.

Any help is appreciated!

1
  • have you tried the pos key?
    – Bordaigorl
    Commented May 24, 2014 at 15:30

2 Answers 2

4

You can control where the blue node is positioned in the path using the pos key:

\begin{tikzpicture}
  \node[rectangle,draw] (N1) at (0,0) {N1};
  \node[rectangle,draw] (N2) at (5,1) {N2};

  \path[draw,blue,rounded corners] (N1) -- ++(0,2) -|
      node[above right,pos=1] {1}
      node[above,pos=0] {0}
      node[above,pos=.5] {.5}
      node[above,pos=.25] {.25}
  (N2);
  \path[draw,red] (N1) -- node[above] {works} (N2);
\end{tikzpicture}

As you can see pos=.5 corresponds to the corner of the -| line, half of that .25 is the middle of the first "leg" of the path and 1 and 0 are the start and the end.

preview

0

The solution by Bordaigorl works perfectly. In the meanwhile, I have found a less elegant solution, but nevertheless post it here because I think it contains an interesting element that might be useful for others: referring to the "current" position in a path.

\documentclass{standalone}

\usepackage{tikz}

\makeatletter
\newcommand\currentpos{\the\tikz@lastxsaved,\the\tikz@lastysaved}
\makeatother

\begin{document}    
  \begin{tikzpicture}[font=\sffamily,scale=1,transform shape]
    \node[rectangle,draw] (N1) at (0,0) {N1};
    \node[rectangle,draw] (N2) at (5,1) {N2};

    \path[draw,blue,rounded corners] (N1) -- node[above,sloped] {text1} ++(0,3) -- node[above,sloped] {text2} (\currentpos -| N2)  -- node[above,sloped] {text3} (N2);    

  \end{tikzpicture}  
\end{document}

You must log in to answer this question.

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