7

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?

2
  • I am afraid there is no easy way but define an opposite style: label/.style={draw=none,fill=none}
    – user43963
    Commented Jan 8, 2015 at 13:32
  • Thanks, such an easy solution. I didn't think of that.
    – pschulz
    Commented Jan 8, 2015 at 13:47

2 Answers 2

4

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

0
2

If label nodes are only labels to be added to some module nodes, there's no need for a new style, just including a label option to corresponding node can work.

\documentclass{article}

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

\begin{document}

\begin{tikzpicture}
    \matrix[fill=black!20, matrix of nodes, nodes={module}, row sep=-\pgflinewidth]
    {
        |[label=Label]|Module 1        \\
        Module 2     \\
    };
\end{tikzpicture}

\end{document}

enter image description here

Note: row sep=-\pgflinewidth has been added to avoid double thickness lines between rows.

1
  • Thank you for this solution. I forgot about the label option.
    – pschulz
    Commented Jan 8, 2015 at 21:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .