为什么在此 TikZ 图片中行对齐但列对齐不正确?

为什么在此 TikZ 图片中行对齐但列对齐不正确?

由于某种原因,该列与矩阵 B 无法正确对齐。我不知道为什么。我讨厌不得不yshift为此使用这么多毫米、这么多毫米。

B 的列与矩阵 B 不对齐

\documentclass[11pt]{standalone}

\usepackage{tikz}

\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\definecolor{shade1}{rgb}{0.9, 0.9, 0.9}
\definecolor{shade2}{rgb}{0.8, 0.8, 0.8}
\definecolor{shade3}{rgb}{0.65, 0.65, 0.65}
\definecolor{shade4}{rgb}{0.45, 0.45, 0.45}

\begin{document}

\begingroup
\newlength{\matrixSize}
\setlength{\matrixSize}{2cm}
\newlength{\matrixSep}
\setlength{\matrixSep}{4mm}
\begin{tikzpicture}[%
    auto,
    box/.style={%
      draw,
      rectangle,
    },
    matrix/.style={%
      box,
      fill=shade2,
      minimum size=2cm,
      inner sep=0pt,
      node distance=\matrixSep,
    },
    cell/.style={%
      box,
      fill=shade3,
      minimum size=0.1\matrixSize,
      inner sep=0pt,
    },
  ]

  % Matrices
  \node [matrix]             (C) {C};
  \node [matrix, left=of C]  (A) {A};
  \node [matrix, above=of C] (B) {B};

  % Cell to calculate in C
  \node [cell] at ([xshift=-0.2\matrixSize, yshift=0.2\matrixSize]
    C.south east) (cell) {};

  % Row from A
  \path let \p1 = (cell),
            \p2 = (A.base),
            \p3 = (A.west),
            \p4 = (A.east)
         in
           node [%
             cell,
             minimum width=\x4-\x3-\pgflinewidth,
           ] at ([yshift=\y1-\y2] A.base) (row) {};

  % Column from B
  \path let \p1 = (cell),
            \p2 = (B.base),
            \p3 = (B.south),
            \p4 = (B.north)
         in
           node [%
             cell,
             minimum height=\y4-\y3-\pgflinewidth,
           ] at ([xshift=\x1-\x2] B.base) (col) {};
\end{tikzpicture}
\endgroup

\end{document}

答案1

您需要使用B.center锚点来代替B.base;后者的位置比前者略低。在下面的代码中,我展示了两者的区别,分别使用红色圆圈B.center和蓝色圆圈来表示B.base

\documentclass[11pt]{standalone}

\usepackage{tikz}

\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\definecolor{shade1}{rgb}{0.9, 0.9, 0.9}
\definecolor{shade2}{rgb}{0.8, 0.8, 0.8}
\definecolor{shade3}{rgb}{0.65, 0.65, 0.65}
\definecolor{shade4}{rgb}{0.45, 0.45, 0.45}

\begin{document}

\begingroup
\newlength{\matrixSize}
\setlength{\matrixSize}{2cm}
\newlength{\matrixSep}
\setlength{\matrixSep}{4mm}
\begin{tikzpicture}[%
    auto,
    box/.style={%
      draw,
      rectangle,
    },
    matrix/.style={%
      box,
      fill=shade2,
      minimum size=2cm,
      inner sep=0pt,
      node distance=\matrixSep,
    },
    cell/.style={%
      box,
      fill=shade3,
      minimum size=0.1\matrixSize,
      inner sep=0pt,
    },
  ]

  % Matrices
  \node [matrix]             (C) {C};
  \node [matrix, left=of C]  (A) {A};
  \node [matrix, above=of C] (B) {B};

  % Cell to calculate in C
  \node [cell] at ([xshift=-0.2\matrixSize, yshift=0.2\matrixSize]
    C.south east) (cell) {};

  % Row from A
  \path let \p1 = (cell),
            \p2 = (A.base),
            \p3 = (A.west),
            \p4 = (A.east)
         in
           node [%
             cell,
             minimum width=\x4-\x3-\pgflinewidth,
           ] at ([yshift=\y1-\y2] A.base) (row) {};

  % Column from B
  \path let \p1 = (cell),
            \p2 = (B.base),
            \p3 = (B.south),
            \p4 = (B.north)
         in
           node [%
             cell,
             minimum height=\y4-\y3-\pgflinewidth,
           ] at ([xshift=\x1-\x2] B.center) (col) {};

\node[circle,red, draw] at (B.center) {};
\node[circle,blue, draw] at (B.base) {};
\end{tikzpicture}
\endgroup

\end{document}

在此处输入图片描述

稍微解释一下:如果具有rectangle形状的节点的文本标签字段为空,则.base.center锚点将相等;但是,添加文本标签会导致锚点垂直移动.base,如以下代码所示:

\documentclass[11pt]{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[node distance=1cm and 1cm]
  \node[name=t,shape=rectangle] {};
  \foreach \anchor/\placement in {center/above, base/below}
    \draw[shift=(t.\anchor)] plot[mark=x] coordinates{(0,0)} 
      node[\placement] {\scriptsize\texttt{(t.\anchor)}};
  \node[name=s,shape=rectangle,xshift=3cm] {\Huge A};
  \foreach \anchor/\placement in {center/above,base/below}
    \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
      node[\placement] {\scriptsize\texttt{(s.\anchor)}};
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容