7

I previously asked this question here, but now would like to achieve the same goal with a matrix under a different construction. More specifically, how can I highlight or shade the upper or lower triangular part of the following matrix?

enter image description here

The corresponding Latex code is this:

\documentclass{article}
\usepackage{blkarray}
\begin{document}
$R = 
\begin{blockarray}{cccccc}
& S_1 & S_2 &  S_3 & ... & S_n \\
\begin{block}{c(ccccc)}
S_1 &  1 & r_{12} & r_{13} & ... & r_{1n} \\
S_2 &  r_{21} & 1 & r_{23} & ... & r_{2n} \\
S_3 &  r_{31} & r_{32} & 1 & ... & r_{3n} \\
\vdots &  \vdots & \vdots &\vdots & \vdots & \vdots\\
S_n &  r_{n1} & r_{n2} & r_{n3} & ... & 1 \\
\end{block}
\end{blockarray},$
\end{document}

Thanks!

6

You can get the desired effect with TikZ(mark). I define the following variant of TikZmark:

\newcommand\tm[2][]{\tikz[overlay,remember picture,baseline=(#1.base),inner sep=0pt]\node(#1){$#2$};}

It takes two arguments: the first sets the name and the second specifies the content. If you want to highlight the lower triangle of the matrix, use \tm at the top left, bottom left and bottom right element of the matrix. Then fit two nodes to these three elements and \filldraw the shaded triangle.

Code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\newcommand\tm[2][]{\tikz[overlay,remember picture,baseline=(#1.base),inner sep=0pt]\node(#1){$#2$};}
\usepackage{blkarray}
\begin{document}
$R = 
\begin{blockarray}{cccccc}
& S_1 & S_2 &  S_3 & ... & S_n \\
\begin{block}{c(ccccc)}
S_1 &  \tm[a]{1} & r_{12} & r_{13} & ... & r_{1n} \\
S_2 &  r_{21} & 1 & r_{23} & ... & r_{2n} \\
S_3 &  r_{31} & r_{32} & 1 & ... & r_{3n} \\
\vdots &  \vdots & \vdots &\vdots & \vdots & \vdots\\
S_n &  \tm[b]{r_{n1}} & r_{n2} & r_{n3} & ... & \tm[c]{1} \\
\end{block}
\end{blockarray},$
\begin{tikzpicture}[overlay, remember picture]
  \node(x)[fit=(a) (b),inner sep=0pt]{};
  \node(y)[fit=(b) (c),inner sep=0pt]{};
  \filldraw[rounded corners,opacity=.2,green](x.north west)--(x.south west)--(y.south east)--(y.north east)--(x.north east)--cycle;
\end{tikzpicture}
\end{document}

Output

enter image description here

  • This is great! Thanks a lot. How can I modify your code so that the diagonals are excluded? – bluepole Dec 23 '15 at 22:47
  • @bluepole: use \tm at r_{21} and \dots at the bottom row instead of the two 1s. – Herr K. Dec 23 '15 at 22:49
  • Got it! Thanks again for the wonderful solution. – bluepole Dec 23 '15 at 22:52
3

I often highlight matrix parts by using different colors for the elements. You can use the xcolor package and simplify the process with \newcommand.

Minimal Working Example:

 \documentclass{article}
    \thispagestyle{empty}
    \usepackage{blkarray}
    \usepackage{xcolor}

    \newcommand\RED{\color{red}}
    \newcommand\BLUE{\color{blue}}

    \begin{document}
    $R = 
    \begin{blockarray}{cccccc}
    & S_1 & S_2 &  S_3 & ... & S_n \\
    \begin{block}{c(ccccc)}
    S_1 &  1 & \BLUE r_{12} & \BLUE r_{13} & \BLUE ... & \BLUE r_{1n} \\
    S_2 &  \RED r_{21} & 1 & \BLUE r_{23} & \BLUE ... &  \BLUE r_{2n} \\
    S_3 &  \RED r_{31} & \RED r_{32} & 1 &  \BLUE... & \BLUE r_{3n} \\
    \vdots &  \RED \vdots & \RED \vdots &\RED \vdots & \vdots & \BLUE \vdots\\
    S_n &  \RED r_{n1} & \RED r_{n2} & \RED r_{n3} & \RED ... & 1 \\
    \end{block}
    \end{blockarray}$
    \end{document}

If you want to change the background color, we have to think probably about a tikz solution

  • Thanks a lot for the suggestion. It looks good even though I prefer the background highlight. – bluepole Dec 23 '15 at 22:54
1

I created and saved a generic tikz triangle with opacity as \lowertriangle. I then used \stackinset to lay it atop the blockarray, with suitable (empirically derived) scaling and clipping.

EDITED to add \uppertriangle with the upper part of the matrix highlighted.

\documentclass{article}
\usepackage{blkarray,stackengine,tikz,trimclip}
\savestack\lowertriangle{%
\begin{tikzpicture}
  \node[outer sep=1in] (earmark) {};
  \draw[blue!40, fill, opacity=0.20] (earmark.south west) -- (earmark.south east) -- 
    (earmark.north west)-- (earmark.south west);
  \end{tikzpicture}%
}
\savestack\uppertriangle{%
\begin{tikzpicture}
  \node[outer sep=1in] (earmark) {};
  \draw[blue!40, fill, opacity=0.20] (earmark.north west) -- (earmark.south east) -- 
    (earmark.north east)-- (earmark.north west);
  \end{tikzpicture}%
}
\begin{document}
$R = 
\stackinset{r}{8pt}{b}{12pt}{%
\clipbox{0pt 0pt 12pt 7pt}{\scalebox{0.81}[0.48]{\lowertriangle}}}{$
\begin{blockarray}{cccccc}
& S_1 & S_2 &  S_3 & ... & S_n \\
\begin{block}{c(ccccc)}
S_1 &  1 & r_{12} & r_{13} & ... & r_{1n} \\
S_2 &  r_{21} & 1 & r_{23} & ... & r_{2n} \\
S_3 &  r_{31} & r_{32} & 1 & ... & r_{3n} \\
\vdots &  \vdots & \vdots &\vdots & \vdots & \vdots\\
S_n &  r_{n1} & r_{n2} & r_{n3} & ... & 1 \\
\end{block}
\end{blockarray}$},$


$R = 
\stackinset{r}{7pt}{b}{12pt}{%
\clipbox{12pt 7pt 0pt 0pt}{\scalebox{0.81}[0.48]{\uppertriangle}}}{$
\begin{blockarray}{cccccc}
& S_1 & S_2 &  S_3 & ... & S_n \\
\begin{block}{c(ccccc)}
S_1 &  1 & r_{12} & r_{13} & ... & r_{1n} \\
S_2 &  r_{21} & 1 & r_{23} & ... & r_{2n} \\
S_3 &  r_{31} & r_{32} & 1 & ... & r_{3n} \\
\vdots &  \vdots & \vdots &\vdots & \vdots & \vdots\\
S_n &  r_{n1} & r_{n2} & r_{n3} & ... & 1 \\
\end{block}
\end{blockarray}$},$
\end{document}

enter image description here

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.