突出显示矩阵的不​​同部分

突出显示矩阵的不​​同部分

我想突出显示矩阵的不​​同部分。我使用过此代码,但效果不佳。我该如何修改它?

我想要构建下图:

在此处输入图片描述

我在下面使用了 nicematrix,但这是输出使用 Overleaf

在此处输入图片描述

 \documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit}
\newcommand{\overlay}[2][]{\tikz[overlay,
  remember picture, #1]{#2}}
\tikzset{
  highlighted/.style = { draw, thick, rectangle,
                         rounded corners, inner sep = 0pt,
                         fill = red!15, fill opacity = 0.5
                       }
}
\newcommand{\highlight}[1]{%
  \overlay{
    \node [fit = (left.north west) (right.south east),
           highlighted] (#1) {}; }
}
\newcommand{\flag}[2]{\overlay[baseline=(#1.base)]
  {\node (#1) {$#2$};}}
\begin{document}
\[
  M = \begin{pmatrix}
    \flag{left}{p_1}, & p_2, & \flag{right}{p_3}, & \cdots, & \flag{left}{p_1},& p_2-p_1,& \flag{right}{p_3-p_2},&\cdots \\
  \end{pmatrix}
  \highlight{N}
  \qquad
  M^T = \begin{pmatrix}
    \flag{left}{p_1} & p_2 & p_3  \\
    p_1, & p_2-p_1, & \flag{right}{p_3-p_2}
  \end{pmatrix}
  \highlight{NT}
\]
\overlay{
  \draw[->, thick, red, dotted] (before) -- (after);
  \draw[->, thick, red, dashed] (N) -- (NT)
    node [pos=0.68, above] {};
  \node[above of = N ] { $x$   };
  \node[above of = NT] { $z$ };
}
\end{document}

答案1

我使用该nicematrix包来复制手动绘图。它使用 Tikz 节点,因此语法对您来说很熟悉。

是

\documentclass{article}
 
\usepackage{nicematrix} % added <<<<<<
 
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}        

\tikzset{highlight/.style={rectangle,
        fill=red!15,
        blend mode = multiply,
        rounded corners = 0.5 mm,
        inner sep=1pt,
        fit = #1}}          
    
\[
M = \begin{bNiceMatrix}[first-row, extra-margin=2pt]
        & x     &       &           &       &y          &           & \\
    p_1,& p_2,  & p_3,  & \cdots    & p_1,  & p_2-p_1,  & p_3-p_2,  &\cdots \\
\CodeAfter \tikz \node [highlight = (1-1) (1-3)] {} ;
\tikz \node [highlight = (1-5) (1-7)] {} ;      
\end{bNiceMatrix}\] 

\bigskip

\[
M^T = \begin{bNiceMatrix}[first-row,extra-margin=2pt]
            &z          &       \\
    p_1,    & p_2,      & p_3  \\
    p_1,    & p_2-p_1,  & p_3-p_2
        \CodeAfter \tikz \node [highlight = (2-1) (2-3)] {} ;
\end{bNiceMatrix}
\]

\end{document}

更新(带箭头)

是

\documentclass{article}
 
\usepackage{nicematrix} % added <<<<<<
 
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit}

% from https://latex-cookbook.net/highlight-formula/
\newcommand{\overlay}[2][]{\tikz[overlay,remember picture, #1]{#2}}

\newcommand{\flag}[2]{\overlay[baseline=(#1.base)]
{\node (#1) {$#2$};}}   

\begin{document}        

\tikzset{highlight/.style={rectangle,
        fill=red!15,
        blend mode = multiply,
        rounded corners = 0.5 mm,
        inner sep=1pt,
        fit = #1}}          
    
\[
M = \begin{bNiceMatrix}[first-row, extra-margin=2pt]
        & x                 &       &           &       &y          &                       & \\
    p_1,& p_2, \flag{Mx}{}  & p_3,  & \cdots    & p_1,  & p_2-p_1,  & \flag{My}{}p_3-p_2,   &\cdots \\
\CodeAfter \tikz \node [highlight = (1-1) (1-3)] {} ;
\tikz \node [highlight = (1-5) (1-7)] {} ;      
\end{bNiceMatrix}\]


\bigskip

\[
M^T = \begin{bNiceMatrix}[first-row,extra-margin=2pt]
            &\flag{MTz}{z}          &       \\
    p_1,    & p_2,      & p_3  \\
    p_1,    & p_2-p_1,  & p_3-p_2
        \CodeAfter \tikz \node [highlight = (2-1) (2-3)] {} ;
\end{bNiceMatrix}
\]

    \overlay{
    \draw[->, thick, red, dotted] (Mx) -- (MTz);
    \draw[->, thick, red, dotted] (My) -- (MTz);
}

\end{document}

答案2

nicematrix对 Simon Dispa 的答案进行了轻微修改,由于最新版本(2021-06-20 的 v 5.16)未使用透明度。

\documentclass{article}
 
\usepackage{nicematrix} % added <<<<<<
 
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}        

\tikzset{highlight/.style={rectangle,
        fill=red!15,
        rounded corners = 0.5 mm,
        inner sep=1pt,
        fit = #1}}          
    
\[
M = \begin{bNiceMatrix}[first-row, extra-margin=2pt]
\CodeBefore [create-cell-nodes]
  \tikz \node [highlight = (1-1) (1-3)] {} ;
  \tikz \node [highlight = (1-5) (1-7)] {} ;      
\Body
        & x     &       &           &       &y          &           & \\
    p_1,& p_2,  & p_3,  & \cdots    & p_1,  & p_2-p_1,  & p_3-p_2,  &\cdots \\
\end{bNiceMatrix}\] 

\bigskip

\[
M^T = \begin{bNiceMatrix}[first-row,extra-margin=2pt]
\CodeBefore [create-cell-nodes]
  \tikz \node [highlight = (2-1) (2-3)] {} ;
\Body
            &z          &       \\
    p_1,    & p_2,      & p_3  \\
    p_1,    & p_2-p_1,  & p_3-p_2
\end{bNiceMatrix}
\]

\end{document}

与往常一样nicematrix,您需要进行多次编译(因为 PGF/Tikz 节点)。

上述代码的输出

相关内容