TikZ:如何将表放在任意放置的节点内?

TikZ:如何将表放在任意放置的节点内?

我有几个相互关联的表。基于这个问题,我尝试将表放在矩阵内的节点内,但这限制了我的表必须排列在数组中。由于我的表不能自然地放入数组中,所以我需要一种替代解决方案,但我找不到。以下是我目前所拥有的:

\documentclass{article}

\usepackage{booktabs}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \matrix[ampersand replacement=\&] {
    \node (somenode) [shape=rectangle,fill=blue!20] at (0.0,0.0) {
      \begin{tabular}{lc} \toprule
        Name & Color  \\ \midrule
        A.N. Other & Blue \\
        J. Bloggs & Grey \\
        \bottomrule
      \end{tabular}
    }; \& 
    \node (somenode) [shape=rectangle,fill=orange!20] at (1.0,-2.0) {
      \begin{tabular}{lc} \toprule
        Name & Color  \\ \midrule
        A.N. Other & Blue \\
        J. Bloggs & Grey \\
        \bottomrule
      \end{tabular}
    };    
    \\
  };
\end{tikzpicture}

\end{document}

生成结果:

在此处输入图片描述

我需要做的是:

  1. 随意摆放桌子,
  2. 在表格之间画箭头来表示它们之间的关系,
  3. 在表格上方直接显示文字(作为标题),

最终看起来会是这样的(为了简单起见,我重复了第一个表,但表 3 和表 4 自然会有所不同):

在此处输入图片描述

任何指点都将不胜感激。

答案1

如果要将节点放在任意位置,为什么要使用矩阵?例如:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usepackage{booktabs}

\begin{document}
\begin{tikzpicture}[]
    \node [shape=rectangle,fill=orange!20, align=center](table1) at (1.0,-2.0) {
            Caption for table \\
            \begin{tabular}{lc} \toprule
                Name & Color  \\ \midrule
                A.N. Other & Blue \\
                J. Bloggs & Grey \\
                \bottomrule
            \end{tabular}
        };
    \node [shape=rectangle,fill=blue!20, align=center](table2) at (6.0,-7.0) {
            Caption for table \\
            \begin{tabular}{lc} \toprule
                Name & Color  \\ \midrule
                A.N. Other & Blue \\
                J. Bloggs & Grey \\
                \bottomrule
            \end{tabular}
        };
    \draw [very thick, -Stealth] (table1) -- (table2);
\end{tikzpicture}

\end{document}

给出:

在此处输入图片描述

如果您想要没有背景的标签,那么只需将两个节点一个放在另一个的顶部(使用锚点很容易),并且为了避免箭头接触框,您可以在节点中使用shorten >或。outer sep

例如,在下一个代码中,我定义一个宏来定位表格及其标题,然后我用一个可寻址的节点包含它们。

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, fit}
\usepackage{booktabs}
% arguments are:  name caption content position color
\NewDocumentCommand\mytableat{m m +m m m}{%
    % main node, will contain the table
    \node [shape=rectangle,fill=#5, align=center](#1-t) at (#4) {
        \begin{tabular}{lc} \toprule
            #3
            \bottomrule
        \end{tabular}
    };
    \node [align=center, anchor=south](#1-c) at (#1-t.north) {#2};
    % this is the node encompassing both --- use inner sep to create
    % a white "space" around it
    \node [fit=(#1-t) (#1-c), inner sep=3mm](#1){};
}

\begin{document}
\begin{tikzpicture}[]
    \mytableat{table1}{caption for table 1}{%
                Name & Color  \\ \midrule
                A.N. Other & Blue \\
                J. Bloggs & Grey \\
        }{1, -2}{orange!20}
    \mytableat{table2}{caption for table 2}{%
                Name & Color  \\ \midrule
                A.N. Other & Blue \\
                J. Bloggs & Grey \\
        }{6, -7}{blue!20}
    \draw [very thick, -Stealth] (table1) -- (table2);
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

使用shapes.multipart库和节点的相对定位:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning,
                shapes.multipart}
\usepackage{booktabs}

\begin{document}
\begin{table}[ht]
\centering
    \begin{tikzpicture}[
every edge/.style = {draw=gray, ultra thick, -Triangle,
                     shorten < =3mm, shorten >=3mm},
    N/.style args = {#1/#2}{rectangle split, rectangle split parts=2,
                            rectangle split part fill={white,#1!20},
                            text width=#2, align=center}
                        ] 
\node [N=blue/12em] (t1)  
      { Caption for first table 
        \nodepart{two}
            \begin{tabular}{lc}
            \toprule
            Name        & Color \\
            \midrule
            A.N. Other  & Steall Blue  \\
            J. Bloggs   & Grey  \\
            \bottomrule
            \end{tabular}
      };
\node [N=orange/10em, below right=2 and 2 of t1.south] (t2)
      { Caption for second table
        \nodepart{two}
            \begin{tabular}{lc}
            \toprule
            Name        & Color \\
            \midrule
            A.N. Other  & Blue  \\
            J. Bloggs   & Grey  \\
            \bottomrule
            \end{tabular}
      };
\node [N=blue/11em, below left=1 and 1 of t1.south] (t3)
      { Caption for third table
        \nodepart{two}
            \begin{tabular}{lc}
            \toprule
            Name        & Color \\
            \midrule
            A. N. Other & Blue  \\
            J. Bloggs   & Gray  \\
            \bottomrule
            \end{tabular}
      };
\node [N=blue/13em, below=2 of t3] (t4)
      { Caption for fourth table
        \nodepart{two}
            \begin{tabular}{lcc}
            \toprule
            Name        & Color & year\\
            \midrule
            A.N. Other  & Blue  & 2020\\
            J. Bloggs   & Grey  & 2021\\
            \bottomrule
            \end{tabular}
      };
\path   (t1) edge (t2)
        (t1) edge (t3)    
        (t3) edge (t4)
        (t2) edge (t4);
    \end{tikzpicture}
\end{table}
\end{document}

在此处输入图片描述

相关内容