[Tex/LaTex] Overlay table with arrows

arrowstables

I have a table that I want to overlay with arrows to show which cell in one column is connected to which cells to another column. Something like that:

enter image description here

Is this possible? I could use an external drawing app but this way the table would look inconsistent with the rest of the tables.

\begin{table} 
\centering 
\caption{BLAh-blah} 
\begin{tabular}{|c|c|} \hline 
AAA & 111 \\ \hline & 222\\ \hline
 BBB & 333\\ \hline
  & 444\\ \hline 
  CCC & 555\\ \hline \end{tabular}
\label{tab:tab1} 
\end{table}

Some people suggest I do it on TikZ. So, how do I do it there? I have never worked with Tikz before?

Best Answer

Something likes this. Basically use of \tikzmark and \link skill defined in the macros. Also, thanks to @AboAmmar for the `shorten >=xx pt' suggestion.

\newcommand\tikzmark[2]{%
\tikz[remember picture,overlay] 
\node[inner sep=0pt,outer sep=2pt] (#1){#2};%
}

\newcommand\link[2]{%
\begin{tikzpicture}[remember picture, overlay, >=stealth, shorten >= 1pt]
  \draw[->] (#1.east) to  (#2.west);
\end{tikzpicture}%
}

enter image description here

Code

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{array}
\usepackage{tikz}

\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}

\newcommand\tikzmark[2]{%
\tikz[remember picture,overlay] 
\node[inner sep=0pt,outer sep=2pt] (#1){#2};%
}

\newcommand\link[2]{%
\begin{tikzpicture}[remember picture, overlay, >=stealth, shorten >= 1pt]
  \draw[->] (#1.east) to  (#2.west);
\end{tikzpicture}%
}

\begin{document}

\noindent
\begin{tabular}{|M{2cm}|M{2cm}|@{}M{0pt}@{}} 
\hline
\tikzmark{a}{AAA} & \tikzmark{1}{111} &\\ [2ex]\hline 
                  & \tikzmark{2}{222} &\\ [2ex]\hline
\tikzmark{b}{BBB} & \tikzmark{3}{333} &\\ [2ex]\hline
                  & \tikzmark{4}{444} &\\ [2ex]\hline
\tikzmark{c}{CCC} & \tikzmark{5}{555} &\\ [2ex]\hline
                  &                   &\\ [2ex]\hline
\end{tabular}

\link{a}{1}
\link{a}{2} 
\link{b}{2}
\link{b}{4} 
\link{c}{1} 
\link{c}{5}
\end{document}
Related Question