Consider the following snippet that draws two identical (supposedly) matrices side by side.
\documentclass{book}
\usepackage{tikz}
\usepackage{xcolor}
\usetikzlibrary{positioning, matrix}
\begin{document}
\begin{tikzpicture}[]
\matrix (nc) [
matrix of nodes
, row sep = 0
, column sep = 0
, nodes = {inner sep = 0, fill = green!30!black!20}
, minimum size = 16.8pt
] {
0 & 0 & 0 & |(rtl)| 0 \
0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 \
|(lbl)| 0 & 0 & 0 & 0 \
};
\draw [color = red!50!black, thick] (lbl.south west) grid[step = 16.8pt] (rtl.north east);
\draw [color = red!50!black, thick] (lbl.south west) -- (rtl.north east);
\draw [color = green!30!black, very thick] (lbl.south west) rectangle (rtl.north east);
\matrix (world) [
matrix of nodes
, row sep = 0
, column sep = 0
, nodes = {inner sep = 0, fill = green!30!black!20}
, minimum size = 16.8pt
] [right = of nc] {
0 & 0 & 0 & |(rtr)| 0 \
0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 \
|(lbr)| 0 & 0 & 0 & 0 \
};
\draw [color = red!50!black, thick] (lbr.south west) grid[step = 16.8pt] (rtr.north east);
\draw [color = red!50!black, thick] (lbr.south west) -- (rtr.north east);
\draw [color = green!30!black, very thick] (lbr.south west) rectangle (rtr.north east);
\end{tikzpicture}
\end{document}
The problem is that in the right matrix the grid is misaligned for some inexplicable reason. In the picture below the diagonal line is just to show that it's properly drawn as expected, from one corner to another, in contrast to the grid. What am I doing wrong?


-2 * \pgflinewidthis the right separation in that example. – facetus Jul 07 '20 at 06:20