4

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

enter image description here

4 Answers 4

8

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}
5

This is a job for a TikZ CD (the arrow bending is copied from the accepted answer).

You could also customize your arrows using the options of the TikZ library arrows.meta.

\documentclass{article}
\usepackage{amsmath, tikz-cd}
\usetikzlibrary{arrows.meta}

\begin{document}
    \[
    \begin{tikzcd}[row sep=4pt]
        1 & 2\ar[ddl, -{Straight Barb[scale length=3]}] & 3 & 4 & 5\ar[dd,out=-20, in=20, looseness=2, -Triangle]\ar[dr,out=-20, in=180, looseness=1.5, -Triangle]&[8pt] \\
        &&&&&1\\
        1 & 2 & 3 & 4 & 5\ar[dd,out=-20, in=20, looseness=2, -Triangle]\ar[dr,out=-20, in=180, looseness=1.5, -Triangle] \\
        &&&&&2\\
        1 & 2 & 3 & 4 & 5\ar[dd,out=-20, in=20, looseness=2, -Triangle]\ar[dr,out=-20, in=180, looseness=1.5, -Triangle] \\
        &&&&&1\\
        1 & 2 & 3 & 4 & 5 
    \end{tikzcd}
    \]
\end{document}

enter image description here

3

An example done with Mathcha...

enter image description here

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}

     \tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt        

\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
%uncomment if require: \path (0,300); %set diagram left start at 0, and has height of 300

%Straight Lines [id:da0488046216530591] 
\draw    (252.8,103.2) -- (232.41,123.59) ;
\draw [shift={(231,125)}, rotate = 315] [color={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.75]    (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29)   ;
%Curve Lines [id:da6654036500183049] 
\draw    (365,84) .. controls (403.01,87.63) and (395.6,117.89) .. (375.69,123.22) ;
\draw [shift={(372.8,123.8)}, rotate = 352.23] [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.08]  [draw opacity=0] (8.93,-4.29) -- (0,0) -- (8.93,4.29) -- cycle    ;
%Curve Lines [id:da8494199371753657] 
\draw    (385,90) .. controls (404.03,108.12) and (405.65,106.31) .. (410.38,114.3) ;
\draw [shift={(411.8,116.8)}, rotate = 241.39] [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.08]  [draw opacity=0] (8.93,-4.29) -- (0,0) -- (8.93,4.29) -- cycle    ;

%Curve Lines [id:da7784033864639219] 
\draw    (367,131) .. controls (405.01,134.63) and (397.6,164.89) .. (377.69,170.22) ;
\draw [shift={(374.8,170.8)}, rotate = 352.23] [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.08]  [draw opacity=0] (8.93,-4.29) -- (0,0) -- (8.93,4.29) -- cycle    ;
%Curve Lines [id:da8941721970026415] 
\draw    (387,137) .. controls (406.03,155.12) and (407.65,153.31) .. (412.38,161.3) ;
\draw [shift={(413.8,163.8)}, rotate = 241.39] [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.08]  [draw opacity=0] (8.93,-4.29) -- (0,0) -- (8.93,4.29) -- cycle    ;

%Curve Lines [id:da02149247938872656] 
\draw    (368,180) .. controls (406.01,183.63) and (398.6,213.89) .. (378.69,219.22) ;
\draw [shift={(375.8,219.8)}, rotate = 352.23] [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.08]  [draw opacity=0] (8.93,-4.29) -- (0,0) -- (8.93,4.29) -- cycle    ;
%Curve Lines [id:da5470003754531052] 
\draw    (388,186) .. controls (407.03,204.12) and (408.65,202.31) .. (413.38,210.3) ;
\draw [shift={(414.8,212.8)}, rotate = 241.39] [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.08]  [draw opacity=0] (8.93,-4.29) -- (0,0) -- (8.93,4.29) -- cycle    ;


% Text Node
\draw (294.75,150.4) node    {$\begin{matrix}
1 &  & 2 &  & 3 &  & 4 &  & 5\\
 &  &  &  &  &  &  &  & \\
2 &  & 2 &  & 3 &  & 4 &  & 5\\
 &  &  &  &  &  &  &  & \\
1 &  & 3 &  & 3 &  & 4 &  & 5\\
 &  &  &  &  &  &  &  & \\
3 &  & 2 &  & 3 &  & 4 &  & 5
\end{matrix}$};
% Text Node
\draw (417,110.4) node [anchor=north west][inner sep=0.75pt]    {$1$};
% Text Node
\draw (416,155.4) node [anchor=north west][inner sep=0.75pt]    {$2$};
% Text Node
\draw (415,207.4) node [anchor=north west][inner sep=0.75pt]    {$1$};


\end{tikzpicture}




\end{document}
2
  • thanks. I also tried mathcha but failed....
    – Lluvio Liu
    Mar 16, 2022 at 20:45
  • @LluvioLiu I wouldn't know what your problem is with mathcha but as you can see it works.
    – Sebastiano
    Mar 16, 2022 at 20:55
0

It's possible to use {NiceMatrix} of nicematrix and Tikz in the \CodeAfter to draw the arrows.

The arrows are those of the SebGlav's answer.

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

\begin{document}
\[
\begin{NiceMatrix}
    1 & 2 & 3 & 4 & 5 \\
    1 & 2 & 3 & 4 & 5 \\
    1 & 2 & 3 & 4 & 5 \\
    1 & 2 & 3 & 4 & 5
\CodeAfter
  \begin{tikzpicture}[remember picture, overlay,->]
      \draw (1-2) -- (2-1);
      \draw (1-5.-10) to[out=-20, in=20, looseness=2] (2-5.10);
      \draw (1-5.-10) to[out=-20, in=180, looseness=1.5] ++ (0.5,-0.25) node[right] {$1$};
      \draw (2-5.-10) to[out=-20, in=20, looseness=2] (3-5.10);
      \draw (2-5.-10) to[out=-20, in=180, looseness=1.5] ++ (0.5,-0.25) node[right] {$1$};
      \draw (3-5.-10) to[out=-20, in=20, looseness=2] (4-5.10);
      \draw (3-5.-10) to[out=-20, in=180, looseness=1.5] ++ (0.5,-0.25) node[right] {$1$};
  \end{tikzpicture}
\end{NiceMatrix}
\]
\end{document}

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .