# How to place an arrow on top and to the right of a matrix

I am trying to create a matrix shown below

but I want the left arrow to be on the right side of the matrix

because I want to have the name of the matrix on the left side.

Here is what I have:

$M =\begin{bmatrix} f(1,1) & f(1,2) & \dots \\ f(2,1) & f(2,2) & \dots \\ \vdots & \vdots & \vdots \end{bmatrix}$


I am not sure how to proceed after that. Anyone has any suggestions?

• Welcome to TeX.SE!! Off-topic: shouldn't it say "j changes" in the horizontal line? Mar 14, 2019 at 20:15
• You are right, it should!
– AL B
Mar 14, 2019 at 20:21

I've slightly modified marmot's solution in order to avoid using tikzmark.

I've also used Triangle as arrow tips and other little changes.

My solution works also if you haven't updated your distribution.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary[arrows.meta]

\begin{document}
M =\tikz[remember picture, baseline=(mat.center)]{\node[inner sep=0](mat){\begin{bmatrix} f(1,1) & f(1,2) & \dots \\ f(2,1) & f(2,2) & \dots \\ f(3,1) & f(3,2) & \dots \\ \vdots & \vdots & \ddots \\ & & \\ \end{bmatrix}};} \begin{tikzpicture}[overlay,remember picture, >=Triangle] \draw[blue,thick,->] node[anchor=south west] (nn1) at (mat.north west) {n columns} (nn1.east) -- (nn1-|mat.north east) node[midway,above,black]{i changes}; \draw[red,thick,->] node[anchor=north west,align=center, inner xsep=0pt] (nn2) at (mat.north east) {m\\ rows} (nn2.south) -- (nn2.south|-mat.south) node[midway,above,black,rotate=-90]{j changes}; \end{tikzpicture}
\end{document}


• @ALB You're welcome! Mar 15, 2019 at 22:18

With tikzmark it is straightforward.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
M =\hspace*{1.5cm}\tikzmarknode{mat}{\begin{bmatrix} f(1,1) & f(1,2) & \dots \\ f(2,1) & f(2,2) & \dots \\ f(3,1) & f(3,2) & \dots \\ \vdots & \vdots & \ddots \\ & & \\ \end{bmatrix}} \begin{tikzpicture}[overlay,remember picture] \draw[blue,thick,-latex] node[anchor=south west] (nn1) at (mat.north west) {n columns} (nn1.east) -- (nn1-|mat.north east) node[midway,above,black]{i changes}; \draw[red,thick,-latex] node[anchor=north east,align=center] (nn2) at (mat.north west) {m\\ rows} (nn2.south) -- (nn2.south|-mat.south west) node[midway,above,black,rotate=90]{j changes}; \end{tikzpicture}
\end{document}


\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
M =\tikzmarknode{mat}{\begin{bmatrix} f(1,1) & f(1,2) & \dots \\ f(2,1) & f(2,2) & \dots \\ f(3,1) & f(3,2) & \dots \\ \vdots & \vdots & \ddots \\ & & \\ \end{bmatrix}} \begin{tikzpicture}[overlay,remember picture] \draw[blue,thick,-latex] node[anchor=south west] (nn1) at (mat.north west) {n columns} (nn1.east) -- (nn1-|mat.north east) node[midway,above,black]{i changes}; \draw[red,thick,-latex] node[anchor=north west,align=center] (nn2) at (mat.north east) {m\\ rows} (nn2.south) -- (nn2.south|-mat.south) node[midway,above,black,rotate=90]{j changes}; \end{tikzpicture}
\end{document}


• Thank you so much, is there any way to put the 'm rows, j changes' and red arrow on the right side of the matrix so it is not between the M and the matrix?
– AL B
Mar 14, 2019 at 20:23
• @ALB Yes. (But if you want the arrow on the right, why do you post a screen shot in which it is on the other side?)
– user121799
Mar 14, 2019 at 20:33
• I posted this screenshot because it was the closest that I could find to what I wanted to draw. I described what I wanted in the question but I should have made it more clear. Thank you for your help.
– AL B
Mar 14, 2019 at 20:38
• Thank you for the edit, this is exactly what I need, unfortunately it does not work for me. When I type the solution I get:'' !Undefined control sequence. M=\tikzmarknode''. Is there another package needed except the ones you have on the top?
– AL B
Mar 14, 2019 at 20:57
• @ALB You haven't updated your TeX installation in a while, right? \tikzmarknode has been added last year. And sorry for being so direct: it is not the purpose of this site to convert screen shots into LaTeX code, nor to provide a hotline for TeX issues. There are two possibilities: either my answer solves your original problem, in which case I'd kindly like to ask to accept it, or it does not, in which case I will be happy to delete it.
– user121799
Mar 14, 2019 at 21:08