3

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?

2
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}

enter image description here

2
  • This is great. Thanks a lot! Just curious: is there a way to replace those zigzags along the diagonals with a straight line?
    – bluepole
    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
1

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}
1
  • Thanks for the suggestion! I would like to highlight the whole triangular part, not just the individual elements.
    – bluepole
    Dec 18 '15 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.