11

I am trying to superimpose some text over an arrow with TikZ. I know it is possible to have the text over the arrow but this is not what I am trying to achieve.

What I have done so far is:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture} 
  \node(n1){n1};
  \node(n2)[below right=3cm and 3cm of n1]{n2};
  \draw[->] (n1) -- node[sloped, fill=white]{Text} (n2);
\end{tikzpicture}
\end{document}

However, this only works on a white background. Is there a better way to do obtain the same result?

1
  • Your trick of fill=white helps me.
    – hengxin
    Dec 14, 2014 at 5:26

1 Answer 1

10

One possibility would be to draw the text as a node. You can place it with a path help. Once all nodes are placed, draw the arrow(s).

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture} 
  \node(n1){n1};
  \node(n2)[below right=3cm and 3cm of n1]{n2};
  % this path will place/draw a node call (text)
  \path (n1) -- node[sloped] (text) {Text} (n2);
  % Now draw arrows. This way it will be like you want.
  \draw[->] (n1)--(text)--(n2);
  % If you use two draw commands, will get two arrows.
  %\draw[->] (n1)--(text);
  %\draw[->] (text)--(n2);
\end{tikzpicture}
\end{document}

enter image description here

3
  • That's a neat solution to working out the position and orientation of the node (I'd just worked out a way to do it explicitly using the calc library but this is much better). One minor change: you only need (text) once in the \draw command, I think. Apr 17, 2013 at 15:18
  • @AndrewStacey You're right. I suppressed it according your suggestion.
    – Ignasi
    Apr 17, 2013 at 15:27
  • Sadly, this won't work well (read: not at all) with other paths, curved or controlled. Apr 17, 2013 at 18:42

You must log in to answer this question.

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