如何在两个表之间画箭头?

如何在两个表之间画箭头?

我想在 latex 中制作带箭头的表格。我编写了表格代码,但我不知道如何实现箭头。

\begin{table}[h]
\centering
\begin{tabular}{lllllllllllllllllllll}
\cellcolor{blue!10}0    
&\cellcolor{blue!10}1
&\cellcolor{blue!10}0
&\cellcolor{blue!10}1
&\cellcolor{blue!10}0
&\cellcolor{blue!10}...
&\cellcolor{blue!10}1
&\cellcolor{blue!10}0
&\cellcolor{blue!10}0
&\cellcolor{blue!10}1
& \cellcolor{blue!10}1
&\cellcolor{blue!20}0
&\cellcolor{blue!20}1
&\cellcolor{blue!20}0
&\cellcolor{blue!20}0
&\cellcolor{blue!20}0
&\cellcolor{blue!20}1
&\cellcolor{blue!20}1
&\cellcolor{blue!20}1
&\cellcolor{blue!20}0
&\cellcolor{blue!20}1
\end{tabular}
\begin{tabular}{lllllllllllllllllllll}
\cellcolor{purple!10}1  
&\cellcolor{purple!10}1
&\cellcolor{purple!10}0
&\cellcolor{purple!10}0
&\cellcolor{purple!10}0
&\cellcolor{purple!10}...
&\cellcolor{purple!10}0
&\cellcolor{purple!10}0
&\cellcolor{purple!10}0
&\cellcolor{purple!10}1
& \cellcolor{purple!10}1
&\cellcolor{purple!20}1
&\cellcolor{purple!20}1
&\cellcolor{purple!20}0
&\cellcolor{purple!20}1
&\cellcolor{purple!20}0
&\cellcolor{purple!20}1
&\cellcolor{purple!20}0
&\cellcolor{purple!20}1
&\cellcolor{purple!20}0
&\cellcolor{purple!20}1
\end{tabular}
\end{table}

在此处输入图片描述

答案1

  • 我建议不要将每个位放入其自己的表格列中,而是依靠数字(特别是打字机字体)等距的事实。
  • 我为一起移动的八个位块中的每一个定义了一个命令(\Ba,......,\Bh)。
  • 我为六种颜色定义了快捷方式(ca,...,cf),以便可以在一个地方进行调整。
  • 对于图形元素,我用来\tikznode为每个位块的出现分配一个名称(a1,...,,,h1... a2h2),以便在图片部分中引用其位置。
  • 在环境中绘制箭头和条形图tikzpicture。需要选项overlayremember picture
  • 运行 LaTeX 两次以获得正确的位置。

我对“如何在方程和矩阵中添加箭头?”的回答有关如何在文本元素之间画箭头的一些解释。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\newcommand\tikznode[3][]{%
  \tikz[remember picture,baseline=(#2.base)]
    \node[minimum size=0pt,inner sep=0pt,#1](#2){#3};%
}
\tikzset{block/.style={font=\ttfamily}}  
\usepackage{xcolor}
% colors
\colorlet{ca}{blue!20}
\colorlet{cb}{blue!50!red!30}
\colorlet{cc}{red!40!brown!40}
\colorlet{cd}{magenta!30}
\colorlet{ce}{olive!30}
\colorlet{cf}{orange!30}
% blocks of bits
% argument = identifier of block occurrence
\newcommand\Ba[1]{\tikznode[block]{#1}{\colorbox{ca}{110}}}
\newcommand\Bb[1]{\tikznode[block]{#1}{\colorbox{ca}{001000}\colorbox{cb}{1000}}}
\newcommand\Bc[1]{\tikznode[block]{#1}{\colorbox{cb}{100}\colorbox{cc}{1011}}}
\newcommand\Bd[1]{\tikznode[block]{#1}{\colorbox{cc}{0111}}}
\newcommand\Be[1]{\tikznode[block]{#1}{\colorbox{cd}{111}}}
\newcommand\Bf[1]{\tikznode[block]{#1}{\colorbox{cd}{000100}\colorbox{ce}{0010}}}
\newcommand\Bg[1]{\tikznode[block]{#1}{\colorbox{ce}{110}\colorbox{cf}{1111}}}
\newcommand\Bh[1]{\tikznode[block]{#1}{\colorbox{cf}{0010}}}
\begin{document}
  
\begin{tabular}{l}
  \Ba{a1}\Bb{b1}\Bc{c1}\Bd{d1}\\[2ex]
  \Be{e1}\Bf{f1}\Bg{g1}\Bh{h1}\\[5ex]
  \Ba{a2}\Bf{f2}\Bc{c2}\Bh{h2}\\[2ex]
  \Be{e2}\Bb{b2}\Bg{g2}\Bd{d2}
\end{tabular}
\begin{tikzpicture}[remember picture,overlay,thick,rounded corners,shorten <=2pt,shorten >=2pt,>=stealth']
% arrows
\draw[->,blue] (a1) -- +(-1,0) |- (a2);
\draw[->,blue] (b1) edge[bend right] (b2);
\draw[->,blue] (c1) edge[bend left] (c2);
\draw[->,blue] (d1) -- +(1,0) |- (d2);
\draw[->,red] (e1) -- +(-0.8,0) |- (e2);
\draw[->,red] (f1) edge[bend right] (f2);
\draw[->,red] (g1) edge[bend left] (g2);
\draw[->,red] (h1) -- +(0.8,0) |- (h2);
% upper black lines
\draw[shorten >=-8pt,shorten <=-8pt] (a1.east) -- (e1.east);
\draw[shorten >=-8pt,shorten <=-8pt] (b1.east) -- (f1.east);
\draw[shorten >=-8pt,shorten <=-8pt] (c1.east) -- (g1.east);
% lower black lines
\draw[shorten >=-8pt,shorten <=-8pt] (a2.east) -- (e2.east);
\draw[shorten >=-8pt,shorten <=-8pt] (b2.east) -- (f2.east);
\draw[shorten >=-8pt,shorten <=-8pt] (c2.east) -- (g2.east);
\end{tikzpicture}
\end{document}

答案2

使用 TikZ 矩阵,我让你自己设置你喜欢的颜色:

\documentclass{article}
\usepackage{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{fit}
\usetikzlibrary{backgrounds}

\tikzset{
    mymatr/.style={
        matrix of nodes,
        nodes in empty cells,
        nodes={
            draw, 
            inner sep = 0pt, 
            minimum size=14pt,
            font=\bfseries
            },
        column sep=-\pgflinewidth
        },
    myarrow/.style={
        line width=2.5pt, 
        -{Straight Barb[length=7pt,width=9pt]}, 
        },
    mysegm/.style={
        line width=3pt,
        shorten >=-6pt,
        shorten <=-6pt 
        }
    }
\begin{document}
\begin{figure}\centering
\begin{tikzpicture}
\matrix[mymatr] (M) {
1&1&0&0&0&1&0&0&0&1&0&0&0&1&0&0&1&0&1&1&0&1&1&1\\[25pt]
1&1&1&0&0&0&1&0&0&0&0&1&0&1&1&0&1&1&1&1&0&0&1&0\\[50pt]
1&1&0&0&0&0&1&0&0&0&0&1&0&1&0&0&1&0&1&1&0&0&1&0\\[25pt]
1&1&1&0&0&1&0&0&0&1&0&0&0&1&1&0&1&1&1&1&0&1&1&1\\
}; 
% background colors
\scoped[on background layer]
    {% first row
    \node[inner sep = 0pt, fill=blue!10, fit=(M-1-1)(M-1-9)] {} ;
    \node[inner sep = 0pt, fill=blue!20, fit=(M-1-10)(M-1-16)] {};
    \node[inner sep = 0pt, fill=purple!10, fit=(M-1-17)(M-1-24)] {};
    % second row
    \node[inner sep = 0pt, fill=cyan!50, fit=(M-2-1)(M-2-9)] {} ;
    \node[inner sep = 0pt, fill=green!50, fit=(M-2-10)(M-2-16)] {};
    \node[inner sep = 0pt, fill=orange!50, fit=(M-2-17)(M-2-24)] {};
    % third row
    \node[inner sep = 0pt, fill=blue!10, fit=(M-3-1)(M-3-3)] {} ;
    \node[inner sep = 0pt, fill=cyan!50, fit=(M-3-4)(M-3-9)] {} ;
    \node[inner sep = 0pt, fill=green!50, fit=(M-3-10)(M-3-13)] {};
    \node[inner sep = 0pt, fill=blue!20, fit=(M-3-14)(M-3-16)] {};
    \node[inner sep = 0pt, fill=purple!10, fit=(M-3-17)(M-3-20)] {};
    \node[inner sep = 0pt, fill=orange!50, fit=(M-3-21)(M-3-24)] {};
    % fourth row
    \node[inner sep = 0pt, fill=cyan!50, fit=(M-4-1)(M-4-3)] {} ;
    \node[inner sep = 0pt, fill=blue!10, fit=(M-4-4)(M-4-9)] {} ;
    \node[inner sep = 0pt, fill=blue!20, fit=(M-4-10)(M-4-13)] {};
    \node[inner sep = 0pt, fill=green!50, fit=(M-4-14)(M-4-16)] {};
    \node[inner sep = 0pt, fill=orange!50, fit=(M-4-17)(M-4-20)] {};
    \node[inner sep = 0pt, fill=purple!10, fit=(M-4-21)(M-4-24)] {};
}
% arrows
\draw[myarrow, blue] (M-1-1) -- ++(-1.5,0) |- (M-3-1);
\draw[myarrow, orange] (M-2-1) -- ++(-1,0) |- (M-4-1);
\draw[myarrow, blue] (M-1-24) -- ++(1.5,0) |- (M-4-24);
\draw[myarrow, orange] (M-2-24) -- ++(1,0) |- (M-3-24);
\draw[myarrow, orange] (M-2-8) -- (M-3-8);
\draw[myarrow, blue] ([xshift=4pt]M-1-10.south) -- ([xshift=4pt]M-4-10.north);
\draw[myarrow, blue] ([xshift=4pt]M-1-17.south) -- ([xshift=4pt]M-3-17.north);
\draw[myarrow, orange] ([xshift=4pt]M-2-18.south) -- ([xshift=4pt]M-4-18.north);
% black segments
\draw[mysegm](M-1-3.north east)--(M-2-3.south east);
\draw[mysegm](M-1-13.north east)--(M-2-13.south east);
\draw[mysegm](M-1-20.north east)--(M-2-20.south east);
\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

答案3

如果我理解得没错,这里有一个可以做这些事情的代码pstricks:我在相关位置插入了两对空节点,并使用以下\ncbar命令将它们连接起来:

\documentclass[11pt, svgnames]{article}
\usepackage{array}
\usepackage[table]{xcolor} 
\usepackage{pst-node, pst-arrow}

\begin{document}

\begin{table}[h]
\centering
\begin{tabular}{!{\pnode[0,0.5ex]{A}}>{\cellcolor{blue!10}}l*{10}{>{\cellcolor{blue!10}}l}*{10}{>{\cellcolor{blue!20}}l}!{\pnode[0,0.5ex]{C}}}
0 & 1 & 0 & 1 & 0 & ... & 1 & 0 & 0 & 1 & 1
& 0 & 1 & 0 & 0 & 0 & 1 & 1 & 1 & 0 & 1
\end{tabular}
\vskip 4ex
\begin{tabular}{!{\pnode[-0.2ex,0.5ex]{B}}>{\cellcolor{purple!10}}l*{10}{>{\cellcolor{purple!10}}l}*{10}{>{\cellcolor{purple!20}}l}!{\pnode[0.2ex,0.5ex]{D}}}
1 & 1 & 0 & 0 & 0 & ... & 0 & 0 & 0 & 1 & 1
& 1 & 1 & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 1
\end{tabular}
\psset{linewidth=2pt, arrowinset=0.12, arm=15pt, veearrowangle=45, veearrowlinewidth=2pt, linejoin=1}
\ncbar[angle=180, linecolor=Crimson]{-v}{A}{B}
\ncbar[angle=0, linecolor=LightBlue]{-v}{C}{D}
\end{table}

\end{document} 

在此处输入图片描述

相关内容