[Tex/LaTex] TIKZ Matrix nodes filled with colour afterwards and set to background

backgroundsnodespgflayerstikz-matrixtikz-pgf

I have a (maybe easy) problem in formatting a matrix in tikz.
In the first step I ordinary filled my matrix with nodes inside the matrix environment.

After finishing my matrix I use the fit function to draw a rectangle like shape in order to mark for example some columns in the matrix with a colour (I mean to mark a specific arrangement of nodes). For this I use the first node in the row as a anchor point and a second node at the end of the matrix. For the ancor points I just used the names I gave to the nodes inside the matrix.

However, as I draw the shading rectangel after the nodes are drawn the rectangle is placed "in front" of the nodes and therefore hide all text behind.
As I used the nodenames as anchor I cannot put the code for drawing the rectangel before the matrix code.

I searched the PGF manual and there seems to be something like "behind path" and in "front of path" but this seems to work only inside a path command.

For sure I could just draw a rectangle at the beginning but then I have to adjust the anchor points every time I changes my matrix. Using the nodes as ancor points result in a perfect alignment.

Here is some code you could test to see what I meant to achieve:

\documentclass{article}
\usepackage{tikz,amsmath, amssymb,bm,color}
\usepackage[margin=0cm,nohead]{geometry}
\usepackage[active,tightpage]{preview}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\usetikzlibrary{fit,matrix}
\usetikzlibrary{shadings}

\begin{tikzpicture} [auto, remember picture, block/.style ={ rectangle, 
draw, fill=white,  text centered, minimum height=15mm, text width=12em, 
fill=green}]

\matrix (table) [column sep=.8cm,row sep=.5cm, ampersand replacement=\&,  
nodes in empty cells, in front of path]
{
% row 1
\node [block] (11) {\textbf{Kapitelstruktur der Arbeit}};\& 
\node [block] (12) {Theorie}; \&
\node [block] (13) {\\Empirie};  \& 
\node [block] (14) {\textbf{Resultat}};   \\
% row 2
\node [block] (kap1) {Kapitel 1\\Problembeschreibung und Zielsetzung};
\&  \node [block] (22) {Prior theoretical knowledge};
\&  \node [block] (23) {Prior theoretical knowledge};
\&  \node [block] (24) {Prior theoretical knowledge};
\\
% row 3
\node [block] (kap2) {Kapitel 2\\Grundlagen und Definitionen}; \& 
\node [block] (step1) {Prior theoretical knowledge};  
\& \node [block] (33) {Prior theoretical knowledge} ;
\&  \node [block] (34) {Prior theoretical knowledge};\\
};

%%%% THIS SHOULD APPEAR BEHIND THE NODES OF THE MATRIX%%%%
\node[fit=(12)(33), draw, minimum height=1cm, fill=black, fill opacity=.8, 
text=white]{should be behind nodes};
%%%% THIS SHOULD APPEAR BEHIND THE NODES OF THE MATRIX%%%%

\end{tikzpicture}
\end{document}

Many thanks in advance for your help.

Best Answer

You could use backgrounds for that.

\documentclass[border=3.14mm,x11names,dvipsnames,svgnames]{standalone}
\usepackage{tikz,amsmath, amssymb,bm,color}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\usetikzlibrary{fit,matrix}
\usetikzlibrary{shadings}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[auto, remember picture, block/.style ={ rectangle, 
draw, fill=white,  text centered, minimum height=15mm, text width=12em, 
fill=green}]

\matrix (table) [column sep=.8cm,row sep=.5cm, ampersand replacement=\&,  
nodes in empty cells, in front of path]
{
% row 1
\node [block] (11) {\textbf{Kapitelstruktur der Arbeit}};\& 
\node [block] (12) {Theorie}; \&
\node [block] (13) {\\Empirie};  \& 
\node [block] (14) {\textbf{Resultat}};   \\
% row 2
\node [block] (kap1) {Kapitel 1\\Problembeschreibung und Zielsetzung};
\&  \node [block] (22) {Prior theoretical knowledge};
\&  \node [block] (23) {Prior theoretical knowledge};
\&  \node [block] (24) {Prior theoretical knowledge};
\\
% row 3
\node [block] (kap2) {Kapitel 2\\Grundlagen und Definitionen}; \& 
\node [block] (step1) {Prior theoretical knowledge};  
\& \node [block] (33) {Prior theoretical knowledge} ;
\&  \node [block] (34) {Prior theoretical knowledge};\\
};

%%%% THIS SHOULD APPEAR BEHIND THE NODES OF THE MATRIX%%%%
\begin{scope}[on background layer]
\node[fit=(12)(33), draw, minimum height=1cm, fill=black, fill opacity=.8, 
text=white]{should be behind nodes};
\end{scope}
%%%% THIS SHOULD APPEAR BEHIND THE NODES OF THE MATRIX%%%%

\end{tikzpicture}
\end{document}

enter image description here