3

I'm trying to ilustrate how matrix multiplication works with the following code:

\begin{align*}
&  \begin{pmatrix}
     1 &2 \\
    -1 &0 
   \end{pmatrix} \\
   \begin{pmatrix}
    3 & 2 \\
    1 &-2 
\end{pmatrix} & \begin{pmatrix}
    ? & ? \\ ? & ?
\end{pmatrix}
\end{align*}

With the following result:

I'd like to add some highlighting boxes like this:

I've seen some ways of doing boxes like these, but they don't seem to work properly within matrices (the & symbol for separating columns doesn't do its job when put within the boxes). Is there a way to do these that plays nicely with matrices? Also, is there a way to nicely align the top and bottom right matrices? Because right now they're aligned at the parenthesis, and it looks a bit odd.

1

1 Answer 1

7

You might want to make the whole expression as one array using nicematrix and add the parentheses using \SubMatrix. The highlighting boxes can be drawn with tikz using the fit library.

Note that \SubMatrix doesn't add space for the parentheses, so some additional space needs to be added between columns 2 and 3.

enter image description here

\documentclass{article}

\usepackage{nicematrix, tikz}
\usetikzlibrary{fit}

\begin{document}

\[
\begin{NiceArray}{cc@{\hspace{1.5em}}cc}
& & 1 & 2\\
& & -1 & 0\\[1ex]
3 & 2 & ? & ?\\
1 & -2 & ? & ?
\CodeAfter
    \SubMatrix({3-3}{4-4})
    \SubMatrix({1-3}{2-4})
    \SubMatrix({3-1}{4-2})
    \tikz{
        \node [draw, red, fit = (3-1)(3-2), inner sep=1pt]{};
        \node [draw, red, fit = (1-3)(2-3), inner sep=1pt]{};
        \node [draw, red, fit = (3-3), inner sep=1pt]{};
    }
\end{NiceArray}
\]

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .