4

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

3 Answers 3

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, 2015 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, 2015 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, 2015 at 14:18
1

With {pNiceMatrix} of nicematrix and TikZ.

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

$Q = 
\begin{pNiceMatrix}[margin]
\CodeBefore
  \tikz \fill [blue!10,rounded corners] (1-|1) |- (2-|2) |- (3-|3) |- (4-|4) -- (1-|4) -- cycle  ; 
\Body
  q_{11} & q_{12} & q_{13} \\
  q_{21} & q_{22} & q_{23} \\
  q_{31} & q_{32} & q_{33} \\
\end{pNiceMatrix}$

\end{document} 

You need several compilations.

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .