3

I have the following problem: With the code

\documentclass[border=10pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

\everymath{\displaystyle}

\begin{document}
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
  text centered, rounded corners, minimum height=4em]

\tikzstyle{line} = [-stealth, thick, draw]

\begin{tikzpicture}[node distance=5cm, text width=8em, auto]
  \node [block] (A) {Block 1};
  \node [block, right of=A] (B) {Block 2};
  \path [line] (A) edge node [midway] {Text} (B);
\end{tikzpicture}

\end{document}

I get the following image

enter image description here

The text description is not shown at the center of the arrow. It seems that the start point of the error is wrong. What do I need to do to have the arrow description at the right position (middle of arrow)?

My attempts: I can change midway to at end but this does not seem to be the right solution...

  • remove text width=8em – CroCo Nov 15 '15 at 14:28
  • @CroCo: Is there another way to ensure that all nodes have the same width? – Stephan Kulla Nov 15 '15 at 14:31
  • For the blocks? or you want also the text nodes? – CroCo Nov 15 '15 at 14:33
  • 1
    \path [line] (A) edge node [yshift=1cm] {Text} (B); – Arne Timperman Nov 15 '15 at 14:46
  • 1
    @Arne, with yshift=1cm does not guarantee the node being in the middle. Better to stick with the positioning package. In this case, it is set by default to be in the middle however due to the fact that text width is taking to much space, the text is shifted to the left. – CroCo Nov 15 '15 at 15:33
2

The problem with text width=8em which takes too much space. The solution is to remove it. Therefore, the result should be

enter image description here

and the code is

\documentclass[border=10pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

\everymath{\displaystyle}

\begin{document}
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
  text centered, rounded corners, minimum height=3em, minimum width=5em]


\tikzstyle{line} = [-stealth, thick, draw]

\begin{tikzpicture}[node distance=3.5cm, auto]
  \node [block] (A) {Block 1};
  \node [block, right of=A] (B) {Block 2};
  \path [line] (A) edge node [midway] {Text} (B);
\end{tikzpicture}

\end{document} 

I've changed minimum height=3em and node distance=3.5cm. Also, I've added minimum width=5em as the OP requested in the comment.


Update: as @Zarko mentioned in below comments, the node is already placed in the midway, therefore, there is no need to explicitly add it to the node.

|improve this answer|||||
  • @Cr0Co, nodes on edges are always on the middle of them, so the option [midway] of their node is surplus. – Zarko Nov 15 '15 at 14:45
  • @Zarko, I've addressed only the problem. And you're right. I will update it now. – CroCo Nov 15 '15 at 14:48

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.