0

I want to make this tables with arrows in latex. I write the code for tables but I don't know how to implement the arrows.

\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}

enter image description here

3 Answers 3

2
  • I suggest not to put each bit into a tabular column of its own, but to rely on the fact that digits (in particular in typewriter font) are equally spaced.
  • I define a command for each of the eight bit blocks that are moved around together (\Ba, ..., \Bh).
  • I define shortcuts for the six colors (ca,...,cf) such that they can be adjusted in one place.
  • For the graphical elements, I use \tikznode to assign a name to each occurrence of a bit block (a1,...,h1,a2,...,h2), in order to refer to the positions in the picture part.
  • The arrows and bars are drawn in the tikzpicture environment. It needs the options overlay and remember picture.
  • Run LaTeX twice to get the positions right.

See my answer to "How to add arrow in equations and matrix?" for some explanations on how to draw arrows between text elements.

enter image description here

\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

With a TikZ matrix, I leave it to you to set the colors you like:

\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}

enter image description here

1

If I've well understood, here is a code to do such things with pstricks: I inserted txwo pairs of empty nodes at the relevant places, and connected them with the \ncbar command:

\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} 

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .