# Adding nodes in a style

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.

You can apply styles on nodes in cells very easily with various styles, they are in order of application:

1. every cell (the key cells appends to every cell),
2. column <number>,
3. every odd column,
4. every even column,
5. row <number>,
6. every odd row,
7. every even row,
8. row <row number> column <column number>.

I believe you are looking for the last one, i.e. row 1 column 1.

With

\tikzset{
fill first cell/.style={
row 1 column 1/.append style={
nodes={fill=#1}}},
fill first cell/.default=red!7
}


you can define a style fill first cell that adds fill=red!7 to the nodes in the very first cell of the matrix. You can also use an optional value that is used instead of the default red!7 color.

If you really want to use your combination, you can add an append after command to the matrix as in the example below.

## Code

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,fit,backgrounds}
\tikzset{
fill first cell/.style={
append after command={
\pgfextra
\pgfonlayer{background}
\tikzset{every node/.style=}
\node[fit=(\tikzlastnode-1-1), inner sep=+0pt, fill=#1] {};
\endpgfonlayer
\endpgfextra
}
},
fill first cell/.default=red!7
}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes, fill first cell] {
1 & 2 \\
3 & 4 \\
};
\end{tikzpicture}
\end{document}


## Output

• This is just an example, not the actual code. I want to create a node that depends on a node. – Nicolas Dudebout Aug 30 '13 at 21:49
• @NicolasDudebout Well, instead of fill=#1 you can use append after command={{[every node/.style=] node[fit=(\tikzlastnode), inner sep=+0pt, fill=#1] {}}}. Is that what you want? – Qrrbrbirlbel Aug 30 '13 at 21:53
• Let me try if that works in my application. If not I will update the example. – Nicolas Dudebout Aug 30 '13 at 21:55
• It actually shows one of the problems I have. I cannot use the layers in this context as I am still in the middle of the same path. – Nicolas Dudebout Aug 30 '13 at 22:05
• I am having a slight issue described in tex.stackexchange.com/questions/131077/… – Nicolas Dudebout Sep 1 '13 at 13:53