# Aligning edge labels horizontally with tikz package

I have the following figure:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage[active,float]{preview}
\usetikzlibrary{calc}
\usetikzlibrary{plotmarks,arrows,automata,positioning,fit,shapes.geometric,backgrounds}
\usepackage{graphicx}
\PreviewEnvironment{tikzpicture}

\begin{document}

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=4cm,
thick,main node/.style={circle,fill=blue!20,draw,font=\sffamily\Large\bfseries}]
\tikzstyle{every path}=[line width=2pt]
\node[main node] (1) {$m_0$};
\node[rectangle,fill=gray] (0) [above of=1] {\Large $\textbf{Source}$};
\node[main node,fill=white,xshift=1.5cm] (3) [below left of=1] {v$_j$};
\node[main node,fill=white,xshift=-1.5cm] (4) [below right of=1] {v$_k$};
\node[main node,fill=white,xshift=1.5cm] (2) [left of=3] {v$_i$};
\node[main node,fill=black,scale=.2] (dot_ij) at ($(2)!0.5!(3)$) {b};
\node[main node,fill=black,scale=.2] (dot_jk) at ($(3)!0.5!(4)$) {b};
\node[main node,fill=white,xshift=-1.5cm] (5) [right of=4] {v$_l$};
\node[main node,fill=black,scale=.2] (dot_kl) at ($(4)!0.5!(5)$) {b};
\node[main node] (6) [below of=dot_jk] {$m_1$};
\node[rectangle,fill=gray] (7) [below of=6] {\Large $\textbf{Sink}$};
\node[fit=(3) (4),ellipse,fill=red!30,draw=red,fill opacity=0.3,xscale=0.75,yscale=1] {};

\path[every node/.style={font=\sffamily\Large\bfseries}]
(0) edge node [right] {$r_0$} (1)
(1) edge node [below right=-0.8cm and -1cm] {$w_j\theta_d$} (3)
(1) edge node [above left=0cm and -1cm] {$w_k\theta_d$} (4)
(2) edge node [above left=-0.8cm and -0.2cm] {$w_j\theta_a$} (6)
(3) edge node [left] {$w_k\theta_a$} (6)
(4) edge node [right] {$w_j\theta_a$} (6)
(5) edge node [above left=-0.7cm and -1.1cm] {$w_k\theta_k$} (6)
(6) edge node [left] {$r_1$} (7);
\end{tikzpicture}


I want to align all the labels defined for each label horizontally. In other words I want to horizontally align $w_j$ with $w_k$ and $w_i$,$w_j$,$w_k$,$w_l$ respectively.

• A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). And please don't post fragments of code. Your example isn't complete – user31729 Apr 25 '15 at 10:38

The problem arises here because your line to path uses the border of the nodes not only to draw the edges but also to position the nodes along that path. If you would use the .center anchors of the nodes to position the edge labels they would be on the same height for the same position.

I changed a little bit of your figure setup, for instance I used the keys from the positioning library to place nodes. The red ellipse is drawn with the backgrounds library on its background layer. I also used the arrows.meta library for the arrows.

First I used the edges without nodes to draw the lines, then I used the same edge commands but with nodes and with a special to path installed that uses the .center nodes automatically:

{ [<-, shorten <=+1pt] % without nodes
(m_1) edge (v_i) edge (v_j) edge (v_k) edge (v_l)}
{ [every edge/.append style={draw=none, % now with nodes
to path={(\tikztostart.center) -- (\tikztotarget.center) \tikztonodes}}]
(m_1) edge node[left=5pt]  {$w_j\theta_a$} (v_i)
edge node[left]      {$w_k\theta_a$} (v_j)
edge node[right]     {$w_j\theta_a$} (v_k)
edge node[right=5pt] {$w_k\theta_k$} (v_l)}     ;


## Code

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta,positioning,fit,shapes.geometric,backgrounds}
\begin{document}
\begin{tikzpicture}[
>={Stealth[round]}, thick, node distance=3cm and 1cm,
main node/.style={circle,fill=blue!20,draw,font=\sffamily\Large\bfseries},
dot/.style={circle, fill, inner sep=+0pt, minimum size=+.2cm, node contents=},
rect node/.style={rectangle, fill=gray, outer sep=+0pt},
every path/.style={line width=2pt}]
\node[main node] (m_0) {$m_0$};
\node[rect node, above=of m_0] (Source) {\Large\bfseries Source};

\node (dot_jk) [dot, below=of m_0];

\node[main node, left=of dot_jk] (v_j) {$\mathbf{v}_j$};
\node (dot_ij) [dot, left=of v_j];
\node[main node, left=of dot_ij] (v_i) {$\mathbf{v}_i$};

\node[main node, right=of dot_jk] (v_k) {$\mathbf{v}_k$};
\node (dot_kl) [dot, right=of v_k];
\node[main node, fill=none, right=of dot_kl] (v_l) {$\mathbf{v}_l$};

\node[main node, below=of dot_jk] (m_1) {$m_1$};
\node[rect node, below=of m_1] (Sink) {\Large\bfseries Sink};
\begin{pgfonlayer}{background}
\node[fit=(v_j)(v_k), ellipse, fill=red!30, draw=red, xscale=0.75] {};
\end{pgfonlayer}

\path[nodes={font=\Large}] {[shorten >=+1pt, ->, auto]
(Source) edge node {$r_0$}         (m_0)
(m_0) edge node['] {$w_j\theta_d$} (v_j)% ' = swap
edge node    {$w_k\theta_d$} (v_k)
(m_1) edge node    {$r_1$}         (Sink) }
{ [<-, shorten <=+1pt]
(m_1) edge (v_i) edge (v_j) edge (v_k) edge (v_l)}
{ [every edge/.append style={draw=none,
to path={(\tikztostart.center) -- (\tikztotarget.center) \tikztonodes}}]
(m_1) edge node[left=5pt]  {$w_j\theta_a$} (v_i)
edge node[left]      {$w_k\theta_a$} (v_j)
edge node[right]     {$w_j\theta_a$} (v_k)
edge node[right=5pt] {$w_k\theta_k$} (v_l)}     ;
\end{tikzpicture}
\end{document}


## Output

• it doesnt compile on my machine. Did you add any extra package? – Kostas Apr 25 '15 at 11:49
• @Kostas No, I did not (but only the arrows.meta library and the node contents key from TikZ itself). Do you have the newest (3.0.0 from Dec 2013) version of TikZ/PGF installed? You can check easily with \pgfversion somewhere in your document. – Qrrbrbirlbel Apr 25 '15 at 11:57
• the error is at "node contents=" .. I think there should have a value defined there – Kostas Apr 25 '15 at 11:59