# Matrix of nodes over multiple columns in tikz

I want to create a diagram with tikz in which entries overdraw more than one column. I solved this problem already with the fit package. But now there is a problem with the look of the nodes within the matrix. The text in the multicolumn rectangles is not centered. How can I solve this Problem?

\documentclass[
a4paper,
12pt,
]{scrartcl}

\usepackage{tikz}
\usetikzlibrary{positioning,arrows,chains,matrix,scopes,fit}

\begin{docoument}

\begin{tikzpicture}

\tikzset{element/.style={rectangle,
inner sep=0pt,
minimum width=15mm,
very  thick,
draw = black,
top  color=blue!20!white!20, bottom  color=blue!70!white,
font=\tiny,
}}
\tikzset{element2/.style={rectangle,
minimum  size=5mm,
very  thick,
draw = black,
top  color=blue!20!white!20, bottom  color=blue!70!white,
font=\tiny
}}
\tikzset{point/.style={circle,
inner  sep=0pt,
minimum  size=2pt,
fill=red
}}

\matrix (table) [%
matrix of nodes,
nodes in empty cells,
row  sep=4mm,column  sep=5mm,
nodes={text centered}
] {%
\node(p1)[point]{}; &  & \node(p2)[point]{}; &  &  & \node(p3)[point]{}; &  &  & \\
&  &  &  &  &  &  &  &\\
&  &  &  &  &  & \node()[element2]{$+1$}; &  & \\
&  &  & \node(p4)[point]{}; &  &  &  &  & \node(p5)[point]{}; \\
&  &  &  &  &  &  &  & \\
\node(p6)[point]{}; &  &  & \node(p7)[point]{}; &  &  &  &  & \\
& \node(p8)[point]{}; &  &  &  &  & \node(p9)[point]{}; &  & \\
&  &  &  &  &  &  &  & \\
\node(p10)[point]{}; &  &  &  &  &  &  &  & \\
};
\node[fit=(table-2-2)(table-2-4),element]{label1};
\node[fit=(table-2-7)(table-2-9),element]{label2};
\node[fit=(table-5-2)(table-5-3),element]{label3};
\node[fit=(table-5-6)(table-5-7),element]{label4};
\node[fit=(table-7-4)(table-7-5),element]{label5};
\node[fit=(table-8-4)(table-8-5),element]{label6};

\end{tikzpicture}

\end{docoument}

-
What about specifying the height of the rectangle and the text height with something like minimum height=2ex, text height=1.2ex, in the element style? – Claudio Fiandrino Jan 18 '13 at 10:21
Thank you, this solved the problem very easy! – Froop Jan 18 '13 at 10:59
If the problem is solved, could you post (when you will be allowed) your solution? This will help people with a similar problem that will read the question. :) – Claudio Fiandrino Jan 18 '13 at 11:02

fit library does some extra measurements to cover the given coordinates and apparently it's location of base anchor is not respected so you might be better off with a label to the fitted node. Claudio's comment is an option but it might break down if your fitted node spans more rows.

Also in a matrix of nodes setting you don't need to provide extra node declarations.

\documentclass[
a4paper,
12pt,
]{scrartcl}

\usepackage{tikz}
\usetikzlibrary{positioning,arrows,chains,matrix,scopes,fit}

\begin{document}

\begin{tikzpicture}

\tikzset{element/.style={rectangle,
inner sep=0pt,
minimum width=15mm,
very  thick,
draw = black,
top  color=blue!20!white!20, bottom  color=blue!70!white,
}}
\tikzset{element2/.style={rectangle,
minimum  size=5mm,
very  thick,
draw = black,
top  color=blue!20!white!20, bottom  color=blue!70!white,
font=\tiny
}}
\tikzset{point/.style={circle,
inner  sep=0pt,
minimum  size=2pt,
fill=red
}}

\matrix (table) [%
matrix of nodes,
nodes in empty cells,
row  sep=4mm,column  sep=5mm,
nodes={text centered}
] {%
|[point]| &  & |[point]| &  &  & |[point]| &  &  & \\
&  &  &  &  &  &  &  &\\
&  &  &  &  &  & |[element2]|$+1$ &  & \\
&  &  & |[point]| &  &  &  &  & |[point]| \\
&  &  &  &  &  &  &  & \\
|[point]| &  &  & |[point]| &  &  &  &  & \\
& |[point]|&  &  &  &  & |[point]| &  & \\
&  &  &  &  &  &  &  & \\
|[point]| &  &  &  &  &  &  &  & \\
};
\node[fit=(table-2-2)(table-2-4),element,label={[font=\tiny]center:label1}]{};
\node[fit=(table-2-7)(table-2-9),element,label={[font=\tiny]center:label2}]{};
\node[fit=(table-5-2)(table-5-3),element,label={[font=\tiny]center:label3}]{};
\node[fit=(table-5-6)(table-5-7),element,label={[font=\tiny]center:label4}]{};
\node[fit=(table-7-4)(table-7-5),element,label={[font=\tiny]center:label5}]{};
\node[fit=(table-8-4)(table-8-5),element,label={[font=\tiny]center:label6}]{};

\end{tikzpicture}

\end{document}


-
Labels are a very nice idea: +1. – Claudio Fiandrino Jan 18 '13 at 11:07
Now it looks very nice! – Froop Jan 18 '13 at 11:13