Matrix product in Latex

arraysmatrices

I would be immensely happy if I could draw this neat (even if absolutely unoriginal) way to present the matrix product in LaTeX :

Neato matrix product

If possible, I would like a code quite simple, that doesn't use tikz, and rather rely on the array environment.

Is there any way to do so?

Best Answer

A first approach with nicematrix.

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

\begin{document}

$\begin{NiceArray}
  [xdots/line-style={solid,<->},cell-space-limits=10pt]
  {ccccccw{c}{5mm}}
& \hspace*{10mm}  & \hspace*{10mm}  & & \Hdotsfor[shorten=-\arraycolsep]{1}^r & \Hdotsfor[shorten=-\arraycolsep]{1}^{n-r}\\
&   &   & & A'      & B' & \Vdotsfor[shorten=0pt]{1}^{q}\\
&   &   & & C'      & D' & \Vdotsfor[shorten=0pt]{1}^{n-q}\\
& \Hdotsfor[shorten=-\arraycolsep]{1}^q & \Hdotsfor[shorten=-\arraycolsep]{1}^{n-q}\\
& A & B & & AA'+BC' & AB'+BD' & \Vdotsfor[shorten=0pt]{1}^{p}\\
& C & D & & CA'+DC' & CB'+DD' & \Vdotsfor[shorten=0pt]{1}^{n-p}\\
&   &   & & \Hdotsfor[shorten=-\arraycolsep]{1}^{r}  & \Hdotsfor[shorten=-\arraycolsep]{1}^{n-r} \\
\CodeAfter
  \SubMatrix({2-5}{3-6})[hvlines]
  \SubMatrix({5-5}{6-6})[hvlines]
  \SubMatrix({5-2}{6-3})[hvlines]
\end{NiceArray}$

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code