1

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?

enter image description here

facetus
  • 837
  • Hi and welcome. Please give a fully compilable code. – AndréC Jul 07 '20 at 05:48
  • Wouldn't it be easier to let the matrix draw the lines instead of overlaying a line less matrix with a grid and outline? – leandriis Jul 07 '20 at 05:50
  • As far as I understand, the matrix can draw only its frame. You can specify to draw the nodes, but that results in double lines between them. – facetus Jul 07 '20 at 06:00
  • You can reduce the distance between the nodes resulting in a grid like effect. See for example: https://tex.stackexchange.com/a/534469/134144 – leandriis Jul 07 '20 at 06:05
  • Thank you. In this example if I specify thick line for the node borders, the inner lines look 2 times thicker, but this is at least something I can work with. – facetus Jul 07 '20 at 06:17
  • It seems like -2 * \pgflinewidth is the right separation in that example. – facetus Jul 07 '20 at 06:20

1 Answers1

2

I simplified the code by using only the native options of the matrices (which are nodes like the others). I commented the code to explain.

screenshot

\documentclass{book}
\usepackage{tikz}
\usepackage{xcolor}
\usetikzlibrary{positioning, matrix}

\begin{document} \begin{tikzpicture}[] \matrix (nc) [ matrix of nodes , inner sep=0pt% <--- delete space around frame , row sep = 0pt , column sep = 0pt , nodes = {draw=red,inner sep = 0pt, fill = green!30!black!20} , minimum size = 16.8pt ] { 0 & 0 & 0 & 0 \ 0 & 0 & 0 & 0 \ 0 & 0 & 0 & 0 \ 0 & 0 & 0 & 0 \ }; % \draw [color = red!50!black, thick] (lbl.south west) grid[step = 16.8pt] (rtl.north east); \draw [color = blue, thick] (nc.south west) -- (nc.north east); % \draw [color = green!30!black, very thick] (lbl.south west) rectangle (rtl.north east); \matrix (world) [ matrix of nodes ,inner sep =0pt
, row sep = 0 , column sep = 0 , nodes = {draw,inner sep = 0, fill = green!30!black!20} , minimum size = 16.8pt ] [right = of nc] { 0 & 0 & 0 & 0 \ 0 & 0 & 0 & 0 \ 0 & 0 & 0 & 0 \ 0 & 0 & 0 & 0 \ }; % \draw [color = red!50!black, thick] (world.south west) grid[step = 16.8pt] (rtr.north east); \draw [color = red!50!black, thick] (world.south west) -- (world.north east); % \draw [color = green!30!black, very thick] (lbr.south west) rectangle (rtr.north east); \end{tikzpicture} \end{document}

AndréC
  • 24,137
  • Thanks! The inner lines are two times thicker. Should I use inner sep = -\pgflinewidth? – facetus Jul 07 '20 at 06:46
  • @facetus simply add very thinhere nodes = {draw=red,very thin,inner sep = 0pt, fill = green!30!black!20} – AndréC Jul 07 '20 at 07:47
  • I need them thick. I ended up adding row sep = -\pgflinewidth, column sep = -\pgflinewidth to get nice lines. – facetus Jul 07 '20 at 17:49