tikz矩阵对齐问题

tikz矩阵对齐问题

我正在尝试解释 SAD(绝对差值总和)。经过大量调整后,我得到了一些看起来还不错的东西,只是水平和垂直对齐/距离很糟糕!而且,正如您所见,所有内容都是硬编码的。

我怎样才能让一切都排列整齐?

标签右侧未与 对齐Left,以及Center3 个“行”之间的垂直距离。Left, Center & Right矩阵可能应该向右移动一点,以使所有内容都集中在某个假想的垂直轴上。添加可重复的偏移直到看起来更好是一件麻烦事,因为有很多值需要更改,例如对于网格。

输出:

伤心

梅威瑟:

\documentclass[convert={density=300,size=640x480,outext=.png}]{standalone}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usepackage{adjustbox}
\usetikzlibrary{calc, matrix}

\begin{document}

\begin{adjustbox}{max size={.95\textwidth}{.8\textheight}}
    \begin{tikzpicture}
        \draw[step=0.5cm,color=lightgray] (-.75,-.75) grid (.75,.75);
        \matrix (template) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}]{
        2 & 5 & 5 \\
        4 & 0 & 7 \\
        7 & 5 & 9 \\
        };
        \node[label={[xshift=0cm, yshift=0cm]Template}] at (template.north) {};

        \draw[step=0.5cm,color=lightgray] (1.75,-.75) grid (4.25,.75);
        \matrix (search) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}] at (3,0){
        2 & 7 & \node[red]{5}; & \node[red]{8}; & \node[red]{6}; \\
        1 & 7 & \node[red]{4}; & \node[red]{2}; & \node[red]{7}; \\
        8 & 4 & \node[red]{6}; & \node[red]{8}; & \node[red]{5}; \\
        };
        \node[label={Search Image}] at (search.north) {};

        \draw[step=0.5cm,color=lightgray] (-1.75,-3.75) grid (-.25,-2.25);
        \matrix (left) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}] at (-1,-3){
        0 & 2 & 0 \\
        3 & 7 & 3 \\
        1 & 1 & 3 \\
        };
        \node[label={Left}] at (left.north) {};

        \draw[step=0.5cm,color=lightgray] (.25,-3.75) grid (1.75,-2.25);
        \matrix (center) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}] at (1,-3){
        5 & 0 & 3 \\
        3 & 4 & 5 \\
        3 & 1 & 1 \\
        };
        \node[label={Center}] at (center.north) {};

        \draw[step=0.5cm,color=lightgray] (2.25,-3.75) grid (3.75,-2.25);
        \matrix (right) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}] at (3,-3){
        \node[red]{3}; & \node[red]{3}; & \node[red]{1}; \\
        \node[red]{0}; & \node[red]{2}; & \node[red]{0}; \\
        \node[red]{1}; & \node[red]{3}; & \node[red]{4}; \\
        };
        \node[label={Right}] at (right.north) {};

        \matrix (sad) [matrix of nodes] at (1,-5) {
        20 & 25 & \node[red]{17}; \\
        };
        \node[label={SAD}] at (sad.north) {};
        % ..
        \end{tikzpicture}
    \end{adjustbox}
    \end{document}

答案1

这是一个非常不同的版本,其中没有明确的位置。center首先定义矩阵,然后相对于此放置另一个矩阵。这给出了矩阵(边缘)之间的一致距离。您可以通过更改node distance我添加到tikzpicture选项中的键来修改间距。第一个值是垂直间距,第二个是水平间距。positioning添加库以允许这种类型的定位。

的垂直对齐Right是通过在每个标签节点上添加一个来固定的text depth(类似于逗号对齐问题)。

要向节点添加选项,matrix of nodes您可以将其添加|[<options>]|到单元格的开头。我用它来制作一些值|[red]|。(事后看来:对于right矩阵,您可以直接将red选项添加到矩阵中。对于search您还可以修改column 3/ column 4/column 5样式,而不是修改这些列中的每个单元格。)

我还添加了几个样式,以减少代码重复。

网格是在矩阵之后绘制的,但是在backgrounds库的帮助下在背景层上绘制。

在此处输入图片描述

\documentclass[convert={density=300,size=640x480,outext=.png}]{standalone}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}

\usepackage{tikz}

\usetikzlibrary{calc, matrix, positioning,backgrounds}

\begin{document}
\begin{tikzpicture}[
  node distance=1cm and 0.5cm,
  matrixlabel/.style={text depth=3pt,above},
  matrixnodes/.style={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}]

\matrix (center) 
   [matrix of nodes,
    nodes={matrixnodes}]{
        5 & 0 & 3 \\
        3 & 4 & 5 \\
        3 & 1 & 1 \\
        };
\node [matrixlabel] at (center.north) {Center};

