# How to evenly space out nodes or in tikz?

What I want is a block diagram, with a central node called "API", with a node on the left called "User" and a node on the right called "Workflow".

However, because workflow node has lengthier text, it squishes up against the other nodes:

Now I know that I can increase the node distance argument, but is there a way to say something like "Always have nodes separated by a given distance"? So that it does not depend on the amount of size of the node "rectangle"?

Here is my source for the diagram:

\begin{tikzpicture}[node distance=15mm]
\tikzstyle{block} = [inner sep=2mm, rectangle, draw, fill=blue!20]

\node [block] (api) {API};
\node [block, right of=api] (workflow) {Workflow};
\node [block, left of=api] (user) {User};

% Connect the blocks
\draw [->] (user) to (api);
\draw [->] (api) to (workflow);

\end{tikzpicture}


EDIT: I should be able to use arrows that can go in both directions, and add new nodes above and below.

-
The correct syntax in TikZ v2.10 is left=of api (or left=0.5cm of api) using \usetikzlibrary{positioning}. Then the anchors are automatically set correctly. (I did not write an answer as I'm currently on a computer without TeX and hence cannot test any code for correctness.) –  Caramdir May 18 '12 at 22:26

The chains library of TikZ specifically addresses this problem. Here is a simple example:

\documentclass{article}
\usepackage{tikz}% http://ctan.org/pkg/pgf
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[
start chain,
node distance=5mm,
every node/.style={draw,on chain,join,fill=blue!20,inner sep=2mm},
every join/.style={->}
]
\node {User};
\node  {API};
\node {Workflow};
\end{tikzpicture}
\end{document}


EDIT:

The arrows can be passed to the chain nodes via join= by <-> specification. Here is another simple example with random words. The library commands are explained in much detail in the manual for branching and continuing the chains.

\documentclass{article}
\usepackage{tikz}% http://ctan.org/pkg/pgf
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[
start chain,
node distance=5mm,
mynode/.style={draw,fill=blue!20,inner sep=2mm,join=by #1},
mynode/.default=->
]
\node [mynode,on chain] {User};
\node [mynode,on chain] {API};
\node [mynode=<->,on chain] {Workflow};
\node [mynode=<->,on chain=going below] {Update};
\node [mynode=<-,on chain] {Bug Tracker};
\end{tikzpicture}


Often, we need to keep the chain node styles local with respect to the rest of picture so we might use scopes instead of supplying the options to the general tikzpicture environment.

-
Um... what if I want to have another node under the API node? Does it work for cases like that too? –  drozzy May 19 '12 at 3:17
@drozzy Yes it does, please refer to the section 28 of the manual for v3.10. –  percusse May 19 '12 at 4:48
Sorry, I think I failed to clarify that arrows can sometimes go in different directions... (some might go both ways at the same time). –  drozzy May 19 '12 at 5:17
@drozzy I've edited the answer. Is that closer to what you asked? –  percusse May 19 '12 at 6:01

This may not address your question sufficiently, but provides some means to an end. You can use a non-centred anchor for the surrounding nodes:

\documentclass{article}
\usepackage{tikz}% http://ctan.org/pkg/pgf
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\tikzstyle{block} = [inner sep=2mm, rectangle, draw, fill=blue!20]

\node [block] (api) {API};
\node [block, right=1cm of api] (workflow) {Workflow};
\node [block, left=1cm of api] (user) {User};

% Connect the blocks
\draw [->] (user) to (api);
\draw [->] (api) to (workflow);

\end{tikzpicture}
\end{document}


\node [block, above=1cm of api] (aboveapi) {Above api};
\node [block, below=1cm of api] (belowapi) {Below api};


while arrow direction can be modified using

\draw [<->] (..) to (..);


for double arrows.

-
Note that right of is depreciated syntax in v2.10. –  Caramdir May 18 '12 at 22:27
@Caramdir: Thanks. –  Werner May 19 '12 at 1:41
With the right= syntax, you should not need to specify the anchors manually. Also, the node distance key is only used when you don't give a length in right=. –  Caramdir May 19 '12 at 2:12