I know that there are several posts about colouring matrices rows and columns, but I was trying to create a diagram to show matrix multiplication, and ended up with the following code, which is a bit empirical and a bit clumsy. I was wondering if there wasn't a more intelligent way to do the same thing.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}
\begin{document}
\begin{tikzpicture}[
every node/.style={minimum height=14pt,minimum width=1em},
every matrix/.style={matrix of math nodes,
column sep=1mm,
row sep=1mm,
left delimiter=(,
right delimiter=)}]
\fill[red!10] (-1,-6pt) rectangle (1,6pt);
\fill[red!10] (4.8,0) ++(-7pt,-1) rectangle +(13pt,2);
\fill[red!10] (10.3,0) ++(-7pt,-6pt) rectangle +(13pt,15pt);
\matrix (A) at (0,0) {
\phantom{\bullet} & & \
\bullet & \cdots & \bullet \
\phantom{\bullet} & & \
};
\matrix (B) at (4.5,0) {
\phantom{\bullet} & \phantom{\bullet} & \bullet & \phantom{} \
& & |[rotate=90]| \dots & \
& & \bullet & \
};
\matrix (AB) at (10,0) {
\phantom{\bullet} & \phantom{\bullet} & \phantom{\bullet} & \phantom{\bullet} \
\phantom{\bullet} & \phantom{\bullet} & \bullet & \phantom{} \
& & & \
\phantom{\bullet} & & & \
};
\node[left=3mm] at (A.west) {\tikz\draw[semithick,<->] (0,0) -- (0,2) node[midway,anchor=center,fill=white] {$m$};};
\node[above=-1mm] at (A.north) {\tikz\draw[semithick,<->] (0,0) -- (2,0) node[midway,anchor=center,fill=white] {$p$};};
\node[left=3mm] at (B.west) {\tikz\draw[semithick,<->] (0,0) -- (0,2) node[midway,anchor=center,fill=white] {$p$};};
\node[above=-1mm] at (B.north) {\tikz\draw[semithick,<->] (0,0) -- (2.5,0) node[midway,anchor=center,fill=white] {$n$};};
\node[left=3mm] at (AB.west) {\tikz\draw[semithick,<->] (0,0) -- (0,2) node[midway,anchor=center,fill=white] {$m$};};
\node[above=-1mm] at (AB.north) {\tikz\draw[semithick,<->] (0,0) -- (2.5,0) node[midway,anchor=center,fill=white] {$n$};};
\node at ($(A)!0.45!(B)$) {$\times$};
\node at ($(B)!0.45!(AB)$) {$=$};
\node[below] (a) at (A.south) {row $i$};
\node[below] (b) at (B.south) {column $j$};
\node[below] (ab) at (AB.south) {$(i,j)$-cell};
\node at ($(a)!0.45!(b)$) {$\times$};
\node at ($(b)!0.45!(ab)$) {$=$};
\end{tikzpicture}
\end{document}
Thank you for any help to make this a bit better.



\phantom{\bullet},\tikz\draw[semithick,<->],[midway,anchor=center,fill=white]) to begin with, but this way is fairly acceptable, in my opinion. – SebGlav Apr 13 '21 at 17:53