[Tex/LaTex] How to shade or highlight the upper or lower triangular part in a matrix

highlightingmatrices

enter image description here

How can I shade or highlight the lower (or upper) triangular part of a matrix in some color like Q above?

Best Answer

A solution would be:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,fit}
\begin{document}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\begin{tikzpicture}
 \matrix[matrix of math nodes,left delimiter = (,right delimiter = ),row sep=10pt,column sep = 10pt] (m)
 {
 1  &3 &-8\\
 2  &0 &1\\
 -7 &9 &1\\
 };
\begin{pgfonlayer}{background}
 \node[inner sep=3pt,fit=(m-1-1)]          (1)   {};
 \node[inner sep=3pt,fit=(m-1-2) (m-2-3)]  (2)   {};
 \node[inner sep=3pt,fit=(m-3-3)]          (3)   {};
 \draw[rounded corners,dotted,fill=green!50!white,inner sep=3pt,fill opacity=0.1] (1.north west) -- (2.north east) |- (3.south west) |- (2.south west) |- (1.south west) -- cycle;
\end{pgfonlayer}
\end{tikzpicture}
\end{document}

enter image description here