Annotating Tables With Tikz; Adding Arrows

I'd like to be able to draw an arrow with some text on top connecting two arbitrary (i.e. possibly non-right-/left-most, possibly nonadjacent) cells of a table with a left-looking arrow. In particular, I'm trying to get an arrow on the top of a table similar in looks to what you get here, though my tables don't contain numbers in their cells, and I'm looking for a solution that doesn't connect all and only pairwise adjacent cells. I'm not sure what the right way to modify this is.

I'm working with beamer, and using tikz (though I'm quite new to it) and tabularx.

Is there a way to do this?

Edit: Here's a code like the one I'm messing with. I'm not sure how to fix.

    \documentclass[pdf]{beamer}
\mode<presentation>{}
\usepackage[english]{babel}
\usepackage{natbib}
\usepackage{color, colortbl}
\renewcommand{\bibnumfmt}[1]{#1.}
\usepackage{ marvosym }
\usepackage{ tikzsymbols }
\usepackage{ wasysym }
\usepackage{tikz}
\usetikzlibrary{tikzmark, positioning, fit,shapes.misc}

\usetikzlibrary{decorations.pathreplacing, calc}

\newcommand{\tikzmark}[2][-3pt]{\tikz[remember picture, overlay, baseline=-0.5ex]\node[#1](#2){};}

\tikzset{brace/.style={decorate, decoration={brace}},
brace mirrored/.style={decorate, decoration={brace,mirror}},
}

\newcounter{brace}
\setcounter{brace}{0}
\newcommand{\drawbrace}[3][brace]{%
\refstepcounter{brace}
\tikz[remember picture, overlay]\draw[#1] (#2.center)--(#3.center)node[pos=0.5, name=brace-\thebrace]{};
}

\newcounter{arrow}
\setcounter{arrow}{0}
\newcommand{\drawcurvedarrow}[3][]{%
\refstepcounter{arrow}
\tikz[remember picture, overlay]\draw (#2.center)edge[#1]node[coordinate,pos=0.5, name=arrow-\thearrow]{}(#3.center);
}

\newcommand{\annote}[3][]{%
\tikz[remember picture, overlay]\node[#1] at (#2) {#3};
}

\newcommand\marktopleft[1]{
\tikz[overlay,remember picture]
\node (marker-#1-a) at (0,1.5ex) {};
}
\newcommand\markbottomright[1]{
\tikz[overlay,remember picture]
\node (marker-#1-b) at (0,0) {};
\tikz[overlay,remember picture,thick,dashed,inner sep=3pt]
\node[draw,rounded rectangle,fit=(marker-#1-a.center) (marker-#1-b.center)] {};
}

\usepackage{tabularx}
\usepackage{stmaryrd}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{xcolor}

\makeatletter
\newcommand{\redub}{}
\def\redub#1{%
\@ifnextchar_%
{\@redub{#1}}
{\@latex@warning{Missing argument for \string\redub}\@redub{#1}_{}}%
}
\def\@redub#1_#2{%
\colorlet{currentcolor}{.}%
\color{red}%
\underbrace{\color{currentcolor}#1}_{\color{red}#2}%
\color{currentcolor}%
}

\newcounter{savedenumi}
\newenvironment{moreenum}{%
\setcounter{savedenumi}{\value{enumi}}%
\begin{enumerate}\setcounter{enumi}{\value{savedenumi}}%
}{%
\end{enumerate}%
}
\AtBeginSection[]
{
\begin{frame}<beamer>
\frametitle{}
\tableofcontents[currentsection]
\end{frame}
}

\newcolumntype{g}{>{\columncolor{red}}c}

\begin{document}
\begin{frame}
\begin{tabular}{ | l | l |}
\hline
letter & number \\ \hline
\tikzmark[xshift=-8pt,yshift=1ex]{x}A &  1\tikzmark[xshift=3.5em]{a} \\ \hline
A &  2 \\ \hline
\tikzmark[xshift=-8pt,yshift=-1ex]{y}A &  1\tikzmark[xshift=3.5em]{b} \\ \hline
\tikzmark[xshift=-8pt,yshift=1ex]{w}B &  1 \\ \hline
\tikzmark[xshift=-8pt,yshift=-1ex]{z}B &  2 \\ \hline
\end{tabular}
\drawcurvedarrow[bend left=60,-stealth]{a}{b}
\drawbrace[brace mirrored, thick]{x}{y}
\drawbrace[brace mirrored, thick]{w}{z}
\annote[right]{arrow-1}{Duplicate}
\annote[left]{brace-1}{A}
\annote[left]{brace-2}{B}

\tikzset{
every picture/.style={remember picture,baseline},
every node/.style={
inner sep=0pt,
anchor=base,
minimum width=1.8cm,
align=center,
text depth=.25ex,
outer sep=1.5pt},
every path/.style={
thick,
rounded corners
}
}

\begin{table}
\centering
\begin{tabular}{|c|c|c|g|c|}
\hline
bla   &  bla  & bla   & $b_1$\tikzmark{a} \\ \hline
bla   &  bla  & bla   & bla \\\hline
bla   &  bla  & bla   & bla \\\hline
bla   &  bla  & bla   & bla \\\hline
bla   &  bla  & bla   & bla \\\hline
\end{tabular}
\end{table}
\begin{tikzpicture}[remember picture,overlay]
\node [right=2cm,above=2cm,minimum width=0pt] at (pic cs:a) (A) {A};
\draw [<-,out=5,in=180] ([xshift=15pt]{pic cs:a}) to (A);
\end{tikzpicture}
\end{frame}

\end{document}

• Look for tikzmark command in TeX.SX and will find examples – Ignasi Mar 22 '14 at 7:55
• Have a look to Add a curved arrow and a bracket to a table: it works also for arrows on top of the table (though minor adjustments are needed). – Claudio Fiandrino Mar 22 '14 at 8:07
• Thanks. I tried doing this Add a curved arrow, but as soon as I use \usetikzlibrary{tikzmark} (which I'm using for other stuff) at the same time I get an error. I'm not sure how to get around this. – bozidarka Mar 22 '14 at 15:27

For example, you could do the following if you rewrote your original code to use tikzmark. Note that I eliminated extraneous stuff from your MWE.

\documentclass[pdf]{beamer}
\mode<presentation>{}
\usepackage[english]{babel}
\usepackage{color, colortbl}
\usepackage{tikz}
\usetikzlibrary{tikzmark, positioning, fit, shapes.misc}

\usetikzlibrary{decorations.pathreplacing, calc}

\tikzset{brace/.style={decorate, decoration={brace}},
brace mirrored/.style={decorate, decoration={brace,mirror}},
}

\newcolumntype{g}{>{\columncolor{red}}c}

\begin{document}
\begin{frame}
\begin{tabular}{ | l | l |}
\hline
letter & number \\ \hline
\tikzmark{x}A &  1\tikzmark{a} \\ \hline
A &  2 \\ \hline
\tikzmark{y}A &  1\tikzmark{b} \\ \hline
\tikzmark{w}B &  1 \\ \hline
\tikzmark{z}B &  2 \\ \hline
\end{tabular}
\begin{tikzpicture}[remember picture, overlay]
\draw ($(pic cs:a) + (3.5em,0)$) edge[bend left=60,-stealth] ($(pic cs:b) + (3.5em,0)$) node [midway, right, xshift=5pt, yshift=5pt] {Duplicate};
\draw[brace mirrored, thick] ($(pic cs:x) + (-8pt,1ex)$)--($(pic cs:y) + (-8pt,-1ex)$) node [midway, left] {A};
\draw[brace mirrored, thick] ($(pic cs:w) + (-8pt,1ex)$)--($(pic cs:z) + (-8pt,-1ex)$) node [midway, left] {B};
\end{tikzpicture}

\tikzset{
every picture/.style={remember picture,baseline},
every node/.style={
inner sep=0pt,
anchor=base,
minimum width=1.8cm,
align=center,
text depth=.25ex,
outer sep=1.5pt},
every path/.style={
thick,
rounded corners
}
}

\begin{table}
\centering
\begin{tabular}{|c|c|c|g|c|}
\hline
bla   &  bla  & bla   & $b_1$\tikzmark{c} \\ \hline
bla   &  bla  & bla   & bla \\\hline
bla   &  bla  & bla   & bla \\\hline
bla   &  bla  & bla   & bla \\\hline
bla   &  bla  & bla   & bla \\\hline
\end{tabular}
\end{table}
\begin{tikzpicture}[remember picture,overlay]
\node [right=2cm,above=2cm,minimum width=0pt] at ({pic cs:c}) (A) {A};
\draw [<-,out=5,in=180] ([xshift=15pt]{pic cs:c}) to (A);
\end{tikzpicture}
\end{frame}

\end{document}


This integrates the annotation commands with the drawing commands which saves having to define points for annotation later. It uses calc to adjust the positioning rather than altering the positioning of the original marks. This saves some counters. I found putting everything in a single overlay picture worked better than worrying about making a bunch of commands work together but your kilometres may obviously vary.