# How do I draw arrows between vectors elements in an equation

I want draw arrows between two column vectors like I have shown here.

To make this image I rendered the two matrices in LaTeX like so:

$\vec{P}=\begin{bmatrix} A_1\\ A_2\\ A_1\\ A_3\\ \cdot\\ \cdot\\ \cdot\\ A_6\\ A_5 \end{bmatrix} \qquad{\longrightarrow}\qquad \begin{bmatrix} A_1\\ A_1\\ A_1\\ A_2\\ \cdot\\ \cdot\\ \cdot\\ A_5\\ A_3 \end{bmatrix}=\vec{P}'$


And then I exported the PNG to a drawing app and added the arrows going between entries. Is there some way I can do this directly in LaTeX to make it look nice?

• Welcome to TeX.SE! Commented Mar 30, 2023 at 17:11

An alternative to F. Pantigny's solution, also using the nicematrix package, but with only one single matrix that contains everything (also to showcase the several amazing abilities of this package):

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

\begin{document}
$\begin{NiceArray}{ c w{c}{4em} c }[first-col, last-col] \Block{10-1}{\vec{P} =} & A_1 & \Block{10-1}{\longrightarrow} & A_1 & \Block{10-1}{= \vec{P}'} \\ & A_2 & & A_3 & \\ & A_1 & & A_1 & \\ & A_1 & & A_1 & \\ & A_2 & & A_2 & \\ & A_3 & & A_1 & \\ & \Vdots & & \Vdots & \\ & & & & \\ & A_6 & & A_2 & \\ & A_5 & & A_5 & \\ \CodeAfter \SubMatrix[{1-1}{10-1}] \SubMatrix[{1-3}{10-3}] \begin{tikzpicture} \foreach \a/\b in {1/1, 3/3, 3/4, 3/6, 5/5, 5/9, 6/2, 10/10} { \draw[-stealth] ([xshift=5pt]\a-1.east) -- ([xshift=-5pt]\b-3.west); } \end{tikzpicture} \end{NiceArray}$
\end{document}


With {bNiceMatrix} of nicematrix and TikZ to draw the arrows.

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

\begin{document}

$\vec{P}=\begin{bNiceMatrix}[name=A] A_1\\ A_2\\ A_1\\ A_3\\ \cdot\\ \cdot\\ \cdot\\ A_6\\ A_5 \end{bNiceMatrix} \qquad{\longrightarrow}\qquad \begin{bNiceMatrix}[name=B] A_1\\ A_1\\ A_1\\ A_2\\ \cdot\\ \cdot\\ \cdot\\ A_5\\ A_3 \end{bNiceMatrix}=\vec{P}' \begin{tikzpicture}[remember picture,overlay,->,shorten > = 2mm, shorten < = 2mm] \draw (A-1-1) -- (B-1-1) ; \draw (A-2-1) -- (B-3-1) ; \draw (A-4-1) -- (B-4-1) ; \end{tikzpicture}$

\end{document}


• Is there any way we could do this with SubMatrix? Commented Mar 30, 2023 at 18:38
• I see Jasper has already done this. Commented Mar 30, 2023 at 18:41

Here's a solution based on tikz and tikzmark library. Once you have created anchors with \tikzmark{<label>}, then in tikz you can draw arrows between specified anchors.

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

\begin{document}
$\vec{P}=\begin{bmatrix} A_1 \tikzmark{11} \\ A_2 \tikzmark{12} \\ A_1 \\ A_3 \tikzmark{14} \\ \cdot \\ \cdot \\ \cdot \\ A_6 \\ A_5 \end{bmatrix} \qquad{\longrightarrow}\qquad \begin{bmatrix} \tikzmark{21}A_1 \\ A_1 \\ \tikzmark{23}A_1 \\ \tikzmark{24}A_2 \\ \cdot \\ \cdot \\ \cdot \\ A_5 \\ A_3 \end{bmatrix}=\vec{P}'$
\tikz[
remember picture,
overlay,
left/.style = {xshift=2mm, yshift=1mm},
right/.style= {xshift=-2mm, yshift=1mm},
]{
\path[-latex]
([left]pic cs:11) edge ([right]pic cs:21)
([left]pic cs:12) edge ([right]pic cs:23)
([left]pic cs:14) edge ([right]pic cs:24);
}
\end{document}


• Okay this looks like should work. TYSM!
– A-P
Commented Mar 30, 2023 at 18:20