How to have a specific style for empty cells in a tikz matrix

As far as I know, there is no empty cells style. But you can use a .list to declare a list of individual cells which has to use certain style. The code is taken form cjorssen answer to TikZ matrix, style for combination or rows and columns

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

\begin{document}
    \begin{tikzpicture}[
            table/.style={
                matrix of nodes,
                nodes in empty cells,
                nodes = {fill=gray, draw = black},
            },
            myset/.style args = {(#1,#2)}{%
    row #1 column #2/.style={nodes={draw=none, fill=none, inner sep=0pt}}}
        ]
        \matrix (test) [table,myset/.list={(2,2),(3,1),(3,3)}] {
            1 & 2 & 3 \\
            4 &   & 5 \\
              & 6 &  \\
        };
        \draw[red] (test-3-1)--(test-2-2)--(test-3-3);
    \end{tikzpicture}
\end{document}

enter image description here


The macro in the matrix library that adds nodes in empty cells seem to be \tikz@lib@matrix@empty@cell, defined on line 24 of tikzlibrarymatrix.code.tex. You can perhaps make a new style for the empty cells, and redefine that macro from the matrix library to include the new style in the node found in the definition of the macro.

output of code

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{empty node/.style={draw=none,fill=none}}
\makeatletter
\def\tikz@lib@matrix@empty@cell{\iftikz@lib@matrix@empty\node[name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn,empty node]{};\fi}
\makeatother
\begin{document}
    \begin{tikzpicture}[
            table/.style={
                matrix of nodes,
                nodes in empty cells,
                nodes = {fill=gray, draw = black},
            }
        ]
        \matrix (test) [table] {
            1 & 2 & 3 \\
            4 &   & 5 \\
        };

     \draw [red] (test-2-2.center) -- (test-1-3.center);
    \end{tikzpicture}
\end{document}