5

For the following code:

\begin{tikzpicture}
\matrix [matrix of nodes, column 2/.style={red}]
{
8 & 1 & 6 \\
3 & 5 & 7 \\
4 & 9 & 2 \\
};
\end{tikzpicture}

This picture is created:

TikZ matrix

I also tried with:

\begin{tikzpicture}
\matrix [matrix of nodes, column 2/.style={rectangle,draw=black,fill=gray,minimum width=8em}]
{
8 & 1 & 6 \\
3 & 5 & 7 \\
4 & 9 & 2 \\
};
\end{tikzpicture}

The result is shown below:

TikZ matrix, 2

It seems that some styles like red and minimum width are applied. Why are rectangle, fill and draw ignored?

How can I use them in matrix of nodes?

  • There is a size difference if minimum width is not applied and circle is applied instead of rectangle. I am therefore guessing that rectangle is also applied. – ipavlic May 16 '11 at 14:32
8

You need to use the nodes key inside the column x/.style key, otherwise the behaviour will be as if you passed the option to the whole matrix, not the individual nodes.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix [matrix of nodes, column 2/.style={nodes={rectangle,draw=black,fill=yellow,minimum width=8em}}]
{
8 & 1 & 6 \\
3 & 5 & 7 \\
4 & 9 & 2 \\
};
\end{tikzpicture}

\end{document}

matrix of nodes with node options passed to a column

  • This is counterintuitive to me. Why is then red applied as expected? And why isn't the whole matrix bordered by a black border? – ipavlic May 16 '11 at 15:01
  • 1
    @ipavlic: If you supply red,draw to the whole matrix, all cells' text will be red, but a border will only be drawn around the whole matrix, not the individual cells. The options you pass using column 2/.style={red,draw} make the affected cells behave as if they were in a matrix that was created with red,draw (red text, no cell border), but the matrix as a whole is not affected. – Jake May 16 '11 at 15:08
  • I would expect that at least the second column has a border with column 2/.style={draw}. Even if the nodes affected by column <n>/.style behave as though they were nodes of a matrix that had those styles, I would expect that they would be filled. I am still confused. – ipavlic May 17 '11 at 6:59
  • @ipavlic: You can think of the column <n>/.style as behaving like a {scope}[options] environment: Only some styles, like text colour or line thickness, will be passed to the nodes inside this scope. Others, such as draw or fill, need to be passed to each node explicitly, for example using [every node/.style={fill=yellow}] or the shorthand [nodes={fill=yellow}]. When you begin a matrix with some options, those options will apply to the entire matrix node (so draw will draw one rectangle around the entire matrix), but not the nodes inside the matrix: Those will... – Jake May 17 '11 at 8:24
  • ...be affected as if they were inside a scope with the same options, so there will not be a rectangle around them if the draw option is used. As to why there isn't a border around the whole column: The column <n> style works like a filter: For every cell it checks whether the cell is in the specified column, then it creates a scope with the specified options. It is not really aware of what a "column" is, so it cannot draw a border around the whole column. – Jake May 17 '11 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.