Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The draw and fill options are ignored when supplied to column 1/.style in a tikz matrix, but text color and other options aren't:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
  \matrix[matrix of nodes, column 1/.style={draw=red, fill=blue, text=green}] {
    A & B \\
    C & D \\
  };
\end{tikzpicture}
\end{document}

Matrix containing A B C D with A and C green, but no border and no fill

Why is that happening and how can I fix it?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

The column 1 style is not applied to the node in the entries in column 1 but to the cell. With a standard matrix (ie not one with matrix of nodes) then the cell can contain things other than nodes so the style has to be more general. So you should consider the cells as scopes, not paths. And to affect the nodes within a scope you need to use the every node/.style={...} key. That is, if you want to use a scope to affect a node you write:

\begin{scope}[every node/.style={draw=blue}]
\node {This will have a blue outline};
\end{scope}

So you need to do the same with the matrix. Thus column 1/.style={every node/.style={draw = red, fill = blue, text = green}} is correct. It might seem a little clunky, but it's there because of the greater flexibility in cell contents.

If there isn't already (and without looking in the manual I don't know) then you could define a style column 1 nodes which did the double for you.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/133636/86}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
  \matrix[matrix of nodes, column 1/.style={every node/.style={draw=red, fill=blue, text=green}}] {
    A & B \\
    C & D \\
  };
\end{tikzpicture}

\tikzset{column 1 nodes/.style={column 1/.style={every node/.style={#1}}}}

\begin{tikzpicture}
  \matrix[matrix of nodes, column 1 nodes={draw=red, fill=blue, text=green}] {
    A & B \\
    C & D \\
  };
\end{tikzpicture}
\end{document}
share|improve this answer
    
Out of curiosity, do you know how one would define a "family" of options based on the template column #1 nodes=#2, in a more elegant way than using a foreach loop to define many options? I tried column/.style args={#1:#2}{column #1/.style={every node/.style={#2}}}, which is used like this: \matrix[matrix of nodes, column=1:{draw=red}, column=2:{draw=blue}] { ... };, but the syntax doesn't feel quite as idiomatic as it should. –  Georges Dupéron Sep 16 '13 at 17:50
1  
I don't know, sorry. TikZ doesn't actually define the column <n> styles, it just tries to execute them, and because it executes them when it knows the column number then it just has to do column \the\pgfmatrixcurrentcolumn/.try. You could try emulating that, but my initial experiments draw blank. –  Loop Space Sep 16 '13 at 18:26
1  
I’d avoid every node/.style here and rather use every node/.append style to not overwrite the name=<matrix>-<row>-<column> setting of matrix of nodes (different in CVS). @GeorgesDupéron Take a look at this answer and its rows style definition where one can apply the same style to multiple rows (same for columns) via the help of an auxiliary style and the .list handler. –  Qrrbrbirlbel Sep 16 '13 at 18:39
    
@Qrrbrbirlbel Fair point. –  Loop Space Sep 16 '13 at 19:27

You have to explicitly assign the attributes to the nodes of the matrix. Try something like this.

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
  \begin{tikzpicture}
    \matrix[matrix of nodes, column 1/.style={nodes={draw=red, fill=blue, text=green}}] {
      A & B \\
      C & D \\
    };
  \end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
I accepted Andrew Stacey's answer because it condained more explanations, but this one works too (and that's the one I ended up using). Do you know if there's a difference between nodes= and every node/.style= in that situation (AFAICT both have the same effect)? –  Georges Dupéron Sep 16 '13 at 19:01
1  
@GeorgesDupéron nodes is defined nodes/.style={every node/.append style={#1}}, it appends keys to the every node style while every node/.style redefines it. –  Qrrbrbirlbel Sep 16 '13 at 19:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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