Is there a way to draw an arrow which has text inside, like the one in
image? I saw many examples with the TikZ but most of them had the text over the arrow not inside it.
5 Answers
\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);
- 27,118
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}

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

- 74,238
-
1Nice 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
\specialprimitive or pdfTeX primitive\pdfsetmatrixfor 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
-
-
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
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}
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 is0.3333em.
- 271,626
-
Very interesting solution. Unfortunately it work only for horizontal lines. – Zarko Feb 01 '15 at 14:07
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



- 18,756
-
1Nice! I had never used MetaPost. How to use your code with a normal
texfile 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
gmppackage for LaTeX and PDFLaTeX and theluamplibpackage 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
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.
- 18,756
- 296,517

tikzpicture? – Jessy09 Jan 31 '15 at 21:02\draw[->] .. ;code inside the sametikzpicture. – Manuel Jan 31 '15 at 21:22tikz-cddocumentation. – Manuel Jan 31 '15 at 21:48