如何将两个多列表格(表 A 和表 B)并排放置,并从表 A 中的单元格绘制到表 B 中的另一个单元格的箭头?
我试过两个表格之间的箭头 并且它对于简单的表格也能正常工作。但如果我尝试使用嵌套节点来创建多个列,就会出错。
该怎么办?
这是我尝试过的,但如果我将标签改为较长的单词,如“Copenhagen”,整个单元格就会变得非常宽,在页面上看起来很奇怪。是不是边距的问题,我需要更改吗?我应该如何在嵌套节点中添加边框?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart,matrix,positioning}
\begin{document}
\begin{tikzpicture}
\node[rectangle split, rectangle split parts=3,draw,inner sep=2ex] (A) at (0,1)
{
\begin{tikzpicture}
\node[rectangle,text height=2em] (1st) {a11};
\node[rectangle,text height=2em,right=of 1st] (2nd) {a21};
\end{tikzpicture}
\nodepart{two}
\begin{tikzpicture}
\node[rectangle,text height=2em] (1st) {a12};
\node[rectangle,text height=2em,right=of 1st] (2nd) {a22};
\end{tikzpicture}
\nodepart{three}
\begin{tikzpicture}
\node[rectangle,text height=2em] (1st) {a13};
\node[rectangle,text height=2em,right=of 1st] (2nd) {a23};
\end{tikzpicture}};
\node[rectangle split, rectangle split parts=3,draw,inner sep=2ex] (B) at (7,1)
{\begin{tikzpicture}
\node[rectangle,text height=2em] (1st) {b11};
\node[rectangle,text height=2em,right=of 1st] (2nd) {b21};
\end{tikzpicture}
\nodepart{two}
\begin{tikzpicture}
\node[rectangle,text height=2em] (1st) {b12};
\node[rectangle,text height=2em,right=of 1st] (2nd) {b22};
\end{tikzpicture}
\nodepart{three}
\begin{tikzpicture}
\node[rectangle,text height=2em] (1st) {b13};
\node[rectangle,text height=2em,right=of 1st] (2nd) {b23};
\end{tikzpicture}};
\draw[->] (A.two east) -- (B.text west);
\draw[->] (B.two west) -- (A.text east);
\end{tikzpicture}
\end{document}
答案1
如果常规表格是一种选择,那么我建议以正常方式绘制表格(即使用您通常使用和喜欢的任何包等),然后使用 添加箭头作为覆盖tikzmark
。此库允许您在表格的 LaTeX 代码(或其他任何内容)中添加标记,然后使用这些标记来定位可以覆盖文本的绘图命令。
例如:
\documentclass{article}
\usepackage{array,tikz}
\usetikzlibrary{tikzmark,arrows.meta}
\newcolumntype{M}{>{\centering\mbox{}\vfill\arraybackslash}m{50pt}<{\vfill}}
\begin{document}
\begin{tabular}{|*{2}{M|}}
\hline
\tikzmark{la11}a11\tikzmark{ra11} & \tikzmark{la21}a21\tikzmark{ra21}\\\hline
\tikzmark{la12}a12\tikzmark{ra12} & \tikzmark{la22}a22\tikzmark{ra22}\\\hline
\tikzmark{la13}a13\tikzmark{ra13} & \tikzmark{la23}a23\tikzmark{ra23}\\\hline
\end{tabular}
\hskip 50pt
\begin{tabular}{|*{2}{M|}}
\hline
\tikzmark{lb11}b11\tikzmark{rb11} & \tikzmark{lb21}b21\tikzmark{rb21}\\\hline
\tikzmark{lb12}b12\tikzmark{rb12} & \tikzmark{lb22}b22\tikzmark{rb22}\\\hline
\tikzmark{lb13}b13\tikzmark{rb13} & \tikzmark{lb23}b23\tikzmark{rb23}\\\hline
\end{tabular}
\begin{tikzpicture}
[
remember picture,
overlay,
-latex,
color=blue!75!green,
yshift=1ex,
shorten >=1pt,
shorten <=1pt,
]
\draw ({pic cs:ra21}) -- ({pic cs:lb13});
\draw ({pic cs:ra23}) -- ({pic cs:lb21});
\draw ({pic cs:ra13}) -- ({pic cs:la21});
\draw ({pic cs:rb13}) -- ({pic cs:lb22});
\draw ({pic cs:lb12}) -- ({pic cs:ra22});
\draw ({pic cs:lb23}) to [bend left] ({pic cs:ra13});
\draw ({pic cs:ra11}) to [bend left] ({pic cs:lb11});
\end{tikzpicture}
\end{document}
我为每个单元格添加了两个标记,但显然您实际上只需要添加在环境中使用的标记tikzpicture
。
请注意,我在每个表格中添加了一条中心线来划分两列,正如您在 JLDiaz 的答案的评论中提到的那样,但这样做的好处是,您可以使用所有常用的格式命令来调整表格,但是您喜欢的。
答案2
根据我之前评论中的建议,有一个用于\matrix
创建表格的解决方案,可以轻松连接各个单元格,如最后所示。
唯一的问题是绘制分隔行的线。我无法通过style
矩阵的一些方法完成此操作,因此我不得不求助于编写特定于此任务的宏(但对于任何矩阵维度来说都足够通用)。
\usetikzlibrary{matrix,positioning}
\tikzset{ % Define some styles
my table/.style = {
matrix of nodes,
nodes = {minimum width=3em, minimum height=2.5em, inner sep=3pt},
draw,
inner sep=0pt,
},
my arrow/.style = {
-latex,
shorten >= -5pt,
shorten <= -5pt,
blue!50!black!60,
}
}
% Macro to draw the lines between rows
\def\drawLineBelowRow#1#2{% #1: row number, #2: matrix name
\draw (#2.west|-#2-#1-1.south) -- (#2.east|-#2-#1-1.south);
}
\begin{tikzpicture}
\matrix[my table] (A) { % First table (it is called A)
a11 & a21 \\
a21 & a22 \\
Copenhagen & a22 \\
};
\drawLineBelowRow{1}{A}
\drawLineBelowRow{2}{A}
\matrix[my table, right=of A] (B) { % Second table (it is called B)
b11 & b21 \\
b21 & b22 \\
b31 & b22 \\
};
\drawLineBelowRow{1}{B}
\drawLineBelowRow{2}{B}
% Drawing arrows between particular cells of each table is easy:
\draw[my arrow] (A-2-2) -- (B-1-1);
\draw[my arrow] (A-3-1) -- (B-2-2);
\end{tikzpicture}
结果:
答案3
您可以使用 绘制两个表格{NiceTabular}
,并使用 TikZ 通过在数组单元格下方nicematrix
创建的 TikZ 节点来绘制箭头。{NiceTabular}
\documentclass{article}
\usepackage{nicematrix,tikz}
\usetikzlibrary{bending,arrows.meta}
\begin{document}
\begin{center}
\NiceMatrixOptions{columns-width=1cm,hvlines}
\renewcommand{\arraystretch}{2}
\begin{NiceTabular}{cc}[name=a]
a11 & a12 \\
a21 & a22 \\
a31 & a32
\end{NiceTabular}
\hspace{1cm}%
\begin{NiceTabular}{cc}[name=b]
b11 & b12 \\
b21 & b22 \\
b31 & b32
\end{NiceTabular}
\begin{tikzpicture}
[remember picture , overlay , -> , blue, shorten < = 3pt, shorten > = 3pt]
\draw (a-1-1) to [bend left] (b-1-1) ;
\draw (a-3-1) to (a-1-2) ;
\draw (b-2-1) to (a-2-2) ;
\draw (a-3-2) to (b-1-2) ;
\draw (b-3-1) to (b-2-2) ;
\draw (a-1-2) to (b-3-1) ;
\draw (b-3-2) to [bend left] (a-3-1) ;
\end{tikzpicture}
\end{center}
\end{document}