[Tex/LaTex] Overruling global node style in matrix

matricestikz-matrixtikz-pgftikz-styles

I want to build a matrix of several nodes of different types. In my example there is a matrix containing two "modules" and one "label".
For convenience I wanted to use matrix of nodes with nodes={module} since there are modules than labels.
Now, if I want to set the style of the label, I would usually use |[label]| as in:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
    module/.style={draw, rectangle, fill=white, minimum width=4cm, minimum height=0.8cm},
    label/.style={ }
}

\begin{document}

\begin{tikzpicture}
    \matrix[fill=black!20, matrix of nodes, nodes={module}]
    {
        |[label]| Label \\ % this  doesn't produce the desired output
        Module 1        \\
        Module 2        \\
    };
\end{tikzpicture}

\end{document}

This produces:

Example

Sure I could just specify every node explicitly, but this seems quite cumbersome. Is there a better solution?

Best Answer

As label is predefined style in TikZ, for my answer I'll use mymodule and mylabel in place of module and label.

What is happening here is that when you put nodes={mymodule} and then |[mylabel]|, the style of the cell became equivalent to |[mymodule,mylabel]|. So if mylabel/.style={} finaly the style of your cell is just like for all other cells |[mymodule]|.

As @user43963 says in his comment, you have to overwrite the properties that you don't want anymore. Like this:

\documentclass[varwidth,border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
    mymodule/.style={draw, rectangle, fill=white, minimum width=4cm, minimum height=0.8cm},
    mylabel/.style={draw=none,fill=none}
}

\begin{document}

\begin{tikzpicture}
    \matrix[fill=black!20, matrix of nodes, nodes={mymodule}]
    {
        |[mylabel]| Label \\ % this  produce the different output
        Module 1        \\
        Module 2        \\
    };
\end{tikzpicture}

\end{document}

enter image description here