How can I shade or highlight the lower (or upper) triangular part of a matrix in some color like Q above?
Asked
Active
Viewed 2,367 times
4
-
2If you produced that code in Latex with a matrix, please post the code. :) Also, what do you mean by "triangle"? – Alenanno Dec 17 '15 at 23:04
-
1Did you have a look at http://tex.stackexchange.com/a/40041/69445? – cryingshadow Dec 17 '15 at 23:11
3 Answers
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}
Bibi
- 1,532
-
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}
Robert Fuster
- 1,151
-
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
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.
F. Pantigny
- 40,250


