Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How to change tikzset the text is automatically added to the link

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,fit}
\begin{document}


\tikzset{mylink/.style={dashed,->}}
\begin{tikzpicture}

\node[circle,draw] (a) at (0,0){a};

\node[circle,draw] (b) at (5,2){b};

\draw[mylink](a) -- node{aaa}(b) ;
\end{tikzpicture}
\end{document}
share|improve this question
1  
What text are you talking about? If you to add a text node along an arrow you can just add a node after (b) and add the pos=0.5 key. –  Matthew Leingang Mar 28 '13 at 20:44
    
Do you want to change the link text (the aaa in 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
    
I want to automatically add the text "aaa" with the style –  rpapa Mar 28 '13 at 21:36
1  
\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
    
ok, but can be replaced (a) to (b) by (a) -- (b). –  rpapa Mar 29 '13 at 8:18

1 Answer 1

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

enter image description here enter image description here enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.