13

Is there a way to draw an arrow which has text inside, like the one in this image? I saw many examples with the TikZ but most of them had the text over the arrow not inside it.

Jessy09
  • 717

5 Answers5

17
\documentclass{scrartcl}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \coordinate (A) at (-5,0);
  \coordinate (B) at ( 5,0);
  \draw[->] (A) -- (B) node[midway,fill=white] {\emph{success}};
\end{tikzpicture}

\end{document}

This, which does the same, may be more understandable to other users

\draw[->] (A) -- node[midway,fill=white] {\emph{success}} (B);
Manuel
  • 27,118
10

You can do this without tikz and without any complicated code. The knowledge of TeX primitives is sufficient.

\def\arrowtext#1#2{\hbox to#1{\arrowtextA\ #2 \arrowtextA\kern2pt\llap{$\succ$}}}
\def\arrowtextA{\leaders\vrule height2.7pt depth-2.3pt\hfil}

\arrowtext{5cm}{success}

success

Edit: In response on the comment by @fpast below I show the example of diagonal arros with the middle text. The pdfTeX is supposed.

\def\arrowtext#1#2{\hbox to#1{\arrowtextA\ #2 \arrowtextA\kern2pt\llap{$\succ$}}}
\def\arrowtextA{\leaders\vrule height2.7pt depth-2.3pt\hfil}
\def\diagonal{\pdfsave\pdfsetmatrix{.7071 .7071 -.7071 .7071}}

\arrowtext{5cm}{success}

\vskip2cm \noindent
\diagonal\rlap{\arrowtext{3cm}{diagonal}}\pdfrestore

success2

wipet
  • 74,238
  • 1
    Nice answer! However TeX primitives would not be enough if the arrow is not horizontal, I suppose – Franck Pastor Feb 01 '15 at 07:56
  • @fpast You can use \special primitive or pdfTeX primitive \pdfsetmatrix for setting the linear transformation. – wipet Feb 01 '15 at 09:13
  • Could you show us an example of a diagonal arrow, with text inside, drawn with these primitives? – Franck Pastor Feb 01 '15 at 12:45
  • @fpast OK, I've updated my answer. – wipet Feb 01 '15 at 13:05
  • I see. Impressive! Personally, I consider this code as more complicated than the Tikz/Metapost solutions above, but it is certainly due to the fact I don't know anything at all about all these primitives. It seems they are worth the learning! – Franck Pastor Feb 01 '15 at 20:28
9

A solution with TikZ without filling the background with white:

\documentclass{article}

\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}
    \coordinate (A) at (-2,0);
    \coordinate (B) at ( 2,0);
    \path (A) -- node (success) {\emph{success}} (B);
    \draw[->] (A) -- (success) -- (B);
  \end{tikzpicture}
\end{document}

Result

With library calc, the position of the node can also be calculated:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
  \begin{tikzpicture}
    \coordinate (A) at (-2,0);
    \coordinate (B) at ( 2,0);
    \draw[->]
      ($(A)!.5!(B)$) node (success) {\emph{success}}
      (A) -- (success) -- (B);
  \end{tikzpicture}
\end{document}

Remark:

  • The space between the arrow lines and the text is controlled by the inner margin of the node, set by option inner sep; default is 0.3333em.
Heiko Oberdiek
  • 271,626
8

Not too difficult to produce with MetaPost. The draw arrow_withtext macro I defined below produces an arrow with text placed precisely in the middle of the shaft, so that it can always be read from left to right (unless the shaft is vertical, of course :-))

input latexmp; setupLaTeXMP(textextlabel = enable, mode = rerun);

vardef drawarrow_withtext(expr A, B, str) =
  save mylabel, mid; 
  pair mid; mid = 0.5[A, B];
  picture mylabel; mylabel = thelabel(str, mid) 
    rotatedaround (mid, if xpart(B-A) >= 0: angle(B-A) else: angle(A-B) fi);
  drawarrow A -- B; unfill bbox mylabel; draw mylabel;
enddef;

beginfig(1); drawarrow_withtext(origin, 6cm*right, "success"); endfig;

beginfig(2); drawarrow_withtext(origin, 6cm*dir60, "success"); endfig;

beginfig(3); drawarrow_withtext(origin, 6cm*dir150, "success"); endfig;

end.

To be compiled with MetaPost and LaTeX as TeX engine: mpost --tex=latex myfile.mp

enter image description here

enter image description here

enter image description here

Franck Pastor
  • 18,756
  • 1
    Nice! I had never used MetaPost. How to use your code with a normal tex file to insert the arrow in the middle of a paragraph? – Sigur Jan 31 '15 at 23:12
  • 2
    @Sigur See this topic: http://tex.stackexchange.com/questions/142587/using-metapost-inline-in-latex-no-context?rq=1 for how to integrate MetaPost code into LaTeX code. I recommend the gmp package for LaTeX and PDFLaTeX and the luamplib package for LuaLaTeX (the most direct solution). Otherwise you can integrate the MetaPost drawings as external figures: see this very good introduction: http://www.tug.org/docs/metapost/mpintro.pdf – Franck Pastor Jan 31 '15 at 23:39
3

Based on Heiko Oberdiek solution:

\documentclass[tikz,border=1em]{standalone}
    \usetikzlibrary{calc}

    \begin{document}
\begin{tikzpicture}
    \coordinate (A) at (-2,-1);
    \coordinate (B) at ( 2,+1);
% invisible line, for sloped option:
\path (A) to node[sloped] (success) {\emph{success}} (B);
% real line with sloped text in the midle
\draw[->] (A) -- (success) -- (B);
\end{tikzpicture}
    \end{document}

This lines can bi directed to any direction.

Franck Pastor
  • 18,756
Zarko
  • 296,517