3

In the MWE below, I would like to bump the label "10" up a bit so it doesn't overlap the line from B to D. There are several questions on stackexchange about edge label positioning but most are about moving it along the edge (using pos). This one proposes a solution using inner sep and outer sep, and/or changing the shape of the label's node, but that would also change the horizontal position. I don't want to do that, since I want the label to align with the label "3" on the edge above.

\documentclass[border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\tikzset{stage/.style={draw,minimum width=15mm,minimum height=7mm}}

\begin{document}

    \begin{tikzpicture}

            \node [stage] (A) {A};
            \node [stage] (B) at ($ (A) +(3cm,0cm) $) {B};
            \node [stage] (C) at ($ (B) +(0cm,2cm) $) {C};
            \node [stage] (D) at ($ (B) +(3cm,1cm) $) {D};

            \path[->] (A.east) edge node[above,at start,anchor=south west]  {\small 2}  (B.west);
            \path[->] (C.east) edge node[above,at start,anchor=south west]  {\small 3}  (D.170);
            \path[->] (B.east) edge node[above,at start,anchor=south west]  {\small 10} (D.190);

    \end{tikzpicture}

\end{document}  

enter image description here

Suggestions?

5

Something like this?

shifted 10

I just added yshift=5pt to the node.

  \path[->] (B.east) edge node[above,at start,anchor=south west, yshift=5pt]  {\small 10} (D.190);

If you want to move it down instead, just make it a negative dimension.

  \path[->] (B.east) edge node[above,at start,anchor=south west, yshift=-5pt]  {\small 10} (D.190);

Code:

\documentclass[border=10pt,tikz]{standalone}
\usetikzlibrary{positioning,calc}
\tikzset{stage/.style={draw,minimum width=15mm,minimum height=7mm}}

\begin{document}
\begin{tikzpicture}
  \node [stage] (A) {A};
  \node [stage] (B) at ($ (A) +(3cm,0cm) $) {B};
  \node [stage] (C) at ($ (B) +(0cm,2cm) $) {C};
  \node [stage] (D) at ($ (B) +(3cm,1cm) $) {D};

  \path[->] (A.east) edge node[above,at start,anchor=south west]  {\small 2}  (B.west);
  \path[->] (C.east) edge node[above,at start,anchor=south west]  {\small 3}  (D.170);
  \path[->] (B.east) edge node[above,at start,anchor=south west, yshift=5pt]  {\small 10} (D.190);
\end{tikzpicture}
\end{document}
  • 2
    Yes. But given the length of the manual, finding it if you don't know it is like finding a needle in a haystack. Probably worse. – cfr Dec 19 '15 at 3:43
2

Basic problem is already solved with cfr answer, so I like to point on some possibility to write your code on slightly different way:

  • since you load TikZ library positioning, use them in positioning of nodes
  • define style for edge nodes (they have the same options), where also defines node's font size
  • use option auto for edge nodes positioning
  • draw lines as segments of path

Complete code:

\documentclass[border=5,tikz]{standalone}
\usetikzlibrary{calc,
                positioning}

\tikzset{stage/.style = {draw,minimum width=15mm,minimum height=7mm},
      edgenode/.style = {font=\small,near start}}

\begin{document}
    \begin{tikzpicture}[
    node distance = 3mm and 12mm,
    auto,
                        ]
\node (A) [stage] {A};
\node (B) [stage,right=of A] {B};
\node (D) [stage,above right=of B]  {D};
\node (C) [stage,above left=of D]   {C};
%
\path[->]   (A) edge node[edgenode]  {2}  (B)
            (C.east) edge node[edgenode]  {3}  (D.170)
            (B.east) edge node[edgenode,xshift=2mm]  {10} (D.190);
    \end{tikzpicture}
\end{document} 

The obtained image is almost the same as at cfr answer:

enter image description here

In the case, that edge nodes can be placed at the middle of the lines (in style for edgenode erase near start), than the correction of positioning of "10" is not needed anymore.

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.