4

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?

3
  • Draw the whole thing in tikz
    – daleif
    Jan 28, 2015 at 12:14
  • Yes but that would change the letters, size of table compared to the other tables in the text. That offers no advantage over doing this in Visio or word and import the corresponding figure in the tex file.
    – Alexandros
    Jan 28, 2015 at 12:23
  • 1
    How would using tikz change the letters? I believe it will offer an advantage over those tools, because the font will be the same and all the source can be stored in the LaTeX file...
    – darthbith
    Jan 28, 2015 at 13:07

2 Answers 2

6

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}
1
  • 2
    The appearance would be better if you add shorten >= 2pt as an option in your tikzpicure.
    – AboAmmar
    Jan 28, 2015 at 13:31
1

With {NiceTabular} of nicematrix and TikZ to draw the arrows.

\documentclass{article}
\usepackage{caption}
\usepackage{nicematrix,tikz}
\usetikzlibrary{arrows.meta}

\begin{document}

\begin{table} 
\centering 
\caption{blah-blah} 
\begin{NiceTabular}{cc}[columns-width=2cm,hvlines]
   AAA & 111 \\ 
       & 222 \\ 
   BBB & 333 \\ 
       & 444 \\ 
   CCC & 555 \\ 
\CodeAfter
  \begin{tikzpicture} [shorten < = 0.5mm, shorten > = 0.5mm, blue, ->]
  \draw (1-1) -- (1-2) ; 
  \draw (1-1) -- (2-2) ; 
  \draw (3-1) -- (2-2) ; 
  \draw (3-1) -- (4-2) ; 
  \draw (5-1) -- (1-2) ; 
  \draw (5-1) -- (5-2) ; 
  \end{tikzpicture}
\end{NiceTabular}
\label{tab:tab1} 
\end{table}

\end{document}

Output of the above code

You must log in to answer this question.

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