Draw a matrix/array with arrows between rows and between elements

matricestikz-arrowstikz-pgf

How to draw a matrix/array with arrows between rows and between elements?

enter image description here

Best Answer

You may use the tikzmark library to draw around your matrix. Needs to be compiled twice.

how to draw arrows around matrix

\documentclass{article}
\usepackage{amsmath, tikz}
\usetikzlibrary{tikzmark}

\begin{document}
    \[
    \begin{matrix}
        1 & \tikzmarknode{A}{2} & 3 & 4 & \tikzmarknode{C}{5} \\
        \tikzmarknode{B}{1} & 2 & 3 & 4 & \tikzmarknode{D}{5} \\
        1 & 2 & 3 & 4 & \tikzmarknode{E}{5} \\
        1 & 2 & 3 & 4 & \tikzmarknode{F}{5} 
    \end{matrix}
    \]
    
    \begin{tikzpicture}[remember picture, overlay]
        \draw[->] (A) -- (B);
        
        \draw[->] (C.-10) to[out=-20, in=20, looseness=2] (D.10);
        \draw[->] (C.-10) to[out=-20, in=180, looseness=1.5] ++ (0.5,-0.25) node[right] {$1$};
        
        \draw[->] (D.-10) to[out=-20, in=20, looseness=2] (E.10);
        \draw[->] (D.-10) to[out=-20, in=180, looseness=1.5] ++ (0.5,-0.25) node[right] {$1$};
        
        \draw[->] (E.-10) to[out=-20, in=20, looseness=2] (F.10);
        \draw[->] (E.-10) to[out=-20, in=180, looseness=1.5] ++ (0.5,-0.25) node[right] {$1$};
    \end{tikzpicture}
\end{document}