[Tex/LaTex] Text above line in tikz

tikz-pgf

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.

Best Answer

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