[Tex/LaTex] How to highlight elements in a matrix

highlightingmatrices

I'm about to use this diagram to make computing 3×3 determinants clear and easy for my students. I tried to add this picture to my lecture but it's blurred due to the bad quality of the picture. So i want to create exactly the same figure using latex.
I've searched and read a few topics about highlighting elements and diagonals in matrix but it does not work in this case. I want to add plus signs and minus signs as in the picture, and highlight elements in the matrice with elements outside the matrice.
I appreciate all your help and your advice. Thank you in advance !
enter image description here

Best Answer

An alternative "pure" TikZ solution:

\documentclass[border=1mm,tikz]{standalone}
\usetikzlibrary{calc,matrix}

    \begin{document}
\begin{tikzpicture}[
strip/.style args = {#1/#2}{
    draw=#1, very thin, fill=#1, opacity=0.2,
    text width=#2,% will be calculated in path
    rounded corners,% only if you like them ...
    minimum height=1em,% adjust to desired height,
    inner xsep=-1mm,% adjust to desired length of strip
    inner ysep=2mm,
    yshift=-0.6ex,
    sloped},
                    ]
\matrix (mtrx)  [matrix of math nodes,
                 column sep=1em,   
                 nodes={text height=2ex,text width=2ex}]
{
|[red]|+
    & |[red]|+
          & \color{red}+\color{blue}- 
                & |[blue]|- 
                      & |[blue]|-   \\
a_1 & b_1 & c_1 & a_1 & a_2   \\
a_2 & b_2 & c_2 & a_2 & b_2   \\
a_3 & b_3 & c_3 & a_3 & b_3   \\
};
\draw[thick] (mtrx-2-1.north) -| (mtrx-4-1.south -- (mtrx-4-1.south);
\draw[thick] (mtrx-2-3.north) -| (mtrx-4-3.south -- (mtrx-4-3.south);
\path let   \p1 = (mtrx-4-1.center),
            \p2 = (mtrx-2-4.center),
            \n1 = {veclen(\y2-\y1,\x2-\x1)} in
      (mtrx-4-1.center) -- node[strip=blue/\n1] {} (mtrx-2-3.center)
      (mtrx-4-2.center) -- node[strip=blue/\n1] {} (mtrx-2-4.center)
      (mtrx-4-3.center) -- node[strip=blue/\n1] {} (mtrx-2-5.center)
%
      (mtrx-2-1.center) -- node[strip=red/\n1] {} (mtrx-4-3.center)
      (mtrx-2-2.center) -- node[strip=red/\n1] {} (mtrx-4-4.center)
      (mtrx-2-3.center) -- node[strip=red/\n1] {} (mtrx-4-5.center);
\end{tikzpicture}
    \end{document}

enter image description here

If you like to have different colors, you can defined similarly as this do @MyUserlsThis in his/her answer.

Edit: I wasn't aware for this answer, @egreg thank you for point it. Considered it, the solutions is more simple (and beautiful):

\documentclass[border=1mm,tikz]{standalone}
\usetikzlibrary{calc,matrix}

        \begin{document}
\begin{tikzpicture}[
strip/.style = {
    draw=#1,%color
    line width=1em, opacity=0.2,
    line cap=round ,% only if you like them ...
                            },
                    ]
\matrix (mtrx)  [matrix of math nodes,
                 column sep=1em,   
                 nodes={text height=2ex,text width=2ex}]
{
|[red]|+
    & |[red]|+
          & \color{red}+\color{blue}- 
                & |[blue]|- 
                      & |[blue]|-   \\
a_1 & b_1 & c_1 & a_1 & a_2   \\
a_2 & b_2 & c_2 & a_2 & b_2   \\
a_3 & b_3 & c_3 & a_3 & b_3   \\
};
\draw[thick] (mtrx-2-1.north) -| (mtrx-4-1.south west) 
                              -- (mtrx-4-1.south);
\draw[thick] (mtrx-2-3.north) -| (mtrx-4-3.south east)
                              -- (mtrx-4-3.south);
\path[draw,strip=red,shorten <=-1mm]
    (mtrx-4-1.center) edge (mtrx-2-3.center)
    (mtrx-4-2.center) edge (mtrx-2-4.center)
    (mtrx-4-3.center)  --  (mtrx-2-5.center);
\path[draw,strip=blue,shorten >=-1mm]
    (mtrx-2-1.center) edge (mtrx-4-3.center)
    (mtrx-2-2.center) edge (mtrx-4-4.center)
    (mtrx-2-3.center)  --  (mtrx-4-5.center);

\end{tikzpicture}
    \end{document}

which gives:

enter image description here

Edit (2): Regarding to comment of @BarbaraBenton: signs + and - can be moved nearly line width reducing distance between the first and second row. This and also lookout, when strips is rectangular one can obtain with next MWE (where also colors are interchanged):

\documentclass[border=1mm,tikz]{standalone}
\usetikzlibrary{calc,matrix}

        \begin{document}
\begin{tikzpicture}[
strip/.style = {
    draw=#1,%color
    line width=1em, opacity=0.2,
    shorten <=-2mm,shorten >=-2mm,
                            },
                    ]
\matrix (mtrx)  [matrix of math nodes,
                 column sep=1em,
                 nodes={text height=1ex,text width=2ex}]
{
|[red]|+
    & |[red]|+
          & \color{red}+\color{blue}-
                & |[blue]|-
                      & |[blue]|-   \\[3.3mm,between origins]
a_1 & b_1 & c_1 & a_1 & a_2         \\
a_2 & b_2 & c_2 & a_2 & b_2         \\
a_3 & b_3 & c_3 & a_3 & b_3         \\
};
\draw[thick] (mtrx-2-1.north) -| (mtrx-4-1.south west)
                              -- (mtrx-4-1.south);
\draw[thick] (mtrx-2-3.north) -| (mtrx-4-3.south east)
                              -- (mtrx-4-3.south);
\path[draw,strip=blue]
    (mtrx-4-1.center) edge (mtrx-2-3.center)
    (mtrx-4-2.center) edge (mtrx-2-4.center)
    (mtrx-4-3.center)  --  (mtrx-2-5.center);
\path[draw,strip=red]
    (mtrx-2-1.center) edge (mtrx-4-3.center)
    (mtrx-2-2.center) edge (mtrx-4-4.center)
    (mtrx-2-3.center)  --  (mtrx-4-5.center);
\end{tikzpicture}
    \end{document}

enter image description here

Of course, there is still place to make fine adjustment. For example smaller signs + and -, or different positioning, or etc, but something should be left to OP (if he will accept the answer).

Edit (3): For use with document class beamer, here is complete and many time tested MWE (with recent Beamer: version 3.36):

    \documentclass{beamer} 
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}

        \begin{document}
\begin{frame}[fragile]
\begin{tikzpicture}[
strip/.style = {
    draw=#1,%color
    line width=1em, opacity=0.2,
    shorten <=-2mm,shorten >=-2mm,
                            },
                    ]
\matrix (mtrx)  [matrix of math nodes,
                 column sep=1em,
                 nodes={text height=1ex,text width=2ex}]
{
|[red]|+
    & |[red]|+
          & \color{red}+\color{blue}-
                & |[blue]|-
                      & |[blue]|-   \\[3.3mm,between origins]
a_1 & b_1 & c_1 & a_1 & a_2         \\
a_2 & b_2 & c_2 & a_2 & b_2         \\
a_3 & b_3 & c_3 & a_3 & b_3         \\
};
\draw[thick] (mtrx-2-1.north) -| (mtrx-4-1.south west)
                              -- (mtrx-4-1.south);
\draw[thick] (mtrx-2-3.north) -| (mtrx-4-3.south east)
                              -- (mtrx-4-3.south);
\path[draw,strip=blue]
    (mtrx-4-1.center) edge (mtrx-2-3.center)
    (mtrx-4-2.center) edge (mtrx-2-4.center)
    (mtrx-4-3.center)  --  (mtrx-2-5.center);
\path[draw,strip=red]
    (mtrx-2-1.center) edge (mtrx-4-3.center)
    (mtrx-2-2.center) edge (mtrx-4-4.center)
    (mtrx-2-3.center)  --  (mtrx-4-5.center);
\end{tikzpicture}
\end{frame}
    \end{document}