# TikZ Matrix - How can I align content horizontally which is spanning over multiple cells/rows?

I'd like to finetune the big rectangle in the last column and its content, i.e. that it's all aligned to the third column's center. It is currently too far on the right:

So far I have been unable to find the missing option.

MWE

\documentclass[
11pt
]{scrartcl}
\usepackage{
tikz,
relsize,
tgheros
}

\usetikzlibrary{
decorations.pathreplacing,decorations.pathmorphing,shapes,
matrix,shapes.symbols,patterns,intersections%,fit
}

\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}

\tikzset{
rolle/.style=
{
rectangle,
minimum width=4em,
minimum height=4em,
text width=4cm,
draw=black!80,
very thick,
fill=black!40,
font=\sffamily\bfseries,
align=center
},
einheit/.style=
{
rectangle,
rounded corners,
minimum height=2em,
minimum width=4em,
draw=black!80,
fill=black!20,
font=\sffamily
},
}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small,node distance=0.5cm]
\draw[thick] (-7.5,-4) rectangle (7.5,4);
\draw[thick,fill=black!20] (-7.5,4) rectangle (7.5,5) node[midway] {something};
\matrix (m) [matrix of nodes,nodes in empty cells,row sep=5mm,column sep=5mm] {
%
\node[rolle] {123}; & \node[rolle] {456}; & \node[rolle] {789}; \\
%
\node[einheit] {words}; & & \node[minimum width=5cm] (abc) {}; \\
%
& \node[einheit] {words}; & \\
%
& \node[einheit] {words}; & \\
%
& \node[einheit] {companies}; & \\
%
\node[einheit] {thing}; & & \\
};
\draw[einheit] (abc.north east) rectangle (m-6-3.south west) node[midway]{abc};
\end{tikzpicture}
\end{center}
\end{document}


I would change a couple things you've done:

\documentclass[11pt]{scrartcl}
\usepackage{tikz,relsize,tgheros}

decorations.pathreplacing,decorations.pathmorphing,shapes,
matrix,shapes.symbols,patterns,intersections%,fit
}

\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}

\tikzset{
rolle/.style=
{
rectangle,
minimum width=4em,
minimum height=4em,
text width=4cm,
draw=black!80,
very thick,
fill=black!40,
font=\sffamily\bfseries,
align=center
},
einheit/.style=
{
rectangle,
rounded corners,
minimum height=2em,
minimum width=4em,
draw=black!80,
fill=black!20,
font=\sffamily
},
}

\begin{document}
\begin{center}
\begin{tikzpicture}
[
font=\sffamily\small,
node distance=0.5cm
]
\draw[thick] (-7.5,-4) rectangle (7.5,4);
\draw[thick,fill=black!20] (-7.5,4) rectangle (7.5,5) node[midway] {something};
\matrix (m)
[
matrix of nodes,
nodes in empty cells,
row sep=5mm,
column sep=5mm
]
{
%
\node[rolle] {123};     & \node[rolle] {456};         & \node[rolle] {789};                   \\
\node[einheit] {words}; &                             & \node[minimum width=3cm] (abc) {abc}; \\
& \node[einheit] {words};     &                                       \\
& \node[einheit] {words};     &                                       \\
& \node[einheit] {companies}; &                                       \\
\node[einheit] {thing}; &                             & \node[minimum width=3cm] (xyz) {xyz}; \\
};
\draw[einheit] (abc.north west) rectangle (xyz.south east) node[midway]{abc};
%    \draw[einheit] (abc.north east) rectangle (m-6-3.south west) node[midway]{abc};
\end{tikzpicture}
\end{center}
\end{document}


I would change how your anchoring the corners of the box you're trying to center. I added another dummy box, xyz with the same minimum width as abc. I also changed the minimum width to 3cm.

I would like to show a couple of more options that are, in my opinion, better suited for custom tweaks, since the usage of matrix here seems only for the positioning not for taking advantage of the matrix structure per se.

One of the issues is that if you use matrix of nodes then you only need the content to be specified. If you need extra options to pass you can use |...| syntax as I did in the first column.

Also there are useful keys for batch application of styles to nodes. I've put a few row and column styles. Sometimes the intersection of these conflicting styles needs to be resolved and that you need to specify in the columns (as they win over because they are issued later). These are not optimal applications but save lots of automation time with a simple manual tweak instead.

I've also drawn the big rectangle as an additional path and placed the text on top.

\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\tikzset{
minimum size=4em,text width=4cm,
draw=black!80,very thick,
fill=black!40,font=\sffamily\bfseries,
align=center,anchor=center
},
einheit/.style={rectangle,rounded corners,
minimum height=2em,minimum width=4em,
draw=black!80,fill=black!20,
font=\sffamily,anchor=center
},
}
\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
\matrix (m)
[
matrix of nodes,nodes in empty cells,
row sep=5mm,
column sep=5mm,
row 1/.style={nodes={rolle}},% declare the default style for row 1 nodes
column 2/.style={nodes={einheit}, % declare the default style for col 2 nodes
row 1/.append style={nodes={rounded corners=false}},% Handle the ambiguities
row 2/.style={nodes={fill=none,draw=none}},% Don't append but overwrite!
row 6/.style={nodes={fill=none,draw=none}},
},
]
{
123               & 456       & 789 \\
|[einheit]| words &           &     \\
& words     &     \\
& words     &     \\
& companies &     \\
|[einheit]| thing &           &     \\
};

\draw[einheit] (m-1-3.south west|-m-2-1.north) rectangle (m-1-3.south east|-m-6-1.south)
node at (m-4-3) {ABC};
\end{tikzpicture}
\end{center}
\end{document}


Shameless Plug : Here is another fun exercise for competing row/col styles How to Create Comparison Tables in LaTeX?

## Code

\documentclass[11pt,tikz,convert=false]{standalone}
\usepackage{tgheros}
% https://github.com/Qrrbrbirlbel/pgf/blob/master/tikzlibrarypositioning-plus.code.tex
\tikzset{
rows/.style 2 args={
@/.style={row ##1/.style={#2}},
@/.list={#1}
}
}
\tikzset{
rolle/.style={
rectangle,
minimum height=4em,
minimum width=4cm,
draw=black!80,
very thick,
fill=black!40,
font=\sffamily\bfseries,
align=center
},
einheit/.style={
rectangle,
rounded corners,
minimum height=2em,
minimum width=4em,
draw=black!80,
fill=black!20,
font=\sffamily
}
}
\begin{document}
\begin{tikzpicture}[font=\sffamily\small]
\matrix[inner sep=5mm] (m) [
matrix of nodes,
nodes={inner sep=+.3333em},
rows={2,...,6}{nodes=einheit},
row 1/.style={nodes=rolle},% or rows={1}{nodes=rolle}
row sep=5mm,
column sep=5mm
] {
123  &    456    & 789 \\
words &           & \\
&   words   & \\
&   words   & \\
& companies & \\
thing &           & \\
};

\node[einheit,span vertical=(m-2-1)(m-6-1),minimum width=3cm] at (fit bounding box -| m-1-3) {abc};
\node[draw,thick,fill=black!20,above=+0pt of -m, minimum height=1cm] (s) {something};
\node[draw,thick,span=(m)(s)] at (fit bounding box) {};
\end{tikzpicture}
\begin{tikzpicture}[font=\sffamily\small]
\matrix[inner sep=0mm] (m) [
matrix of nodes,
nodes={inner sep=+.3333em},
rows={2,...,6}{nodes=einheit},
row 1/.style={nodes=rolle},% or rows={1}{nodes=rolle}
row sep=5mm,
column sep=5mm
] {
123  &    456    & 789 \\
words &           & \\
&   words   & \\
&   words   & \\
& companies & \\
thing &           & \\
};

\node[einheit,span vertical=(m-2-1)(m-6-1),minimum width=3cm] at (fit bounding box -| m-1-3) {abc};
\node[draw,thick,fill=black!20,above=+0pt of -(m-1-1)(m-1-3),add minimum width=5mm,minimum height=1cm] (s) {something};
\node[draw,thick,span=(m-6-1)(s)] at (fit bounding box) {};
\end{tikzpicture}
\end{document}


## Output

### inner sep=0mm

• Although 3 weeks ago I'd still like to say I appreciate your effort in providing help. :) But your solutions currently are too much effort to understand for me (I neither want to simply c&p nor browse around the manual for a good hour or so). I view TikZ and pgfplots somewhat as a "luxury" (=a overhead, sort of), so investing extra time again for grasping the level of detail in your answers is not feasible at this point. Once I have a free plate, who knows but not now. – henry Aug 6 '13 at 7:29