[Tex/LaTex] How to reduce the space between nodes

nodestikz-pgf

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}

bad vector

Best Answer

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}

enter image description here

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.