用线条连接两个表格

用线条连接两个表格

我怎样在 Latex 中绘制以下内容? 在此处输入图片描述

我已经创建了两个表格,但我不知道如何在单元格之间画线。

答案1

可以用两个TiKZ矩阵重现整个构图:

在此处输入图片描述

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning}

\begin{document}
\begin{tikzpicture}[
mycell/.style={draw, minimum size=7mm, fill=gray!30},
matrixA/.style={matrix of nodes,
    nodes={mycell, anchor=center},
    column sep=-\pgflinewidth, 
    row sep=-\pgflinewidth, 
    row 1/.style={nodes={draw=none, fill=none}},
    row 2/.style={font=\slshape}},
matrixB/.style={matrix of nodes,
    nodes={mycell, anchor=center},
    column sep=-\pgflinewidth, 
    row sep=-\pgflinewidth, 
    row 3/.style={nodes={draw=none, anchor=base, fill=none}, font=\slshape},
}]

\matrix[matrixA] (A) {
|[label=180:edge]| 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 21 & 21 & 22 & 23\\
|[label={[font=\rmfamily]180:destination}]| b & d & e & a & c & e & b & e & g & a & e & f & a & b & c & d & f & g & d & e & g & c & e & f \\
|[label=180:weight]| 4 & 12 & 9 &  4 & 8 & 7 & 8 & 5 & 2 & 12 & 3 & 6 & 9 & 7 & 5 & 3 & 1 & 13 & 6 & 1 & 11 & 2 & 13 & 11\\};

\matrix[matrixB, below=of A] (B) {
|[label=180:first edge]| 0 & 3 & 6 & 9 & 12 & 18 & 21\\
|[label=180:outdegree]| 3 & 3 & 3 & 3 & 6 & 3 & 3\\
|[label={[font=\rmfamily]180:vertex}]| a & b & c & d & e & f & g\\};

\foreach \i [count=\j] in {1, 4, 7, 10, 12, 18, 21} 
\draw (A-3-\i.south west)--(B-1-\j.north west);
\draw (A-3-23.south east)--(B-1-7.north east);

\end{tikzpicture}
\end{document}

答案2

这是使用nicematrixTikz 的解决方案。

\documentclass[landscape]{article}
\usepackage{geometry}
\geometry{left=1cm,right=1cm}
\usepackage{xcolor}
\usepackage{nicematrix,tikz}

\begin{document}

\begin{center}
\begin{NiceTabular}{*{24}{c}}[first-row,first-col,hvlines,columns-width=auto,name=A]
\CodeBefore
  \arraycolor{lightgray}
\Body
edge & 
0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 21 & 21 & 22 & 23\\
destination & 
\RowStyle{\itshape}
b & d & e & a & c & e & b & e & g & a & e & f & a & b & c & d & f & g & d & e & g & c & e & f \\
weight & 
4 & 12 & 9 &  4 & 8 & 7 & 8 & 5 & 2 & 12 & 3 & 6 & 9 & 7 & 5 & 3 & 1 & 13 & 6 & 1 & 11 & 2 & 13 & 11
\end{NiceTabular}
\end{center}

\vspace{3cm}

\begin{center}
\begin{NiceTabular}{*{7}{c}}[first-col,last-row,hvlines,columns-width=auto,name=B]
\CodeBefore
  \arraycolor{lightgray}
\Body
first edge & 
0 & 3 & 6 & 9 & 12 & 18 & 21\\
outdegree &
3 & 3 & 3 & 3 & 6 & 3 & 3\\
vertex & 
\RowStyle{\itshape}
a & b & c & d & e & f & g\\
\end{NiceTabular}
\end{center}


\tikzset{A/.style = {name prefix = A-} , B/.style = {name prefix = B-}} 

\begin{tikzpicture}[remember picture, overlay]
\draw [A]  (3-|1)  { [B] -- (1-|1) }
           (3-|4)  { [B] -- (1-|2) } 
           (3-|7)  { [B] -- (1-|3) }
           (3-|10) { [B] -- (1-|4) }
           (3-|12) { [B] -- (1-|5) }
           (3-|18) { [B] -- (1-|6) }
           (3-|21) { [B] -- (1-|7) }
           (3-|24) { [B] -- (1-|8) } ;
\end{tikzpicture}

\end{document}

上述代码的输出

相关内容