2

I try to use a customized shape (crossed rectangle) as below:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,calc,decorations.pathreplacing,shapes.misc}
\usetikzlibrary{matrix}
\tikzset{
    module/.style={draw, rectangle, fill=white, minimum size=1em,outer sep=0,inner sep=0},
    mycross/.style={path picture={
        \draw[#1] (path picture bounding box.south west) rectangle (path picture bounding box.north east);
        \draw[#1] (path picture bounding box.south west) --  (path picture bounding box.north east);
        \draw[#1] (path picture bounding box.south east) -- (path picture bounding box.north west);
    }}
}
\begin{document}
    \begin{tikzpicture}
    \tikzset{column align/.style 2 args={column #1/.append style={nodes={execute at begin
                    node={\setbox\matrixcellbox=\hbox\bgroup},
                    execute at end
                    node={\egroup\eqmakebox[\tikzmatrixname\the\pgfmatrixcurrentcolumn][#2]{\copy\matrixcellbox}}}}}}
    \matrix[fill=black!20, matrix of nodes, nodes={module}, 
    row sep=-\pgflinewidth,column sep=-\pgflinewidth
    ]
    {
        Y & Y & Y \\
        Y & Y & |[mycross]|  \\
        Y & Y & Y \\
    };
    \end{tikzpicture}   
\end{document}

But the the customized cell not fit with other cells, and I wish to add a red background with red!30 color as well.

enter image description here

2

You only need to add anchor=center. That is replace nodes={module} by nodes={module,anchor=center} to get

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,calc,decorations.pathreplacing,shapes.misc}
\tikzset{
    module/.style={draw, rectangle, fill=white, minimum size=1em,outer sep=0,inner sep=0},
    mycross/.style={path picture={
        \draw[#1] (path picture bounding box.south west) rectangle (path picture bounding box.north east);
        \draw[#1] (path picture bounding box.south west) --  (path picture bounding box.north east);
        \draw[#1] (path picture bounding box.south east) -- (path picture bounding box.north west);
    }}
}
\begin{document}
    \begin{tikzpicture}
    \tikzset{column align/.style 2 args={column #1/.append style={nodes={execute at begin
                    node={\setbox\matrixcellbox=\hbox\bgroup},
                    execute at end
                    node={\egroup\eqmakebox[\tikzmatrixname\the\pgfmatrixcurrentcolumn][#2]{\copy\matrixcellbox}}}}}}
    \matrix[fill=black!20, matrix of nodes, nodes={module,anchor=center}, 
    row sep=-\pgflinewidth,column sep=-\pgflinewidth
    ]
    {
        Y & Y & Y \\
        Y & Y & |[mycross]|  \\
        Y & Y & Y \\
    };
    \end{tikzpicture}   
\end{document}

enter image description here

BTW, in the current use case the document can be reduced to

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
    module/.style={draw, rectangle, fill=white, minimum size=1em,outer sep=0,inner sep=0},
    mycross/.style={path picture={
        \draw[#1] (path picture bounding box.south west) rectangle (path picture bounding box.north east);
        \draw[#1] (path picture bounding box.south west) --  (path picture bounding box.north east);
        \draw[#1] (path picture bounding box.south east) -- (path picture bounding box.north west);
    }}
}
\begin{document}
    \begin{tikzpicture}
    \matrix[fill=black!20, matrix of nodes, nodes={module,anchor=center}, 
    row sep=-\pgflinewidth,column sep=-\pgflinewidth
    ]
    {
        Y & Y & Y \\
        Y & Y & |[mycross]|  \\
        Y & Y & Y \\
    };
    \end{tikzpicture}   
\end{document}

In particular, column align is not used, and if you want to use it, make sure to load the eqmakebox package.

|improve this answer|||||
  • Great answer! How to fill background within that cell? – lucky1928 Nov 19 '19 at 15:06
  • @lucky1928 Just replace |[mycross]| by |[mycross,fill=blue!20]|, say. You can fill any cell by adding |[fill=...]| to that cell. – Schrödinger's cat Nov 19 '19 at 15:10

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.