You have about three ways to get automatically nodes with styles.
The easiest one is to use a to path operator and simply change the used to path to
-- node {#1} (\tikztotarget) \tikztonodes
(This will of course only work with -- paths.)
If you insist on using -- in your path, you can use the decorations.markings library and automatically mark these paths with a node:
mark=at position .5 with {\node {#1};}
This will also work with non-straight (--/line to) paths.
The third option is only available with the current CVS version of PGF and uses the so-called edge nodes. Similar to solution 1, I change the every edge style to onclude this edge node. (We could also have used every to and to in the path.)
The advantage here is that we still can change the path to for example a curve to.
Code
\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\tikzset{
mylink/.style={
dashed,
->,
},
aaa/.style={
every to/.append style={
to path=-- node {#1} (\tikztotarget) \tikztonodes
}
},
aaa/.default=aaa,
bbb/.style={
decoration={
markings,
mark=at position .5 with {\node {#1};}
},
postaction=decorate
},
bbb/.default=bbb,
ccc/.style={
every edge/.append style={edge node=node {#1}}
},
ccc/.default=ccc
}
\begin{document}
\begin{tikzpicture}
\node[circle,draw] (a) at (0,0) {a};
\node[circle,draw] (b) at (5,2) {b};
\draw[mylink, aaa] (a) to (b);
\end{tikzpicture}
\begin{tikzpicture}
\node[circle,draw] (a) at (0,0) {a};
\node[circle,draw] (b) at (5,2) {b};
\draw[mylink, bbb] (a) -- (b);
\end{tikzpicture}
\begin{tikzpicture}
\node[circle,draw] (a) at (0,0) {a};
\node[circle,draw] (b) at (5,2) {b};
\draw[mylink, ccc, curve to] (a) edge (b);
\end{tikzpicture}
\end{document}
Output

(b)and add thepos=0.5key. – Matthew Leingang Mar 28 '13 at 20:44aaain your example) or the link style (for example, the positioning of the label above or below the line)? – Jake Mar 28 '13 at 21:16\tikzset{mylink/.style={dashed,->, every to/.append style={to path=-- node {aaa} (\tikztotarget) \tikztonodes}}}and then you can use\draw [myling] (a) to (b);– Qrrbrbirlbel Mar 28 '13 at 23:18