12

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?

jmc
  • 1,660
Mike Shaw
  • 235
  • There are many questions that tackles this on this site. You can search for math tikzmark [tikz-pgf] on the top right corner. An example http://tex.stackexchange.com/questions/47610/how-to-draw-arrows-from-cell-to-cell-at-the-borders-of-a-table – percusse Jun 06 '13 at 08:52

2 Answers2

18

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).

5

With {NiceTabular} of nicematrix (and TikZ).

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\begin{center} \begin{NiceTabular}{ll}[hvlines] letter & number \ A & 1 \ A & 2 \ A & 1 \ B & 1 \ B & 2 \ \CodeAfter \tikz \draw [->] (2.5-|last) to [bend left, out= 60, in=120] node [right] { Duplicate } (4.5-|last) ; \SubMatrix{{}{2-1}{4-1}{.}[xshift=0.5em] \end{NiceTabular} \end{center}

\end{document}

You need several compilations (because of the PGF/Tikz nodes).

Output of the above code

F. Pantigny
  • 40,250