3

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.

ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149

1 Answers1

4

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

enter image description here

Moriambar
  • 11,466
Qrrbrbirlbel
  • 119,821
  • 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 http://tex.stackexchange.com/questions/131077/adding-tikz-node-in-the-middle-of-a-path-triggers-redrawing – Nicolas Dudebout Sep 01 '13 at 13:53
  • @NicolasDudebout Ah, interesting, never occurred to me. But yes, then you really can’t use the pgfonlayer environment inside a path. I have reverted back my answer. I don’t see another way but to issue your drawing commands (inside the pgfonlayer environment) after the \matrix. – Qrrbrbirlbel Sep 01 '13 at 17:28
  • It seems that the Tikz node inside the pgfextra is to blame. However, if I do a pgfnode it works, so I think this is fixable. – Nicolas Dudebout Sep 01 '13 at 17:36
  • your previous revision was better. Please revert to it at it really addresses my question. Just add a \path; before the \node to address the current bug investigated in the other question. – Nicolas Dudebout Sep 01 '13 at 20:00
  • @NicolasDudebout I somehow missed your comment. I haven’t had a problem with \pgfextra before if I included its content in the pgfinterruptpath (as recommended by Mark Wibrow). You might edit your question to include the real use-case (i.e. why can’t you simply fill the node as I did previously). And why can’t you do it after the matrix? Why do you need append after command and pgfonlayer? This is [after all] what triggered it. – Qrrbrbirlbel Sep 02 '13 at 19:34
  • I could do it after the matrix. My question was "can I do it in a style". I have updated the example to show why I cannot use fill for example, because I need to use the whole line of a matrix. – Nicolas Dudebout Sep 03 '13 at 00:31
  • I have added the solution to this issue in http://tex.stackexchange.com/questions/131077/adding-tikz-node-in-the-middle-of-a-path-triggers-redrawing – Nicolas Dudebout Sep 05 '13 at 14:39