如何突出显示矩阵中的元素?

如何突出显示矩阵中的元素?

我打算用这张图让学生清楚、轻松地计算 3x3 行列式。我试图将这张图片添加到我的讲座中,但由于图片质量差,图片很模糊。所以我想用乳胶创建完全相同的图形。我搜索并阅读了一些关于在矩阵中突出显示元素和对角线的主题,但在这种情况下不起作用。我想像图片中一样添加加号和减号,并用矩阵外的元素突出显示矩阵中的元素。我感谢您的所有帮助和建议。提前谢谢您! 在此处输入图片描述

答案1

另一种“纯” TikZ 解决方案:

\documentclass[border=1mm,tikz]{standalone}
\usetikzlibrary{calc,matrix}

    \begin{document}
\begin{tikzpicture}[
strip/.style args = {#1/#2}{
    draw=#1, very thin, fill=#1, opacity=0.2,
    text width=#2,% will be calculated in path
    rounded corners,% only if you like them ...
    minimum height=1em,% adjust to desired height,
    inner xsep=-1mm,% adjust to desired length of strip
    inner ysep=2mm,
    yshift=-0.6ex,
    sloped},
                    ]
\matrix (mtrx)  [matrix of math nodes,
                 column sep=1em,   
                 nodes={text height=2ex,text width=2ex}]
{
|[red]|+
    & |[red]|+
          & \color{red}+\color{blue}- 
                & |[blue]|- 
                      & |[blue]|-   \\
a_1 & b_1 & c_1 & a_1 & a_2   \\
a_2 & b_2 & c_2 & a_2 & b_2   \\
a_3 & b_3 & c_3 & a_3 & b_3   \\
};
\draw[thick] (mtrx-2-1.north) -| (mtrx-4-1.south -- (mtrx-4-1.south);
\draw[thick] (mtrx-2-3.north) -| (mtrx-4-3.south -- (mtrx-4-3.south);
\path let   \p1 = (mtrx-4-1.center),
            \p2 = (mtrx-2-4.center),
            \n1 = {veclen(\y2-\y1,\x2-\x1)} in
      (mtrx-4-1.center) -- node[strip=blue/\n1] {} (mtrx-2-3.center)
      (mtrx-4-2.center) -- node[strip=blue/\n1] {} (mtrx-2-4.center)
      (mtrx-4-3.center) -- node[strip=blue/\n1] {} (mtrx-2-5.center)
%
      (mtrx-2-1.center) -- node[strip=red/\n1] {} (mtrx-4-3.center)
      (mtrx-2-2.center) -- node[strip=red/\n1] {} (mtrx-4-4.center)
      (mtrx-2-3.center) -- node[strip=red/\n1] {} (mtrx-4-5.center);
\end{tikzpicture}
    \end{document}

在此处输入图片描述

如果您喜欢不同的颜色,您可以在@MyUserlsThis 的回答中进行类似的定义。

编辑: 我不知道回答,@egreg 谢谢你指出这一点。考虑一下,解决方案更简单(也更漂亮):

\documentclass[border=1mm,tikz]{standalone}
\usetikzlibrary{calc,matrix}

        \begin{document}
\begin{tikzpicture}[
strip/.style = {
    draw=#1,%color
    line width=1em, opacity=0.2,
    line cap=round ,% only if you like them ...
                            },
                    ]
\matrix (mtrx)  [matrix of math nodes,
                 column sep=1em,   
                 nodes={text height=2ex,text width=2ex}]
{
|[red]|+
    & |[red]|+
          & \color{red}+\color{blue}- 
                & |[blue]|- 
                      & |[blue]|-   \\
a_1 & b_1 & c_1 & a_1 & a_2   \\
a_2 & b_2 & c_2 & a_2 & b_2   \\
a_3 & b_3 & c_3 & a_3 & b_3   \\
};
\draw[thick] (mtrx-2-1.north) -| (mtrx-4-1.south west) 
                              -- (mtrx-4-1.south);
\draw[thick] (mtrx-2-3.north) -| (mtrx-4-3.south east)
                              -- (mtrx-4-3.south);
\path[draw,strip=red,shorten <=-1mm]
    (mtrx-4-1.center) edge (mtrx-2-3.center)
    (mtrx-4-2.center) edge (mtrx-2-4.center)
    (mtrx-4-3.center)  --  (mtrx-2-5.center);
\path[draw,strip=blue,shorten >=-1mm]
    (mtrx-2-1.center) edge (mtrx-4-3.center)
    (mtrx-2-2.center) edge (mtrx-4-4.center)
    (mtrx-2-3.center)  --  (mtrx-4-5.center);

\end{tikzpicture}
    \end{document}

这使:

在此处输入图片描述

编辑(2): 关于@BarbaraBenton的评论:符号 + 和 - 可以移动近线宽,减少第一行和第二行之间的距离。这也要注意,当条带是矩形时,可以使用下一个 MWE 获得(其中颜色也互换):

\documentclass[border=1mm,tikz]{standalone}
\usetikzlibrary{calc,matrix}

        \begin{document}
\begin{tikzpicture}[
strip/.style = {
    draw=#1,%color
    line width=1em, opacity=0.2,
    shorten <=-2mm,shorten >=-2mm,
                            },
                    ]
\matrix (mtrx)  [matrix of math nodes,
                 column sep=1em,
                 nodes={text height=1ex,text width=2ex}]
{
|[red]|+
    & |[red]|+
          & \color{red}+\color{blue}-
                & |[blue]|-
                      & |[blue]|-   \\[3.3mm,between origins]
a_1 & b_1 & c_1 & a_1 & a_2         \\
a_2 & b_2 & c_2 & a_2 & b_2         \\
a_3 & b_3 & c_3 & a_3 & b_3         \\
};
\draw[thick] (mtrx-2-1.north) -| (mtrx-4-1.south west)
                              -- (mtrx-4-1.south);
\draw[thick] (mtrx-2-3.north) -| (mtrx-4-3.south east)
                              -- (mtrx-4-3.south);
\path[draw,strip=blue]
    (mtrx-4-1.center) edge (mtrx-2-3.center)
    (mtrx-4-2.center) edge (mtrx-2-4.center)
    (mtrx-4-3.center)  --  (mtrx-2-5.center);
\path[draw,strip=red]
    (mtrx-2-1.center) edge (mtrx-4-3.center)
    (mtrx-2-2.center) edge (mtrx-4-4.center)
    (mtrx-2-3.center)  --  (mtrx-4-5.center);
\end{tikzpicture}
    \end{document}

在此处输入图片描述

当然,仍有一些地方可以进行微调。例如,将 + 和 - 符号调小,或将位置调整不同,等等,但有些事情应该留给 OP(如果他能接受这个答案的话)。

编辑(3): 对于与文档类一起使用beamer,这里有完整的并且经过多次测试的 MWE(使用最新的 Beamer:版本 3.36):

    \documentclass{beamer} 
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}

        \begin{document}
\begin{frame}[fragile]
\begin{tikzpicture}[
strip/.style = {
    draw=#1,%color
    line width=1em, opacity=0.2,
    shorten <=-2mm,shorten >=-2mm,
                            },
                    ]
\matrix (mtrx)  [matrix of math nodes,
                 column sep=1em,
                 nodes={text height=1ex,text width=2ex}]
{
|[red]|+
    & |[red]|+
          & \color{red}+\color{blue}-
                & |[blue]|-
                      & |[blue]|-   \\[3.3mm,between origins]
a_1 & b_1 & c_1 & a_1 & a_2         \\
a_2 & b_2 & c_2 & a_2 & b_2         \\
a_3 & b_3 & c_3 & a_3 & b_3         \\
};
\draw[thick] (mtrx-2-1.north) -| (mtrx-4-1.south west)
                              -- (mtrx-4-1.south);
\draw[thick] (mtrx-2-3.north) -| (mtrx-4-3.south east)
                              -- (mtrx-4-3.south);
\path[draw,strip=blue]
    (mtrx-4-1.center) edge (mtrx-2-3.center)
    (mtrx-4-2.center) edge (mtrx-2-4.center)
    (mtrx-4-3.center)  --  (mtrx-2-5.center);
\path[draw,strip=red]
    (mtrx-2-1.center) edge (mtrx-4-3.center)
    (mtrx-2-2.center) edge (mtrx-4-4.center)
    (mtrx-2-3.center)  --  (mtrx-4-5.center);
\end{tikzpicture}
\end{frame}
    \end{document}

答案2

在玩了 tikz 之后,我相信我可以做得不错。我只是一个 tikz 初学者,所以我 100% 确信这可以做得更好,而不需要对位置进行如此多的微调。不过,您可以调整所有位置和颜色:

\documentclass{article}

\usepackage{tikz}
\usepackage{amsmath}
\usepackage{xcolor}
\definecolor{myblue}{rgb}{0,0,0.8}
\definecolor{myred}{rgb}{0.8,0,0}


\begin{document}
\begin{tikzpicture}
\node[fill=none,draw=none] (matrix)
{
\begin{minipage}{\textwidth}
\[ \begin{bmatrix} 
a_1 & b_1 & c_1 \\
a_2 & b_2 & c_2 \\
a_3 & b_3 & c_3
\end{bmatrix}
\begin{matrix}
a_1 & b_1 \\
a_2 & b_2 \\
a_3 & b_3
\end{matrix} \]
\end{minipage}
};
\draw[myblue,fill=myblue,opacity=0.2] (-1.4,-0.4)--(0.25,0.6)--(0.4,0.4)--(-1.25,-0.6);
\draw[myblue,fill=myblue,opacity=0.2] (-0.7,-0.4)--(0.9,0.6)--(1.05,0.4)--(-0.55,-0.6);
\draw[myblue,fill=myblue,opacity=0.2] (0,-0.4)--(1.55,0.6)--(1.7,0.4)--(0.15,-0.6);
\draw[myred,fill=myred,opacity=0.2] (-1.4,0.4)--(0.25,-0.6)--(0.4,-0.4)--(-1.25,0.6);
\draw[myred,fill=myred,opacity=0.2] (-0.7,0.4)--(0.85,-0.6)--(1.0,-0.4)--(-0.55,0.6);
\draw[myred,fill=myred,opacity=0.2] (0,0.4)--(1.55,-0.6)--(1.7,-0.4)--(0.15,0.6);
\node[above of=matrix,xshift=-1.3cm,yshift=-0.2cm] {\textcolor{myred}{$+$}};
\node[above of=matrix,xshift=-0.6cm,yshift=-0.2cm] {\textcolor{myred}{$+$}};
\node[above of=matrix,xshift=-0cm,yshift=-0.2cm] {\textcolor{myred}{$+$}};
\node[above of=matrix,xshift=0.4cm,yshift=-0.2cm] {\textcolor{myblue}{$-$}};
\node[above of=matrix,xshift=1cm,yshift=-0.2cm] {\textcolor{myblue}{$-$}};
\node[above of=matrix,xshift=1.6cm,yshift=-0.2cm] {\textcolor{myblue}{$-$}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑:鉴于芭芭拉的评论,我尝试扩大行之间的空间,以便更容易看到。我完全按照以下步骤操作:增量矩阵空间。唯一的问题是矩阵的括号太高(它最终位于加号和减号上方),我不知道如何修复它。我留下了代码和结果,如果有人知道如何做,我很乐意编辑它。或者您可以增加yshift加号和减号的值以将它们放在矩阵上方。

\documentclass{article}

\usepackage{tikz}
\usepackage{amsmath}
\usepackage{xcolor}
\definecolor{myblue}{rgb}{0,0,0.8}
\definecolor{myred}{rgb}{0.8,0,0}

\begingroup
\renewcommand*{\arraystretch}{1.5}
% your pmatrix expression
\endgroup

\makeatletter
\renewcommand*\env@matrix[1][\arraystretch]{%
  \edef\arraystretch{#1}%
  \hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{*\c@MaxMatrixCols c}}
\makeatother

\begin{document}
\begin{tikzpicture}
\node[fill=none,draw=none] (matrix)
{
\begin{minipage}{\textwidth}
\[ \begin{bmatrix}[1.5] 
a_1 & b_1 & c_1 \\
a_2 & b_2 & c_2 \\
a_3 & b_3 & c_3
\end{bmatrix}
\begin{matrix}[1.5]
a_1 & b_1 \\
a_2 & b_2 \\
a_3 & b_3
\end{matrix} \]
\end{minipage}
};
\draw[myblue,fill=myblue,opacity=0.2] (-1.4,-0.7)--(0.2,0.75)--(0.35,0.55)--(-1.25,-0.9);
\draw[myblue,fill=myblue,opacity=0.2] (-0.7,-0.7)--(0.8,0.75)--(0.95,0.55)--(-0.55,-0.9);
\draw[myblue,fill=myblue,opacity=0.2] (0,-0.7)--(1.45,0.75)--(1.6,0.55)--(0.15,-0.9);
\draw[myred,fill=myred,opacity=0.2] (-1.4,0.55)--(0.25,-0.9)--(0.4,-0.7)--(-1.25,0.75);
\draw[myred,fill=myred,opacity=0.2] (-0.7,0.55)--(0.85,-0.9)--(1.0,-0.7)--(-0.55,0.75);
\draw[myred,fill=myred,opacity=0.2] (0,0.55)--(1.55,-0.9)--(1.7,-0.7)--(0.15,0.75);
\node[above of=matrix,xshift=-1.3cm,yshift=-0.1cm] {\textcolor{myred}{$+$}};
\node[above of=matrix,xshift=-0.6cm,yshift=-0.1cm] {\textcolor{myred}{$+$}};
\node[above of=matrix,xshift=-0cm,yshift=-0.1cm] {\textcolor{myred}{$+$}};
\node[above of=matrix,xshift=0.4cm,yshift=-0.1cm] {\textcolor{myblue}{$-$}};
\node[above of=matrix,xshift=1cm,yshift=-0.1cm] {\textcolor{myblue}{$-$}};
\node[above of=matrix,xshift=1.6cm,yshift=-0.1cm] {\textcolor{myblue}{$-$}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

使用{NiceMatrix}of nicematrix(≥ 5.16 2021-06-20) 和 Tikz 来绘制规则。

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

\begin{document}

$\begin{NiceMatrix}[columns-width=7mm]
\CodeBefore [create-cell-nodes]
    \begin{tikzpicture} 
      [line width=3.5mm, opacity=0.2, shorten < = -3mm, shorten > = -3mm]
      \draw [blue] (4-1.center) -- (2-3.center) ;
      \draw [blue] (4-2.center) -- (2-4.center) ;
      \draw [blue] (4-3.center) -- (2-5.center) ;
      \draw [red] (2-1.center) -- (4-3.center) ;
      \draw [red] (2-2.center) -- (4-4.center) ;
      \draw [red] (2-3.center) -- (4-5.center) ;
    \end{tikzpicture}
\Body
    \color{red} +   & \color{red} +   & \color{red}+\color{blue}-  & \color{blue}-   & \color{blue}- \\
    a_1 & b_1 & c_1 & a_1 & a_2   \\[2mm]
    a_2 & b_2 & c_2 & a_2 & b_2   \\[2mm]
    a_3 & b_3 & c_3 & a_3 & b_3   \\
\CodeAfter
    \SubMatrix[{2-1}{4-3}]
\end{NiceMatrix}$

\end{document}

您需要多次编译(因为 PGF/Tikz 节点)。

上述代码的输出

答案4

合并

我们可以使用 TikZ-CD 实现这一点,而无需绘制节点。

代码

\documentclass{article}
\usepackage{tikz-cd,amsmath}
\usetikzlibrary{backgrounds,calc}
\tikzcdset{
  math matrix/.style={
    column sep=1.5*\the\arraycolsep, row sep=.3*\the\baselineskip,
    cells={nodes={font=\mathstrut, inner sep=+0pt, outer sep=+0pt}}},
  lr delim/.style={/tikz/every matrix/.append style={lr delim={#1}}}}
\tikzset{% https://tex.stackexchange.com/a/660470/16595
  lr delim/.style args={#1#2 and #3#4 around #5 to #6}{append after command={
    {{[local bounding box=@] (\tikzlastnode-#5.north west)
     (\tikzlastnode-#6.south east)}(@)[every left delimiter/.append style={#2},
                                      every right delimiter/.append style={#4},
                 late options={left delimiter={#1},right delimiter={#3}}]}[]}}}
\makeatletter
\let\tikzcd@ar@new@orig\tikzcd@ar@new
\def\tikzcd@ar@new@bg[#1]{% https://tex.stackexchange.com/a/664328/16595
  \pgfutil@g@addto@macro\tikzcd@savedpaths{%
    \pgfonlayer{background}\path[/tikz/commutative diagrams/.cd,every arrow,#1]%
    (\tikzcd@ar@start\tikzcd@startanchor)to(\tikzcd@ar@target\tikzcd@endanchor);
    \endpgfonlayer}\let\tikzcd@ar@new\tikzcd@ar@new@orig}
\tikzcdset{every diagram/.append code=%
  \def\bgar{\let\tikzcd@ar@new\tikzcd@ar@new@bg\tikzcd@arrow}}
\makeatother
\tikzcdset{
  highlight/.style 2 args={
    /tikz/arrows=-, draw opacity=.5, draw=#1!50, text=#1, line width=.95em,
    line cap=round, to path={let\p0=($(\tikztostart)-(\tikztotarget)$),
      \n0={atan2(\y0,\x0)} in (\tikztostart.\n0)--(\tikztotarget.\n0-180)
        node[pos=1.2]{$#2$}\tikztonodes}}}
\begin{document}
\[\begin{tikzcd}[math matrix, lr delim={[ and ] around 1-1 to 3-3},
  cells={nodes={align=center,text width=width("$a_0$")}},
  minus/.style={highlight={blue}{-}}, plus/.style={highlight={red}{+}}]
  a_1 & b_1 & c_1 & a_1 & b_1 \\
  a_2 & b_2 & c_2 & a_2 & b_2 \\
  a_3 \bgar[rruu, minus]
& b_3 \bgar[rruu, minus]
& c_3 \bgar[rruu, minus] \bgar[lluu, plus]
& a_3                    \bgar[lluu, plus]
& b_3                    \bgar[lluu, plus]
\end{tikzcd}\]

\newcommand*\mPlus[1] {\mathcolor{red} {#1}}
\newcommand*\mMinus[1]{\mathcolor{blue}{#1}}
\[ \det A = (\mPlus {a_1b_2c_3} + \mPlus {b_1c_2a_3} + \mPlus {c_1a_2b_3})
           -(\mMinus{a_3b_2c_1} + \mMinus{b_3c_2a_1} + \mMinus{c_3a_2b_1}) \]
\end{document}

输出

在此处输入图片描述

相关内容