Styling the text placed on \path [line] using \tikzstyle.

I'm new to LaTeX and I'm trying to work out how I can configure TikZ to position the text ON the connecting lines (rather than beside it) in this flowchart and then give that text a filled white background rectangle.

My issue mainly comes from the fact that I can't associate a style with the text node inside the line (I can't do [text]) and I don't know how to do substyles with \tikzstyle to match a node that doesn't have a style associated with it (in CSS this would be .line > node or something like that).

I'm sure this is a really really simple question, but Google unfortunately seems to have no answers to it!

Can anyone help?

% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=blue!20,
text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, text centered , -latex']
\tikzstyle{line node} = [draw, fill=white, font=\tiny ]
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
minimum height=2em]

\begin{center}
\begin{tikzpicture}[node distance = 3cm, auto]
% Place nodes.
\node [] (_split01) {};
\node [block, left of=_split01, node distance = 2cm]    (program) {Program};
\node [block, right of=_split01, node distance = 2cm]   (unit) {Unit};

\node [block, below of=_split01]                        (procedure) {Procedure};
\node [block, left of=procedure]                        (entrypoint) {Entry Point};
\node [block, right of=procedure]                       (function) {Function};

\node [block, below of=procedure]                       (block) {Block};

\node [block, below of=block]                           (statement) {Statement};

\node [below of=statement] (_split02) {};
\node [block, left of=_split02, node distance = 2cm]    (assignment) {Assignment Statement};
\node [block, right of=_split02, node distance = 2cm]   (expression) {Expression Statement};

% Connect nodes.
\path [line] (program) -- node {References} (unit);
\path [line] (program) -- node {Defines and Calls} (entrypoint);
\path [line] (program) -- node {Defines} (procedure);
\path [line] (program) -- node {Defines} (function);
\path [line] (unit) -- node {Defines} (procedure);
\path [line] (unit) -- node {Defines} (function);
\path [line] (entrypoint) -- (block);
\path [line] (procedure) -- (block);
\path [line] (function) -- (block);
\end{tikzpicture}
\end{center}

-
Just to clear it up if there's confusion, if you run that through PdfLaTeX, you'll see that the "Defines" text is positioned next to the connecting lines; I want it positioned ON the connecting lines, with a white background so that it is readable ;) –  Hach-Que Mar 16 '11 at 9:47

The key line is the \begin{tikzpicture} one. It currently reads:

\begin{tikzpicture}[node distance = 3cm, auto]


To get the effect that you want, it should read:

\begin{tikzpicture}[node distance = 3cm,every node/.style={rectangle,fill=white}]


This produces:

Although it's worth pointing out that some of your nodes overlap so would need a little adjustment to avoid that.

Here's a little explanation of the differences.

You wanted a "default" style that matches anything that doesn't already have a style. I don't know if this is exactly possible, but a close run is to define a default style that sets up how things that aren't specified in later styles are done. The difference is whether the local style completely overrides the default or only overrides the pieces that it specifies. (Actually, my limited experience of CSS is that the latter is the standard behaviour there as well.) So we set up a default node style with the every node/.style={...} syntax. We say that we want all our nodes to be rectangles and to be filled with white. Since your custom styles all specify a shape and a fill colour, this doesn't propagate to any of them, but does influence the labels. We can see this in action if we add the font=\tiny key to the default style. If we only do this, all the nodes have tiny text. If we add font=\normalsize to the custom styles then they resume their former size.

The other difference is the word auto. This influences how a node on a line is positioned. If given, then for a node specified on a line, as in \path (0,0) -- node {a} (1,0); then the node is shifted above the line (though "above" can mean "below" if the line is upside-down!) (adding the key swap swaps it). Removing this ensures that the labels now are actually on the line which is what you wanted.

To avoid the clashes, you can use the pos=.X syntax to shift the nodes proportionally along their lines. I'd also shrink the text (font=\tiny or font=\small), otherwise you can't see the line behind the word "References". Here's that version:

And here's the full code:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=blue!20,
text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=5em, text centered, rounded corners, minimum height=4em,font=\normalsize]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{line node} = [draw, fill=white, font=\tiny ]
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
minimum height=2em]

\begin{center}
\begin{tikzpicture}[node distance = 3cm,every node/.style={rectangle,fill=white,font=\tiny}]
% Place nodes.
\node [] (_split01) {};
\node [block, left of=_split01, node distance = 2cm]    (program) {Program};
\node [block, right of=_split01, node distance = 2cm]   (unit) {Unit};

\node [block, below of=_split01]                        (procedure) {Procedure};
\node [block, left of=procedure]                        (entrypoint) {Entry Point};
\node [block, right of=procedure]                       (function) {Function};

\node [block, below of=procedure]                       (block) {Block};

\node [block, below of=block]                           (statement) {Statement};

\node [below of=statement] (_split02) {};
\node [block, left of=_split02, node distance = 2cm]    (assignment) {Assignment Statement};
\node [block, right of=_split02, node distance = 2cm]   (expression) {Expression Statement};

% Connect nodes.
\path [line] (program) -- node {References} (unit);
\path [line] (program) -- node[pos=.3] {Defines and Calls} (entrypoint);
\path [line] (program) -- node[pos=.7] {Defines} (procedure);
\path [line] (program) -- node[pos=.3] {Defines} (function);
\path [line] (unit) -- node[pos=.3] {Defines} (procedure);
\path [line] (unit) -- node {Defines} (function);
\path [line] (entrypoint) -- (block);
\path [line] (procedure) -- (block);
\path [line] (function) -- (block);
\end{tikzpicture}
\end{center}
\end{document}

-
Wow, thanks so much. Just to clarify, if I did something like "\tikzstyle{line} = [draw, -latex', every node/.style={ ... stuff here ... }]" would that also have the intended effect? Since the brackets are also different for the style declarations there (the outer is [] the inner is {}) does it infinitely recurse, or is it limited to 2 levels? –  Hach-Que Mar 16 '11 at 10:54
@Hach-Que: I don't know how nesting works with styles; it sounds like a useful thing to know, though. I can think of a few obvious experiments to try. I think that only the outer brackets are [], all the others are {}. –  Loop Space Mar 16 '11 at 11:10
@Hach @Andrew Your question is about \tikzstyle but T. Tantau writes "for compatibility reasons \tikzstyle will be around indefinitely" but his conclusion is "Global styles still have to be set globally using \tikzset anyway..." –  Alain Matthes Mar 17 '11 at 15:20