# How to reduce the space between nodes?

I want to make the nodes in the following vector-illustration touch each other. But no matter what I do to text width or node distance the distance between the nodes stays the same.

\usetikzlibrary{backgrounds}
\usetikzlibrary{decorations.pathreplacing}

\tikzstyle{cell} = [rectangle, draw, text width=1.3cm ]
\tikzstyle{capx} = [rectangle, draw, text width=1.3cm, color=black!40  ]

\begin{tikzpicture}[
every node/.style={text centered,  minimum height=1.5em, minimum width=1.5cm,node distance=0pt},
background rectangle/.style={fill=black!10},show background rectangle,
]

\node at (10,0) [capx] (n5) {leer};
\node at (12,0) [capx] (n6) {leer};
\node at (14,0) [capx] (n7) {leer};

\node at (0,0) [cell] (n0) {wert};
\node at (2,0) [cell] (n1) {wert};
\node at (4,0) [cell] (n2) {wert};
\node at (6,0) [cell] (n3) {wert};
\node at (8,0) [cell] (n4) {wert};

\draw [decorate,decoration={brace,amplitude=10pt},xshift=-4pt,yshift=0pt]
(-0.6,0.5) -- (8.8,0.5) node [black,midway,xshift=-0.5cm,yshift=18pt]
{\hskip6ex size()};

\draw [decorate,decoration={brace,amplitude=10pt,mirror},xshift=-4pt,yshift=0pt,color=black!40]
(-0.6,-0.5) -- (14.8,-0.5) node [black,midway,xshift=-0.5cm,yshift=-18pt,color=black!40]
{\hskip6ex capacity()};

\end{tikzpicture}


Use the positioning library and place your nodes using right= of <name>:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning}
\usetikzlibrary{decorations.pathreplacing}

\tikzset{
cell/.style = {rectangle, draw, text width=1.3cm,outer sep=0pt},
capx/.style = {rectangle, draw, text width=1.3cm, color=black!40,outer sep=0pt}
}

\begin{document}

\begin{tikzpicture}[
every node/.style={align=center, minimum height=1.5em, minimum width=1.5cm,node distance=0pt},
background rectangle/.style={fill=black!10},show background rectangle,
]

\node at (0,0) [cell] (n0) {wert};
\node[right=of n0,cell] (n1) {wert};
\node[right=of n1,cell] (n2) {wert};
\node[right=of n2,cell] (n3) {wert};
\node[right=of n3,cell] (n4) {wert};

\node[right=of n4,capx] (n5) {leer};
\node[right=of n5,capx] (n6) {leer};
\node[right=of n6,capx] (n7) {leer};

\draw[
decorate,
decoration={brace,amplitude=10pt}
]
([yshift=4pt]n0.north west) --
node [black,yshift=18pt] {size()}
([yshift=4pt]n4.north east) ;

\draw[
decorate,
decoration={brace,amplitude=10pt,mirror},
color=black!40
]
([yshift=-4pt]n0.south west) --
node [black,yshift=-18pt,color=black!40]  {capacity()}
([yshift=-4pt]n7.south east) ;

\end{tikzpicture}

\end{document}


## Some remarks:

1. \tikzstyle is deprecated; I changed to \tikzset instead.

2. Use the anchors of the existing nodes to place the braces.

3. Set outer sep to 0pt so the lines of the nodes nicely overlap.

4. Notice that I also changed the placement of the nodes for the braces.

• Thank you for the tips, I will try to work them in. i have some difficulties, though. a) every node/.style={...,node distance=0pt}, seems to place all nodes onto each other; changing it to node distance=1.5cm spreads the nodes, but my old problem is back. b) are you sure about [right=of n0,cell]? not rather [right of= n0,cell]? The first fails, the latter parses. Or do I have a wrong version? – towi Feb 1 '14 at 20:14
• @towi Yes, I am sure about right=of n0; the syntax right of=n0 is deprecated; make sure that you are loading the positioning library and that you are using a recent version (at least 2.10) of PGF/TikZ (you can find which version are you using processing this simple document \documentclass{article} \usepackage{tikz} \begin{document} \pgfversion \end{document}) – Gonzalo Medina Feb 1 '14 at 22:45
• 2.10 it is. Uh, that is interesting: When I change right of= to right=of all nodes get spaced with a huge gap -- because I also changed node distance=0cm for my right of=-variant to work properly. But with your right=of-notation I need node distance=0cm and it looks fine! Can you explain the workings of those two variants to me? What does right=of to differently then (deprecated) right of=, and what is node distance=... doing? Or wait, I make another question of it. – towi Feb 2 '14 at 10:45

Here is another way without using positioning library. We place the nodes at previous node.east and set the anchor to west so that they touch each other.

\node[anchor=west,cell] at (n1.east) (n2) {wert};


The outer sep should be zero as in Gonzalo's answer. You can keep the key node distance=1.5cm.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{decorations.pathreplacing}

\tikzset{
cell/.style = {rectangle, draw, text width=1.3cm,outer sep=0pt},
capx/.style = {rectangle, draw, text width=1.3cm, color=black!40,outer sep=0pt}
}

\begin{document}

\begin{tikzpicture}[
every node/.style={align=center, minimum height=1.5em, minimum width=1.5cm,node distance=1.5cm},
background rectangle/.style={fill=black!10},show background rectangle,
]

\node at (0,0) [cell] (n0) {wert};
\node[cell,anchor=west] at (n0.east) (n1) {wert};
\node[anchor=west,cell] at (n1.east) (n2) {wert};
\node[anchor=west,cell] at (n2.east) (n3) {wert};
\node[anchor=west,cell] at (n3.east) (n4) {wert};

\node[anchor=west,capx] at (n4.east) (n5) {leer};
\node[anchor=west,capx] at (n5.east) (n6) {leer};
\node[anchor=west,capx] at (n6.east) (n7) {leer};

\draw[
decorate,
decoration={brace,amplitude=10pt}
]
([yshift=4pt]n0.north west) --
node [black,yshift=18pt] {size()}
([yshift=4pt]n4.north east) ;

\draw[
decorate,
decoration={brace,amplitude=10pt,mirror},
color=black!40
]
([yshift=-4pt]n0.south west) --
node [black,yshift=-18pt,color=black!40]  {capacity()}
([yshift=-4pt]n7.south east) ;

\end{tikzpicture}

\end{document}


• Ok, understood. I do require the distance=1.5cm then? I think I did not grasp the concept of the different node-dimensions, yet. Together with the many automatisms. – towi Feb 2 '14 at 10:35