12

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.

15
\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);
  • 2
    Mmm… second time TeX.SX asks me if I'm a robot. I don't know what to think. – Manuel Jan 31 '15 at 20:22
  • 3
    It must have realized: you're an answering machine! – Paul Gessler Jan 31 '15 at 20:26
  • 1
    @PaulGessler I will change my name to distract them, I hope they don't notice. Or may be just chaning the image would do? – Manuel Jan 31 '15 at 20:35
  • @Manuel: And if I want to draw more arrows do I just have more tikzpicture? – Jessy09 Jan 31 '15 at 21:02
  • @Jessy09 More arrows in what sense? You just need to add as many \draw[->] .. ; code inside the same tikzpicture. – Manuel Jan 31 '15 at 21:22
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

  • 1
    Nice answer! However TeX primitives would not be enough if the arrow is not horizontal, I suppose – Franck Pastor Feb 1 '15 at 7:56
  • @fpast You can use \special primitive or pdfTeX primitive \pdfsetmatrix for setting the linear transformation. – wipet Feb 1 '15 at 9:13
  • Could you show us an example of a diagonal arrow, with text inside, drawn with these primitives? – Franck Pastor Feb 1 '15 at 12:45
  • @fpast OK, I've updated my answer. – wipet Feb 1 '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 1 '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.
  • Very interesting solution. Unfortunately it work only for horizontal lines. – Zarko Feb 1 '15 at 14:07
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

  • 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: tex.stackexchange.com/questions/142587/… 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: 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.

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.