我之前问过这个问题这里,但现在想用不同构造的矩阵实现相同目标。更具体地说,如何突出显示或阴影化以下矩阵的上三角部分或下三角部分?
对应的Latex代码是这样的:
\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}
谢谢!
答案1
你可以用 TikZ(mark) 得到想要的效果。我定义了以下 TikZmark 变体:
\newcommand\tm[2][]{\tikz[overlay,remember picture,baseline=(#1.base),inner sep=0pt]\node(#1){$#2$};}
它需要两个参数:第一个参数设置名称,第二个参数指定内容。如果要突出显示矩阵的下三角,请使用\tm
矩阵左上角、左下角和右下角的元素。然后将两个节点拟合到这三个元素和\filldraw
阴影三角形上。
代码
\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}
输出
答案2
我经常使用不同的颜色来突出显示矩阵部分。您可以使用 xcolor 包并使用 \newcommand 简化此过程。
\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}
如果你想改变背景颜色,我们可能需要考虑一个 tikz 解决方案
答案3
我创建并保存了一个tikz
不透明度为 的通用三角形\lowertriangle
。然后我将\stackinset
其放在 之上blockarray
,并进行适当的(经验得出的)缩放和剪裁。
已编辑,\uppertriangle
并突出显示矩阵的上部。
\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}