9

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?

  • Your trick of fill=white helps me. – hengxin Dec 14 '14 at 5:26
9

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

  • 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. – Loop Space Apr 17 '13 at 15:18
  • @AndrewStacey You're right. I suppressed it according your suggestion. – Ignasi Apr 17 '13 at 15:27
  • Sadly, this won't work well (read: not at all) with other paths, curved or controlled. – Qrrbrbirlbel Apr 17 '13 at 18:42

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.