我如何创建如下所示的带有表格和箭头的图表。
我正在寻找可以进一步构建的样板代码。其次,还有其他工具可以快速绘制此类图像吗?
答案1
这是一个入门指南,可让您了解您可以做什么。
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
%% I define a "style" for the "cells"
\tikzset{
my cell/.style={
draw,
minimum size=5ex,
node distance=0pt,
}}
%% to make changing appearances for various aspects of
%% the diagram easier, I define several macros which need
%% only be changed here. Even though two of these macros
%% have the same definition, I use two to allow the possibility
%% that these styles could be diffferent.
\newcommand\mynodestyle{\sffamily}
\newcommand\myrelocatestyle{\sffamily\bfseries}
\newcommand\mynumberstyle{\sffamily}
\begin{document}
\begin{tikzpicture}[>=Stealth]
%--- BEFORE ---%
\node[my cell] (before/0) {};
\node[anchor=east] at (before/0.west) {0};
%% \myn=node name
%% \myc=node content
%% \myp=previous node
\foreach \myn/\myc [remember=\myn as \myp (initially 0)] in {1/,2/b,3/,4/c,5/,6/a,7/}
{
\node[my cell,below=of before/\myp] (before/\myn) {\mynodestyle\myc};
\node[anchor=east] (before/\myn/label) at (before/\myn.west) {\mynumberstyle\myn};
}
%% right hand anotations
\draw[dashed,arrows=->] (before/6.east)
to[out=30,in=-30,looseness=2]
node[midway,right ] {\myrelocatestyle relocate}
(before/4.east);
\draw[dashed,arrows=->] (before/4.east)
to[out=30,in=-30,looseness=1.5]
node[midway,right ] {\myrelocatestyle relocate}
(before/1.east);
%% left hand anotations
\path (before/5) ++ (175:8em) node (item/x) {item $x$};
\draw[arrows=->] (item/x) -- node[midway,left,yshift=2ex,xshift=1ex] {$h_1(x)$} (before/2/label);
\draw[arrows=->] (item/x) -- node[midway,below,xshift=-1ex] {$h_2(x)$} (before/6/label);
\node[below=0.5cm of before/7] {(a) before inserting item $x$};
%--- AFTER ---%
%% Here's a slightly different approach to placing the contents of
%% each node.
\foreach \myn in {0,1,...,7}
{
\def\myc{}
\ifnum\myn=6\relax\def\myc{x}\fi
\ifnum\myn=4\relax\def\myc{a}\fi
\ifnum\myn=2\relax\def\myc{b}\fi
\ifnum\myn=1\relax\def\myc{c}\fi
\node[my cell,right=6cm of before/\myn] (after/\myn) {\mynodestyle\myc};
\node[anchor=east] (after/\myn/label) at (after/\myn.west) {\mynumberstyle\myn};
}
\node[below=0.5cm of after/7] {(b) after item $x$ inserted};
%--- summary ---%
\foreach \myn in {0,1,...,7}
{
\node[my cell,right=6cm of after/\myn] (summary/\myn/a) {};
\node[anchor=east] (summary/\myn/label) at (summary/\myn/a.west) {\mynumberstyle\myn};
\foreach \mycol [remember=\mycol as \mypcol (initially a)] in {b,c,d}
{
\node[my cell,right=of summary/\myn/\mypcol] (summary/\myn/\mycol) {};
}
}
\foreach \mycol in {a,b,c}
{
\draw[white,dashed,line width=0.9pt] ($(summary/0/\mycol.north east)+(0,-2pt)$) -- ($(summary/7/\mycol.south east)+(0,2pt)$);
}
\end{tikzpicture}
\end{document}
虽然不完全是样板,但应该足以帮助您开始和完成。