# tikzpictures underneath matrices

I'm trying to construct a diagram of how to calculate a 3x3 matrix determinant via the Rule of Sarrus, and I've managed to come this far after lots of research, but I still would like to make the arrows apear underneath the numbers, and the curved arrow to have a circled minus sign in the middle, unfortunately I have yet to find a way of doing this, so if someone can point me in the right direction I would very much appreciate.

This is the code I'm using, and the result bellow.

%preamble
\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{circuitikz}
\usepackage{tikz}
\newcommand\tikznode[1]{\tikz[overlay,remember picture] \node (#1) {};}
\tikzset{>=latex}
------------
%document
\begin{equation*}
\left|\begin{matrix}
\tikznode{11}2& \tikznode{12}7 & \tikznode{13}6\\
\tikznode{21}9& \tikznode{22}5 & \tikznode{23}1\\
\tikznode{31}4& \tikznode{32}3 & \tikznode{33}8
\end{matrix}
\middle|\;\begin{matrix}
\tikznode{14}2& \tikznode{15}7\\
\tikznode{24}9& \tikznode{25}5\\
\tikznode{34}4& \tikznode{35}3
\begin{matrix}
4*5*6+3*1*2+8*9*7 = 630\tikznode{top}\\
\\
\\
\\
2*5*8+7*1*4+6*9*3 = 270\tikznode{btn}
270-630 = -360
\begin{tikzpicture}[overlay, remember picture]
\foreach \i in {1,2,3}
\def\e{\the\numexpr\i+2}
\draw[->,red,line width=1pt] ($(1\i.north east)+(-0.5,0.5)$) -- ($(3\e.south west)+(0.6,-0.2)$);
\foreach \i in {3,4,5}
\def\e{\the\numexpr\i-2}
\draw[<-,blue,line width=1pt] ($(1\i.north west)+(0.7,0.5)$) -- ($(3\e.south east)+(-0.4,-0.2)$);
\path[->,line width=1pt] ($(btn)+(0.25,0)$) edge[bend right=60] ($(top)+(0.25,0)$);
\end{tikzpicture}
\end{equation*}


And the desired result is something along this lines (without the + and - signs ono top and bottom):

• could you please add a sketch where you want the arrows underneath the numbers -- also it would be helpful to add the preamble so that the above code can be compiled Jun 28, 2021 at 4:45
• Thanks for the tips @jsbibra I've added the packages that I suppose are needed, to run this piece of code, and I say that cause when I tried to remove circuitikz it filled the document with errors, even without any circuit diagram on it ¯_(ツ)_/¯. And as you indicated I've also added a picture of the desired goal. Jun 28, 2021 at 5:10
• The umpteenth attempt to illustrate the mostly useless Sarrus' rule… The rule students are fond to apply to larger matrices… Just forget it: if one knows it, good; spending time in teaching it is a waste. Jun 28, 2021 at 13:38

The pstricks way:

\documentclass{article}
\usepackage{pst-node}%

\begin{document}

$\renewcommand{\arraystretch}{1.25} \begin{array}{|ccc|cc|} \Rnode{a1}{2} &\Rnode{b1}{7} & \Rnode{c1}{6} & \Rnode{A1}{2} & \Rnode{B1}{7} \\ \Rnode{a2}{9} & \Rnode{b2}{5} & \Rnode{c2}{1} & \Rnode{A2}{9}& 5\\ \Rnode{a3}{4} & \Rnode{b3}{3} & \Rnode{c3}{8} & \Rnode{A3}{4} & \Rnode{B3}{3} \end{array} \psset{linecolor=red, nodesepA=0.5pt, nodesepB = 0.5pt, arrowinset=0.1} \foreach \s/\t/\u in {a1/b2/c3,b1/c2/A3,c1/A2/B3} {\ncline{-}{\s}{\t}\ncline{->}{\t}{\u}} \psset{linecolor=blue} \foreach \s/\t/\u in {a3/b2/c1,b3/c2/A1,c3/A2/B1} {\ncline{-}{\s}{\t}\ncline{->}{\t}{\u}}$

\end{document}


• What should I do if I want the arrows to start at "$a_{00}$" and end at "$a_{44}$"? i.e. start above the number and end below it. Jun 28, 2021 at 15:51
• @liuzp: Sorry, I had not seen your comment/question. What exactly do you denote $a_{0,0}$ and $a_{44}$? Jul 3, 2021 at 17:45
• The diagonal positions above a_{11} and bellow a_{33}, like if it was a 5x5 matrix 0-indexed. @Bernard Jul 4, 2021 at 18:32
• @liuzp: I'm not sure to interpret correctly what you have in mind. Could you post a hand-made image of what you'd like to have? Jul 4, 2021 at 18:42
• I see. A possibility consists in adding empty nodes in the relevant cells with \pnode[xoffset, yoffset]{node name} and linking each of them to the next (or previous) node. Jul 4, 2021 at 21:49

Here is a solution with {NiceArray} of nicematrix.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}

$\begin{NiceArray}{|ccc|>{\color{gray}}c>{\color{gray}}c} \CodeBefore [create-cell-nodes] \begin{tikzpicture} [shorten < = 2pt,shorten > = 2pt] \draw [red,->] (1-1) -- (3-3) ; \draw [red,->] (1-2) -- (3-4) ; \draw [red,->] (1-3) -- (3-5) ; \draw [blue,->] (3-1) -- (1-3) ; \draw [blue,->] (3-2) -- (1-4) ; \draw [blue,->] (3-3) -- (1-5) ; \node [inner sep=1pt,fill=white,fit = (2-2)] {} ; \node [inner sep=1pt,fill=white,fit = (2-3)] {} ; \node [inner sep=1pt,fill=white,fit = (2-4)] {} ; \end{tikzpicture} \Body a_{11} & a_{12} & a_{13} & a_{11} & a_{12} \\[2mm] a_{21} & a_{22} & a_{23} & a_{21} & a_{22} \\[2mm] a_{31} & a_{32} & a_{33} & a_{31} & a_{32} \\ \end{NiceArray}$

\end{document}


You need several compilations (because of the PGF/Tikz nodes).

the red and blue arrows are essentially a play of cardinal points around each number in the matrix-- each cardinal point of the compass acts as an anchor for the start or end of the arrow -- the idea is to offset each arrow tip by a small amount plus or minus so that the arrow does not collide with the number in the matrix -- for example -- \draw[->,red, line width=1pt] ($(1\i.north east)+(0.1,-0.1)$) -- ($(3\e.north)+(0,0)$); -- means start the arrow from 11.north east which is the top right corner of the number 2 (first row and first column of matrix) -- offset the start point of the arrow 0.1 cm in x direction, and 0.1cm down in y direction -- the end of the arrow is at 33.north -- which is the number 8 (third row and third column of matrix) and offset the arrow tip by (0,0)

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{circuitikz}
\usepackage{tikz}
\newcommand\tikznode[1]{\tikz[overlay,remember picture] \node (#1) {};}
\tikzset{>=latex}
%------------
%document
\begin{document}

\begin{equation*}
\left|\begin{matrix}
\tikznode{11}2& \tikznode{12}7 & \tikznode{13}6\\
\tikznode{21}9& \tikznode{22}5 & \tikznode{23}1\\
\tikznode{31}4& \tikznode{32}3 & \tikznode{33}8
\end{matrix}
\middle|\;\begin{matrix}
\tikznode{14}2& \tikznode{15}7\\
\tikznode{24}9& \tikznode{25}5\\
\tikznode{34}4& \tikznode{35}3
\begin{matrix}
4*5*6+3*1*2+8*9*7 = 630\tikznode{top}\\
\\
\\
\\
2*5*8+7*1*4+6*9*3 = 270\tikznode{btn}
270-630 = -360
\begin{tikzpicture}[overlay, remember picture]
\foreach \i in {1,2,3}
\def\e{\the\numexpr\i+2}
\draw[->,red,line width=1pt] ($(1\i.north east)+(0.1,-0.1)$) -- ($(3\e.north)+(0,0)$);
\foreach \i in {3,4,5}
\def\e{\the\numexpr\i-2}
\draw[<-,blue,line width=1pt] ($(1\i.south)+(0,0.1)$) -- ($(3\e.north east)+(0.1,0.1)$);
\path[->,line width=1pt] ($(btn)+(0.25,0)$) edge[bend right=60]node[fill=white, circle,draw](){-} ($(top)+(0.25,0)$);
\end{tikzpicture}
\end{equation*}
\end{document}

• Your code doesn't make compilation complete. Could you revise? Jun 28, 2021 at 5:46
• ?? compiles fine -- did you add or delete some part Jun 28, 2021 at 9:05
• I can't compile your code. I have an error of division by zero : You asked me to calculate '1/0.0'. Jun 28, 2021 at 9:56
• @F.Pantigny ?? compiles fine -- did you add or delete some part of the code Jun 28, 2021 at 10:23
• Are you sure that your code compiles when you have deleted the aux file? Sometimes, with Tikz, you have a code which compiles, then, after a small modification, it compiles again (using the previous aux file) but, in fact, the code does not compile directly without the aux file). Jun 28, 2021 at 11:01