[Tex/LaTex] TikZ matrix, why doesn’t “every even row” work with “row sep” option

tikz-matrix

Please consider the following mwe:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,
            nodes={draw, font=\footnotesize, minimum size=1em,
                   anchor=center,inner sep=0pt},
                   column sep=-\pgflinewidth,
                   row sep=-\pgflinewidth,
                   every even column/.style={column sep=2pt},
                   every even row/.style={row sep=2pt},% doesn't insert row separation
                   inner sep=1pt,
                   left delimiter={[},right delimiter={]},
             ]
{
\alpha  & \beta   & \gamma  & \delta    \\
\alpha  & \beta   & \gamma  & \delta    \\
\alpha  & \beta   & \gamma  & \delta    \\
\alpha  & \beta   & \gamma  & \delta    \\
};
\end{tikzpicture}
\end{document}

However every even column/.style={column sep=2pt}, yields the expected result, say, introducing additional column separation after each second column, the every even row/.style={row sep=2pt}, doesn't. Did I do something wrong or this is a bug in TikZ v3.1 (I've never tested this before, so I don't know if the same thing happens in v3.0.1)?

enter image description here

edit:

The same thing happens with, for example,

row 2/.style = {row sep=2pt} 

thus it might mean that every even row/.style=... and row <row number> work fine only if one wants to change some properties of cells inside row, for example, the color of nodes borders:

every even row/.style={draw red},

but not if one aims to change row separation (see @AndréC's comment below). However it is interesting, that this is possible to do in every column sep

Of course, the (temporary) workaround terminates each second row of the matrix with for example [2pt], but this is annoying when the matrix is huge like here.

Best Answer

This is from a bug in the matrix library. The bug was reported in issue #504 in the PGF bug tracker.

The problem is the scope where \pgfmatrixrowsep is set (the value you pass to row sep) ends before that value is actually used.

Henri Menke submitted a fix to this issue which will be in the next release of PGF.

Meanwhile, the following patch will work:

\makeatletter
\def\pgfmatrixendrow{%
  \let\pgf@matrix@signal@cell@end=\pgf@matrix@signal@cell@end
  \pgf@matrix@last@cell@in@rowtrue%
  \xdef\pgf@matrix@rowsep{\pgfmatrixrowsep}% <-- Define \pgf@matrix@rowsep globally
  &% <-- Scope ends here
  \pgf@matrix@correct@calltrue%
  \global\pgf@matrix@fixedfalse%
  \pgf@y=0pt%
  % The previous version used \pgfmatrixrowsep, which was reset at the &
  % \pgf@matrix@addtolength\pgf@y{\pgfmatrixrowsep}
  \pgf@matrix@addtolength\pgf@y{\pgf@matrix@rowsep}% <-- Now use the global def
  \pgfutil@ifnextchar[{\pgfmatrixendrow@skip}{\pgf@matrix@finish@line}%
}%
\makeatother

MWE:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix}

\makeatletter
\def\pgfmatrixendrow{%
  \let\pgf@matrix@signal@cell@end=\pgf@matrix@signal@cell@end
  \pgf@matrix@last@cell@in@rowtrue%
  \xdef\pgf@matrix@rowsep{\pgfmatrixrowsep}%
  &\pgf@matrix@correct@calltrue%
  \global\pgf@matrix@fixedfalse%
  \pgf@y=0pt%
  \pgf@matrix@addtolength\pgf@y{\pgf@matrix@rowsep}%
  \pgfutil@ifnextchar[{\pgfmatrixendrow@skip}{\pgf@matrix@finish@line}%
}%
\makeatother

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,
            nodes={draw, font=\footnotesize, minimum size=1em,
                   anchor=center,inner sep=0pt},
                   column sep=-\pgflinewidth,
                   row sep=-\pgflinewidth,
                   every even column/.style={column sep=2pt},
                   every even row/.style={row sep=2pt},% doesn't insert row separation
                   inner sep=1pt,
                   left delimiter={[},right delimiter={]},
             ]
{
\alpha  & \beta   & \gamma  & \delta    \\
\alpha  & \beta   & \gamma  & \delta    \\
\alpha  & \beta   & \gamma  & \delta    \\
\alpha  & \beta   & \gamma  & \delta    \\
};
\end{tikzpicture}
\end{document}
Related Question