[Tex/LaTex] Add a curved arrow and a bracket to a table

tables

I'm a beginner to LaTeX. I want to add a curved arrow pointing from one row to another. And I also want to add a { on the other side of the table to mark several rows.

\begin{center}
 \begin{tabular}{ | l | l |}
  \hline
   letter & number \\ \hline
   A &  1 \\ \hline
   A &  2 \\ \hline
   A &  1 \\ \hline
   B &  1 \\ \hline
   B &  2 \\ \hline
 \end{tabular}
\end{center}

So my idea is to add a curved arrow outside the table to mark the first line and the third line as "duplicated" on the right and add two { to mark the first 3 rows as "A" and the last 2 rows as "B" on the left.

Any suggestion on how can I do it?

Best Answer

As percusse said in his comment, the most convenient way to go is to exploit the tikzmark macro (just one reference: Adding a large brace next to a body of text).

This solution allows to draw both braces and arrows through:

  1. \drawbrace command;
  2. \drawcurvedarrow command,

as well as to put some annotations via \annote and it exploits a variant of tikzmark macro in which it is possible to customize a bit the position of the marker via shifts.

The code:

\documentclass[border=20pt,png]{standalone}
\usepackage{tikz}
\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);
}

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

\begin{document}

 \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}
\end{document}

The result:

enter image description here

Remember to compile at least twice to get the result and to put annotations refer to the order in which braces or arrows have been deployed to identify their position (i.e. second brace: brace-2, fourth arrow: arrow-4).

Related Question