I have the following code, which draws a matrix and then uses the nodes inside the matrix to create new nodes:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{backgrounds}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes] {
1 & 2 \\
3 & 4 \\
};
%% BEGIN
\begin{pgfonlayer}{background}
\node [fit = (m-1-1) (m-1-2), fill=red!7, inner sep=0pt] {};
\end{pgfonlayer}
\node [below of = m-2-2] {foo};
%% END
\end{tikzpicture}
\end{document}
I am trying to create a style (called here fill first cell) that would add the code between BEGIN and END when used as follows:
\matrix (m) [matrix of nodes, fill first cell] {...};
My few attempts were not very succesful.
I tried using the append after command but this caused my matrix to be drawn twice. I think this is due to the fact that I am not actually continuing a path but creating new ones.
I am pretty sure I could get this example to work if I used pgf commands to replicate the role of the fit library. However this would be very verbose and not real easy to read/modify.
Is there any way to have the clean interface provided by styles as well as the power of tikz? By this I mean, can I, in a style, instantiate TikZ nodes that depends on the nodes of the matrix.

fill=#1you can useappend after command={{[every node/.style=] node[fit=(\tikzlastnode), inner sep=+0pt, fill=#1] {}}}. Is that what you want? – Qrrbrbirlbel Aug 30 '13 at 21:53pgfonlayerenvironment inside a path. I have reverted back my answer. I don’t see another way but to issue your drawing commands (inside thepgfonlayerenvironment) after the\matrix. – Qrrbrbirlbel Sep 01 '13 at 17:28\path;before the\nodeto address the current bug investigated in the other question. – Nicolas Dudebout Sep 01 '13 at 20:00\pgfextrabefore if I included its content in thepgfinterruptpath(as recommended by Mark Wibrow). You might edit your question to include the real use-case (i.e. why can’t you simplyfillthe node as I did previously). And why can’t you do it after the matrix? Why do you needappend after commandandpgfonlayer? This is [after all] what triggered it. – Qrrbrbirlbel Sep 02 '13 at 19:34