[Tex/LaTex] tikz connection label and node distance

tikz-pgf

Is it possible to have tikz automatically determining the distance between two nodes based on a third node used to connect them?

For instance, in the following picture the arrow used to connect the nodes are too short. Can tikz calculate its length automatically so that its label fits it better?

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}
  \node [rectangle,draw] (a) {source program};
  \node [rectangle,draw] (b) [right=of a] {token list};
  \draw [->] (a.east) -- (b.west) node[above] {lexical analysis};
\end{tikzpicture}

\end{document}

connection too small for its label

Best Answer

(I feel certain that I've answered a similar question before, but I can't find it now.)

One solution is simply to change the order in which you draw things. Since the token list node's position should depend on the length of the lexical analysis node, draw the lexical analysis one first.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/46842/86}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \node [rectangle,draw] (a) {source program};
  \node[above right] at (a.east) (l) {lexical analysis};
  \draw[->] (l.south west) -- (l.south east);
  \node [rectangle,draw,right] at (l.south east) (b) {token list};
\end{tikzpicture}
\end{document}

To put the lexical analysis and the node commands in the same command, you could write

 \draw[->] node [
    above right,
    append after command={
      (\tikzlastnode.south west) -- (\tikzlastnode.south east)
    }
] at (a.east) (l) {lexical analysis};

Result:

positioning nodes in the right order