23

I have drawn some block elements with tikz and named them A and B.

I am trying to draw a line between them with

\path [line] (requested) -- (processing);

but I also want some text above the line.

\path [line] (requested) -- node [above,midway] {Some text} (processing);

works but if the text is very long it just keeps running without breaking the line.

How can I make sure the text wont be wider than the line itself?

Edit

With

\documentclass[tikz]{standalone}

\usetikzlibrary{shapes,arrows,chains}

\begin{document}

\tikzstyle{block} = [rectangle, draw, text width=6em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']

\begin{tikzpicture}[node distance=1cm, auto]
    \node (init) {};
    \node [block] (A) {A};
    \node [block, right=of A] (B) {B};

    \path [line] (A) -- node [text width=2.5cm,midway,above] {My very looooooong text which is wider than the arrow below} (B);
\end{tikzpicture}

\end{document}

I get

enter image description here

How can I in a smarter way make sure there is room for the text? I have multiple boxes.

7
  • 2
    Please help us to help you and add a minimal working example (MWE). You could give a look to text width key (and perhaps align=center), anyway.
    – Astrinus
    Jun 10, 2015 at 9:09
  • Like @Astrinus said, setting a (max) text width is the solution. (By the way, Astrinus, I'd post that as an answer.)
    – Alenanno
    Jun 10, 2015 at 9:23
  • 1
    Seen the edit. How can you have 2.5cm of text when you ask for 1cm distance between nodes? Another way could be drawing all the three nodes at the same time.
    – Astrinus
    Jun 10, 2015 at 9:29
  • Well, you definitely need more space between the nodes, 1cm is very narrow. Can you increase it to at least 3cm?
    – Alenanno
    Jun 10, 2015 at 9:29
  • I have so many boxes so I cannot have more spare than 1cm. Can I maybe lift the text way above the boxes?
    – Jamgreen
    Jun 10, 2015 at 9:36

3 Answers 3

22

two possibilities - Specifying the distance above - By separating the blocks

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,chains}
\usetikzlibrary[calc]

\begin{document}

\tikzstyle{block} = [rectangle, draw, text width=6em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']

\begin{tikzpicture}[node distance=1cm, auto]
\node (init) {};
\node [block] (A) {A};
\node [block, right= of A] (B) {B};

\path [line] (A) -- node [text width=2.5cm,midway,above=3em ] {My very looooooong text which is wider than the arrow below} (B);
\end{tikzpicture}


\begin{tikzpicture}[node distance=1cm, auto]
\node (init) {};
\node [block] (A) {A};
\node [block, right=3cm of A] (B) {B};

\path [line] (A) -- node [text width=2.5cm,midway,above ] {My very looooooong text which is wider than the arrow below} (B);
\end{tikzpicture}

\end{document}

enter image description here

if you want center the text

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{shapes,arrows,chains}
\usetikzlibrary[calc]

\begin{document}

\tikzstyle{block} = [rectangle, draw, text width=6em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']

\begin{tikzpicture}[node distance=1cm, auto]
    \node (init) {};
    \node [block] (A) {A};
    \node [block, right= of A] (B) {B};

    \path [line] (A) -- node [text width=2.5cm,midway,above=3em,align=center ] {My very looooooong text which is wider than the arrow below} (B);
\end{tikzpicture}


\begin{tikzpicture}[node distance=1cm, auto]
    \node (init) {};
    \node [block] (A) {A};
    \node [block, right=3cm of A] (B) {B};

    \path [line] (A) -- node [text width=2.5cm,midway,above,align=center ] {My very looooooong text which is wider than the arrow below} (B);
\end{tikzpicture}

\end{document}

enter image description here

3
  • IMHO I would centre the text.
    – Sigur
    Jun 10, 2015 at 9:48
  • 2
    Why have you escaped all the brackets?
    – Astrinus
    Jun 10, 2015 at 9:50
  • it is mishandled, I correct
    – rpapa
    Jun 10, 2015 at 10:13
3

As exercise ...

\documentclass[tikz,border=2mm]{standalone}
    \usetikzlibrary{arrows,chains}

\begin{document}
    \begin{tikzpicture}[
 start chain = going right,
block/.style = {rectangle, draw, rounded corners, 
                text width=6em, align=center, minimum height=4em,
                on chain},
every pin/.style = {inner sep=1mm, align=center, font=\footnotesize,
                    pin distance=9mm, pin edge={angle 60-, solid, black}},
                        ]
\node[block] (A) {A};
\node[block] (B) {B};

\linespread{0.9}
    \draw[-latex'] (A) to node[inner sep=0pt,
                                pin=above:My very looooooong\\
                                text which is wider\\
                                than the arrow below] {}    (B);
    \end{tikzpicture}
\end{document}

When is not enough place for text on lines this quit common way to add description of line (meaning). This text is usually smaller than main text. For it I select \footnotesize. I broke it manually, I think that on this way you can easily accommodate his appearances to your taste or to disposable space.

enter image description here

3
  • when I follow your pin approach for a bent arrow between A and B, pin arrow is also bent. How can I make pin arrow straight regardless of A [->] B arrow shape?
    – Diaa
    Apr 3, 2017 at 3:03
  • please, ask this as question, more people will see your problem and can help you. Even I will see MWE, by which you will show your problem.
    – Zarko
    Apr 3, 2017 at 6:15
  • you can find my question here.
    – Diaa
    Apr 3, 2017 at 6:36
2

Here is a way it can be done with MetaPost and the rboxes package (complement of the boxes packages for round-corners boxes), for whom it may interest: putting the text label in a \parbox of specified width (here 2.5 cm), separating the two boxes by a shade more than this width (here 2.8 cm), and locating the label at the middle of the arrow joining both boxes. Note that I have chosen to center the text in the label, since it looks better this way.

The code has been included in a LuaLaTeX program for typesetting convenience. Also, it has allowed the use of the em and en TeX units, thanks luamplib's \mpdim command. See also the gmp package for another, not LuaLaTeX-specific but less direct way of inserting MetaPost code into a LaTeX program.

\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
\begin{document}
  \begin{mplibcode}
    input rboxes;
    beginfig(1);
      defaultdx := \mpdim{3em}; defaultdy := \mpdim{3ex};
      rboxit.A(btex A etex); rboxit.B(btex B etex);
      .5[A.e, B.w] = origin;
      B.w - A.e = (2.8cm, 0);
      drawboxed(A,B);
      drawarrow A.e -- B.w;
      label.top(btex \parbox{2.5cm}{\centering My very looooooong text 
        which is wider than the arrow below} etex, .5[A.e,B.w]);
    endfig;
  \end{mplibcode}
\end{document}

enter image description here

You must log in to answer this question.

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