# How to shade or highlight the upper or lower triangular part in a matrix?

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

• If you produced that code in Latex with a matrix, please post the code. :) Also, what do you mean by "triangle"? Dec 17 '15 at 23:04
• Did you have a look at tex.stackexchange.com/a/40041/69445? Dec 17 '15 at 23:11

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}


• This is great. Thanks a lot! Just curious: is there a way to replace those zigzags along the diagonals with a straight line? Dec 18 '15 at 14:16
• Yes it is possible with the path (1.north west) -- (2.north east) |- (3.south west) -- (1.south west) -- cycle, but I don't think it will not look good without further tinkering.
– Bibi
Dec 18 '15 at 14:23

You can use the color or xcolor package, and print in an alternative color some entries. For example,

\begin{pmatrix}
q_{11} &  q_{12} &  q_{13} \\
\color{gray} q_{21} &  q_{22} &  q_{23} \\
\color{gray} q_{31} & \color{gray}  q_{32} &  q_{33}
\end{pmatrix}

• Thanks for the suggestion! I would like to highlight the whole triangular part, not just the individual elements. Dec 18 '15 at 14:18