将 TikZ 网格与 TikZ 矩阵对齐仅可工作一次

将 TikZ 网格与 TikZ 矩阵对齐仅可工作一次

考虑以下并排绘制两个相同(假定)矩阵的代码片段。

\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}

问题在于,在右侧矩阵中,网格由于某些无法解释的原因而未对齐。下图中的对角线只是为了显示它按照预期正确绘制,从一个角到另一个角,与网格形成对比。我做错了什么?

在此处输入图片描述

答案1

我通过仅使用矩阵的原生选项(与其他节点一样)简化了代码。我注释了代码以进行解释。

截屏

\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}

相关内容