TransWikia.com

How can I highlight elements in a matrix?

TeX - LaTeX Asked by Bungbu on July 22, 2021

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

3 Answers

After playing around with tikz, I could get it, I believe, decently. I'm just a tikz beginner, so I am 100% sure this can be done better, without needing so much fine-tuning of positions. You can adjust all positions and colors though:

documentclass{article}

usepackage{tikz}
usepackage{amsmath}
usepackage{xcolor}
definecolor{myblue}{rgb}{0,0,0.8}
definecolor{myred}{rgb}{0.8,0,0}


begin{document}
begin{tikzpicture}
node[fill=none,draw=none] (matrix)
{
begin{minipage}{textwidth}
[ begin{bmatrix} 
a_1 & b_1 & c_1 
a_2 & b_2 & c_2 
a_3 & b_3 & c_3
end{bmatrix}
begin{matrix}
a_1 & b_1 
a_2 & b_2 
a_3 & b_3
end{matrix} ]
end{minipage}
};
draw[myblue,fill=myblue,opacity=0.2] (-1.4,-0.4)--(0.25,0.6)--(0.4,0.4)--(-1.25,-0.6);
draw[myblue,fill=myblue,opacity=0.2] (-0.7,-0.4)--(0.9,0.6)--(1.05,0.4)--(-0.55,-0.6);
draw[myblue,fill=myblue,opacity=0.2] (0,-0.4)--(1.55,0.6)--(1.7,0.4)--(0.15,-0.6);
draw[myred,fill=myred,opacity=0.2] (-1.4,0.4)--(0.25,-0.6)--(0.4,-0.4)--(-1.25,0.6);
draw[myred,fill=myred,opacity=0.2] (-0.7,0.4)--(0.85,-0.6)--(1.0,-0.4)--(-0.55,0.6);
draw[myred,fill=myred,opacity=0.2] (0,0.4)--(1.55,-0.6)--(1.7,-0.4)--(0.15,0.6);
node[above of=matrix,xshift=-1.3cm,yshift=-0.2cm] {textcolor{myred}{$+$}};
node[above of=matrix,xshift=-0.6cm,yshift=-0.2cm] {textcolor{myred}{$+$}};
node[above of=matrix,xshift=-0cm,yshift=-0.2cm] {textcolor{myred}{$+$}};
node[above of=matrix,xshift=0.4cm,yshift=-0.2cm] {textcolor{myblue}{$-$}};
node[above of=matrix,xshift=1cm,yshift=-0.2cm] {textcolor{myblue}{$-$}};
node[above of=matrix,xshift=1.6cm,yshift=-0.2cm] {textcolor{myblue}{$-$}};
end{tikzpicture}
end{document}

enter image description here

EDIT: In view of the comment by barbara, I've tried to enlarge the space between rows, to make it a little easier to see. I've done it following exactly this: incrementing matrix space. The only problem is that the matrix's bracket is too tall (it ends up being above the plus and - signs), and I don't know how to fix it. I leave the code and result, and if anyone knows how to do so, I'll happily edit this. Or you can just increase the yshift value of the plus and minus signs to put them above the matrix.

documentclass{article}

usepackage{tikz}
usepackage{amsmath}
usepackage{xcolor}
definecolor{myblue}{rgb}{0,0,0.8}
definecolor{myred}{rgb}{0.8,0,0}

begingroup
renewcommand*{arraystretch}{1.5}
% your pmatrix expression
endgroup

makeatletter
renewcommand*env@matrix[1][arraystretch]{%
  edefarraystretch{#1}%
  hskip -arraycolsep
  let@ifnextcharnew@ifnextchar
  array{*c@MaxMatrixCols c}}
makeatother

begin{document}
begin{tikzpicture}
node[fill=none,draw=none] (matrix)
{
begin{minipage}{textwidth}
[ begin{bmatrix}[1.5] 
a_1 & b_1 & c_1 
a_2 & b_2 & c_2 
a_3 & b_3 & c_3
end{bmatrix}
begin{matrix}[1.5]
a_1 & b_1 
a_2 & b_2 
a_3 & b_3
end{matrix} ]
end{minipage}
};
draw[myblue,fill=myblue,opacity=0.2] (-1.4,-0.7)--(0.2,0.75)--(0.35,0.55)--(-1.25,-0.9);
draw[myblue,fill=myblue,opacity=0.2] (-0.7,-0.7)--(0.8,0.75)--(0.95,0.55)--(-0.55,-0.9);
draw[myblue,fill=myblue,opacity=0.2] (0,-0.7)--(1.45,0.75)--(1.6,0.55)--(0.15,-0.9);
draw[myred,fill=myred,opacity=0.2] (-1.4,0.55)--(0.25,-0.9)--(0.4,-0.7)--(-1.25,0.75);
draw[myred,fill=myred,opacity=0.2] (-0.7,0.55)--(0.85,-0.9)--(1.0,-0.7)--(-0.55,0.75);
draw[myred,fill=myred,opacity=0.2] (0,0.55)--(1.55,-0.9)--(1.7,-0.7)--(0.15,0.75);
node[above of=matrix,xshift=-1.3cm,yshift=-0.1cm] {textcolor{myred}{$+$}};
node[above of=matrix,xshift=-0.6cm,yshift=-0.1cm] {textcolor{myred}{$+$}};
node[above of=matrix,xshift=-0cm,yshift=-0.1cm] {textcolor{myred}{$+$}};
node[above of=matrix,xshift=0.4cm,yshift=-0.1cm] {textcolor{myblue}{$-$}};
node[above of=matrix,xshift=1cm,yshift=-0.1cm] {textcolor{myblue}{$-$}};
node[above of=matrix,xshift=1.6cm,yshift=-0.1cm] {textcolor{myblue}{$-$}};
end{tikzpicture}
end{document}

enter image description here

Answered by MyUserIsThis on July 22, 2021

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}

Answered by Zarko on July 22, 2021

With {NiceMatrix} of nicematrix (≥ 5.16 2021-06-20) and Tikz to draw the rules.

documentclass{article}
usepackage{nicematrix}
usepackage{tikz}

begin{document}

$begin{NiceMatrix}[columns-width=7mm]
CodeBefore [create-cell-nodes]
    begin{tikzpicture} 
      [line width=3.5mm, opacity=0.2, shorten < = -3mm, shorten > = -3mm]
      draw [blue] (4-1.center) -- (2-3.center) ;
      draw [blue] (4-2.center) -- (2-4.center) ;
      draw [blue] (4-3.center) -- (2-5.center) ;
      draw [red] (2-1.center) -- (4-3.center) ;
      draw [red] (2-2.center) -- (4-4.center) ;
      draw [red] (2-3.center) -- (4-5.center) ;
    end{tikzpicture}
Body
    color{red} +   & color{red} +   & color{red}+color{blue}-  & color{blue}-   & color{blue}- 
    a_1 & b_1 & c_1 & a_1 & a_2   [2mm]
    a_2 & b_2 & c_2 & a_2 & b_2   [2mm]
    a_3 & b_3 & c_3 & a_3 & b_3   
CodeAfter
    SubMatrix[{2-1}{4-3}]
end{NiceMatrix}$

end{document}

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

Output of the above code

Answered by F. Pantigny on July 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP