# Arrows in table environment

I want to draw Arrows in my table like below

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{amsthm}
\usepackage{tabularx}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}} % linksbündig mit Breitenangabe
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}
\newcolumntype{N}{@{}m{0pt}@{}}
\usepackage{booktabs}

\begin{document}
\begin{table}\centering
{\def\arraystretch{1}
$\begin{array}{C{2.5cm}|C{1cm}C{1cm}C{1cm}C{1cm}C{1cm}|C{1cm}}\toprule$f_{(X,Y)}(x,y)$&$y=1$&$y=2$&$y=5$&$y=7$&$y=8$&$f_{X}(x)$\\ \midrule$x=1$& 0,08 & 0,02 & 0,04 & 0,12 & 0,06 & \color{red} 0,32\\$x=2$& 0,10 & 0,05 & 0,07 & 0,12 & 0,04 & \color{red} 0,38\\$x=3$& 0,03 & 0,04 & 0,07 & 0,10 & 0,06 & \color{red} 0,3\\\midrule$f_{Y}(y) $& \color{red}0,21&\color{red}0,11 &\color{red}0,18 &\color{red}0,34 & \color{red}0,16 & 1\\\bottomrule \end{array}$}
\end{table}
\end{document}


...

• Quick suggestion, nicematrix.sty have this type of tricks and tikz also, I'm not in system... – MadyYuvi Jul 29 '20 at 4:36

## 2 Answers

\documentclass{article}
\usepackage{array}
\usepackage{tikz}
\usetikzlibrary{tikzmark,arrows,calc}
\usepackage{booktabs}
\definecolor{hotmagenta}{rgb}{1.0, 0.11, 0.81}

\begin{document}
\begin{table}[ht]   % <---
$\begin{array}{c|ccccc|c}\toprule[1pt] f_{(X,Y)}(x,y) & y=1 & y=2 & y=5 & y=7 & y=8 & f_{X}(x) \\ \midrule x=1 & \tikzmarknode{A} 0,08 & 0,02 & 0,04 & 0,12 & 0,06 & \color{hotmagenta} \tikzmarknode{B} 0,32\\ x=2 & 0,10 & 0,05 & 0,07 & 0,12 & 0,04 & \color{hotmagenta} 0,38\\ x=3 & 0,03 & 0,04 & 0,07 & 0,10 & 0,06 & \color{hotmagenta} 0,3\\\midrule f_{Y}(y) & \tikzmarknode{C} 0,21 & \color{hotmagenta} 0,11 & \color{hotmagenta} 0,18 & \color{hotmagenta} 0,34 & \color{hotmagenta} 0,16 & 1\\ \bottomrule[1pt] \end{array} \tikz[remember picture,overlay] {\draw[<-, red, thick, >=stealth] ((A.east)+(14pt,0pt)) -- (B); \draw[<-, blue, thick, >=stealth] ((A.south)+(6pt,0pt)) -- ((C.north)+(6pt,0pt));}$
\end{table}
\end{document}


I have edited this answer because, since version 5.5 (2020-10-20), one must no longer write \omit\CodeAfter here (now, \CodeAfter works in all circonstancies).

With nicematrix:

\documentclass[a4paper,11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{nicematrix}
\usepackage{icomma}
\usepackage{tikz}

\begin{document}
\begin{table}
\centering
$\begin{NiceArray}{wc{2.5cm}|wc{1cm}wc{1cm}wc{1cm}wc{1cm}wc{1cm}|wc{1cm}}\toprule f_{(X,Y)}(x,y) & y=1 & y=2 & y=5 & y=7 & y=8 & f_{X}(x) \\ \midrule x=1 & 0,08 & 0,02 & 0,04 & 0,12 & 0,06 & \color{red} 0,32\\ x=2 & 0,10 & 0,05 & 0,07 & 0,12 & 0,04 & \color{red} 0,38\\ x=3 & 0,03 & 0,04 & 0,07 & 0,10 & 0,06 & \color{red} 0,3\\\midrule f_{Y}(y) & \color{red}0,21&\color{red}0,11 &\color{red}0,18 &\color{red}0,34 & \color{red}0,16 & 1\\ \bottomrule \CodeAfter \begin{tikzpicture} [line width=1pt] \draw[red,->] (2-7.west) -- (2-2.east) ; \draw[blue,->] (5-2.north) -- (2-2.south) ; \end{tikzpicture} \end{NiceArray}$
\end{table}
\end{document}


Remarks:

• With nicematrix, you need several compilations.
• I have used a wc type for the columns which is easier for this type of array.
• I have used icomma to have correct spaces around the commas used as decimal point.