\matrix (right)
    [matrix of nodes,
     nodes={matrixnodes},
     right=of center]  {
        |[red]| {3} & |[red]| 3 & |[red]| 1 \\
        |[red]| 0 & |[red]| 2 & |[red]| 0 \\
        |[red]| 1 & |[red]| 3 & |[red]| 4 \\
        };
\node [matrixlabel] at (right.north) {Right};

\matrix (left)
    [matrix of nodes,
     nodes={matrixnodes},
     left=of center] {
        0 & 2 & 0 \\
        3 & 7 & 3 \\
        1 & 1 & 3 \\
        };
\node [matrixlabel] at (left.north) {Left};

\matrix (template)
    [matrix of nodes,
    nodes={matrixnodes},
    above=of left.north west,
    anchor=south west] {
        2 & 5 & 5 \\
        4 & 0 & 7 \\
        7 & 5 & 9 \\
        };
\node[matrixlabel] at (template.north) {Template};

\matrix (search) 
    [matrix of nodes,
     nodes={matrixnodes},
     above=of right.north east,
     anchor=south east] {
        2 & 7 & |[red]| 5 & |[red]| 8 & |[red]| 6 \\
        1 & 7 & |[red]| 4 & |[red]| 2 & |[red]| 7 \\
        8 & 4 & |[red]| 6 & |[red]| 8 & |[red]| 5 \\
        };
\node[matrixlabel] at (search.north) {Search Image};


\matrix (sad) 
     [matrix of nodes,
     below=of center]  {
        20 & 25 & |[red]| 17 \\
        };
\node[matrixlabel] at (sad.north) {SAD};

\begin{scope}[on background layer,lightgray]
  \foreach \mat in {template, left, center, right} {
    \foreach \i in {1,2,3} {
      \draw  (\mat-\i-1.west) -- (\mat-\i-3.east);
      \draw  (\mat-1-\i.north) -- (\mat-3-\i.south);
     }
   }

\foreach \i in {1,2,...,5}
   \draw  (search-1-\i.north) -- (search-3-\i.south);
\foreach \i in {1,2,3}
   \draw  (search-\i-1.west) -- (search-\i-5.east);
\end{scope}


\end{tikzpicture}
\end{document}

答案2

您可以使用scope环境和xshift参数:

\documentclass[convert={density=300,size=640x480,outext=.png}]{standalone}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usepackage{adjustbox}
\usetikzlibrary{calc, matrix}

\begin{document}

\begin{adjustbox}{max size={.95\textwidth}{.8\textheight}}
    \begin{tikzpicture}
        \draw[step=0.5cm,color=lightgray] (-.75,-.75) grid (.75,.75);
        \matrix (template) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}]{
        2 & 5 & 5 \\
        4 & 0 & 7 \\
        7 & 5 & 9 \\
        };
        \node[label={[xshift=0cm, yshift=0cm]Template}] at (template.north) {};

        \draw[step=0.5cm,color=lightgray] (1.75,-.75) grid (4.25,.75);
        \matrix (search) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}] at (3,0){
        2 & 7 & \node[red]{5}; & \node[red]{8}; & \node[red]{6}; \\
        1 & 7 & \node[red]{4}; & \node[red]{2}; & \node[red]{7}; \\
        8 & 4 & \node[red]{6}; & \node[red]{8}; & \node[red]{5}; \\
        };
        \node[label={Search Image}] at (search.north) {};

 \begin{scope}[xshift=0.75cm]
        \draw[step=0.5cm,color=lightgray] (-1.75,-3.75) grid (-.25,-2.25);
        \matrix (left) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}] at (-1,-3){
        0 & 2 & 0 \\
        3 & 7 & 3 \\
        1 & 1 & 3 \\
        };

        \node[label={Left}] at (left.north) {};

        \draw[step=0.5cm,color=lightgray] (.25,-3.75) grid (1.75,-2.25);
        \matrix (center) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}] at (1,-3){
        5 & 0 & 3 \\
        3 & 4 & 5 \\
        3 & 1 & 1 \\
        };
        \node[label={Center}] at (center.north) {};

        \draw[step=0.5cm,color=lightgray] (2.25,-3.75) grid (3.75,-2.25);
        \matrix (right) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}] at (3,-3){
        \node[red]{3}; & \node[red]{3}; & \node[red]{1}; \\
        \node[red]{0}; & \node[red]{2}; & \node[red]{0}; \\
        \node[red]{1}; & \node[red]{3}; & \node[red]{4}; \\
        };
        \node[label={Right}] at (right.north) {};

        \matrix (sad) [matrix of nodes] at (1,-5) {
        20 & 25 & \node[red]{17}; \\
        };
 \end{scope}
        \node[label={SAD}] at (sad.north) {};

        \end{tikzpicture}
    \end{adjustbox}
    \end{document}

在此处输入图片描述

相关内容