# Matrices and TikZ : arrows inside the matrix

In the example below, the arrows fit inside the matrix but I would like them to stop at the limit of the delimiters.

    \documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing, calc, positioning,fit}
\begin{document}

\begin{tikzpicture}[>=stealth,thick,baseline]
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)](A){
a_{1,1} & a_{1,2} & \dots  & a_{1,j} & \dots & a_{1,n}\\
a_{2,1} & a_{2,2} & \dots  & a_{2,j} & \dots & a_{2,n}\\
\vdots  & \vdots  &  & \vdots  &  & \vdots\\
a_{i,1} & a_{i,2} & \dots  & a_{i,j} & \dots & a_{i,n}\\
\vdots  & \vdots  &  & \vdots  &  & \vdots\\
a_{n,1} & a_{n,2} & \dots  & a_{n,j} & \dots & a_{n,n}\\
};

\node[
fit=(A-4-6)(A-4-6),
inner xsep=20pt,inner ysep=0,
label=right: $i$-ième ligne
](L) {};

\node[
fit=(A-6-4)(A-6-4),
inner xsep=20pt,inner ysep=20pt,
label=below: $j$-ième colonne
](C) {};

\draw[->](L.east)-- (A-4-6);
\draw[->](C.south)-- (A-6-4);

\end{tikzpicture}

\end{document}


Add an xshift:

 \documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing, calc, positioning,fit}
\begin{document}

\begin{tikzpicture}[>=stealth,thick,baseline]
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)](A){
a_{1,1} & a_{1,2} & \dots  & a_{1,j} & \dots & a_{1,n}\\
a_{2,1} & a_{2,2} & \dots  & a_{2,j} & \dots & a_{2,n}\\
\vdots  & \vdots  &  & \vdots  &  & \vdots\\
a_{i,1} & a_{i,2} & \dots  & a_{i,j} & \dots & a_{i,n}\\
\vdots  & \vdots  &  & \vdots  &  & \vdots\\
a_{n,1} & a_{n,2} & \dots  & a_{n,j} & \dots & a_{n,n}\\
};

\node[
fit=(A-4-6)(A-4-6),
inner xsep=20pt,inner ysep=0,
label=right: $i$-ième ligne
](L) {};

\node[
fit=(A-6-4)(A-6-4),
inner xsep=20pt,inner ysep=20pt,
label=below: $j$-ième colonne
](C) {};

\draw[->](L.east)-- ([xshift=12pt]A-4-6.east);
\draw[->](C.south)-- (A-6-4);

\end{tikzpicture}

\end{document}


Or shorten the arrow:

 \documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing, calc, positioning,fit}
\begin{document}

\begin{tikzpicture}[>=stealth,thick,baseline]
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)](A){
a_{1,1} & a_{1,2} & \dots  & a_{1,j} & \dots & a_{1,n}\\
a_{2,1} & a_{2,2} & \dots  & a_{2,j} & \dots & a_{2,n}\\
\vdots  & \vdots  &  & \vdots  &  & \vdots\\
a_{i,1} & a_{i,2} & \dots  & a_{i,j} & \dots & a_{i,n}\\
\vdots  & \vdots  &  & \vdots  &  & \vdots\\
a_{n,1} & a_{n,2} & \dots  & a_{n,j} & \dots & a_{n,n}\\
};

\node[
fit=(A-4-6)(A-4-6),
inner xsep=20pt,inner ysep=0,
label=right: $i$-ième ligne
](L) {};

\node[
fit=(A-6-4)(A-6-4),
inner xsep=20pt,inner ysep=20pt,
label=below: $j$-ième colonne
](C) {};

\draw[->, shorten > =12pt](L.east)-- (A-4-6.east);
\draw[->](C.south)-- (A-6-4);

\end{tikzpicture}

\end{document}


And by the way why do you need fit?

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning}

\begin{document}

\begin{tikzpicture}[>=stealth,thick,baseline]
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)](A){
a_{1,1} & a_{1,2} & \dots  & a_{1,j} & \dots & a_{1,n}\\
a_{2,1} & a_{2,2} & \dots  & a_{2,j} & \dots & a_{2,n}\\
\vdots  & \vdots  &  & \vdots  &  & \vdots\\
a_{i,1} & a_{i,2} & \dots  & a_{i,j} & \dots & a_{i,n}\\
\vdots  & \vdots  &  & \vdots  &  & \vdots\\
a_{n,1} & a_{n,2} & \dots  & a_{n,j} & \dots & a_{n,n}\\
};

\node[right =30pt of A-4-6.east](L)  {$i$-ième ligne};

\node[below=20pt of A-6-4.south](C) {$j$-ième colonne};

\draw[->, shorten > =12pt](L.west)-- (A-4-6.east);
\draw[->](C.north)-- (A-6-4.south);

\end{tikzpicture}

\end{document}


• @Fabrice You're welcome and thank you for accepting my answer! – CarLaTeX Apr 17 '19 at 10:31
• @Fabrice However, Ignasi's answer is more elegant. – CarLaTeX Apr 17 '19 at 10:34

Matrix delimiters are nodes but unnamed by default. Therefore, if you add a name you can use them as reference like any other node and lines will stop on their borders.

In following code, I've used every right delimiter style to add a name which is used later on.

A matrix is an special kind of node which contains other nodes. So if you want to stop a line on matrix border you can use the matrix name as reference. Matrix elements have their own border. I've used intersection points between column center and south matrix border to place the vertical arrow and row center and east anchor on right delimiter to place the horizontal arrow. If you need to understand -| (or |-) syntax, look at https://tex.stackexchange.com/a/481234/1952 or https://tex.stackexchange.com/a/22954/1952.

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing, calc, positioning,fit}
\begin{document}

\begin{tikzpicture}[>=stealth,thick,baseline,
every right delimiter/.append style={name=rd},
]
\matrix [matrix of math nodes,
left delimiter=(,
right delimiter=),
](A){
a_{1,1} & a_{1,2} & \dots  & a_{1,j} & \dots & a_{1,n}\\
a_{2,1} & a_{2,2} & \dots  & a_{2,j} & \dots & a_{2,n}\\
\vdots  & \vdots  &  & \vdots  &  & \vdots\\
a_{i,1} & a_{i,2} & \dots  & a_{i,j} & \dots & a_{i,n}\\
\vdots  & \vdots  &  & \vdots  &  & \vdots\\
a_{n,1} & a_{n,2} & \dots  & a_{n,j} & \dots & a_{n,n}\\
};

\draw[<-] (rd.east|-A-4-1.center)--++(0:6mm) node[right]{$i$-ième colonne};
\draw[<-] (A.south-|A-1-4.center) --++(-90:6mm) node[below]{$j$-ième colonne};

\end{tikzpicture}

\end{document}


• Awesome! It didn't come to my mind to name the node of the right delimeter, +1! – CarLaTeX Apr 17 '19 at 10:33
• But the horizontal arrow should be aligned with the ith row... – CarLaTeX Apr 17 '19 at 12:42
• @CarLaTeX you're right. I'll change as soon as I've a computer and Internet access at hand ;-) – Ignasi Apr 17 '19 at 17:51
• Thank you, because your answer is really elegant! – CarLaTeX Apr 17 '19 at 18:12
• @ Ignasi Could you explain this (A.south-|A-1-4.center) ? Thank you. – Fabrice Apr 18 '19 at 9:06