使用 Tikzpicture 用不同的背景颜色填充两个 Nicematrices

使用 Tikzpicture 用不同的背景颜色填充两个 Nicematrices

我想用两种不同的背景颜色填充这两个(2)矩阵。

\documentclass[xcolor=dvipsnames,svgnames]{beamer}
\usetheme{Madrid}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows}
\usepackage{graphicx,animate}
%%%%%%%%%%%%%%                  %%%%%%%%%%%%%%
\usepackage{nicematrix, booktabs}
\usepackage{nicematrix}
\usepackage{booktabs, multirow}
\begin{document}

    \begin{frame}[shrink = 39]
        \frametitle{~}
        \begin{tikzpicture}[
            remember picture,
            overlay,
            expl/.style={draw=orange,fill=orange!30,rounded corners,text width=9cm},
            arrow/.style={red!80!black,ultra thick,->,>=latex}
            ]
        
            \node[expl]
            (rhootext)
            at (5.5,-3.0cm)
            {\[
                \setcounter{MaxMatrixCols}{20}
                    \begin{pNiceMatrix}[last-col = 12]
                    x_1 & x_2 & x_3 &     &     &     &     &     &     &        &  & \mathbf{B_1} \\
                    & x_2 & x_3 & x_4 &     &     &     &     &     &        &  & \mathbf{B_2} \\
                    &     & x_3 & x_4 & x_5 &     &     &     &     &        &  & \mathbf{B_3} \\
                    &     &     & x_4 & x_5 & x_6 &     &     &     &        &  & \mathbf{B_4} \\
                    &     &     &     & x_5 & x_6 & x_7 &     &     &        &  & \mathbf{B_5} \\
                    &     &     &     &     & x_6 & x_7 & x_8 &     &        &  & \mathbf{B_6} \\
                    &     &     &     &     &     & x_7 & x_8 & x_9 &        &  & \mathbf{B_7} \\
                    &     &     &     &     &     &     & x_8 & x_9 & x_{10} &  & \mathbf{B_8}
                \end{pNiceMatrix}
                \]};
        
            \node[expl]
            (rhotext)
            at (14.5,-8.8cm)
            {\[
                \setcounter{MaxMatrixCols}{20}
                \begin{pNiceMatrix}[last-col = 11]
                    x_1 & x_2 & x_3 &     &     &     &     &     &      &        &  \mathbf{B_1} \\
                    & x_2 & x_3 & x_4 &     &     &     &     &      &        &  \mathbf{B_2} \\
                    &     &     & x_4 & x_5 & x_6 &     &     &      &        &  \mathbf{B_3} \\
                    &     &     &     & x_5 & x_6 & x_7 &     &      &        &  \mathbf{B_4} \\
                    &     &     &     &     &     & x_7 & x_8 & x_9  &        &  \mathbf{B_5} \\
                    &     &     &     &     &     &     & x_8 & x_9  & x_{10} &  \mathbf{B_6} \\
                \end{pNiceMatrix}
                \]};
        
            \draw[arrow]
            (rhootext.east) to[out=0,in=0]([yshift=0.5ex]{rhotext});
        \end{tikzpicture}
    \end{frame}
\end{document}

这里的两个矩阵

答案1

您可以在节点样式中将其颜色定义为选项,例如,如果节点样式名称是box

   box/.style = {draw=#1, rounded corners, fill=#1!30},

然后用作

\node[box=cyan] (n1) {...};

考虑到上述情况,并引入新的命令以便更短地写作\mathbf{B_1}\B_1MWE 可以是:

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,    % <---
                bending,        % <---
                positioning}    % <---
\usepackage{nicematrix}

\begin{document}
    \begin{tikzpicture}[
node distance = 6mm and -6mm,   % <---
   box/.style = {draw=#1, rounded corners, fill=#1!30}, % <---
   arr/.style = {draw=red!80!black, ultra thick,
                 -{Latex[width=1pt 3,flex]} }           % <---
                    ]
\newcommand\B{\mathbf{B}}   % <---
\node[box=cyan] (n1)        % <---
    {$
    \begin{pNiceMatrix}[last-col=11]
x_1 & x_2 & x_3 &     &     &     &     &     &     &           & \B_1   \\
    & x_2 & x_3 & x_4 &     &     &     &     &     &           & \B_2   \\
    &     & x_3 & x_4 & x_5 &     &     &     &     &           & \B_3   \\
    &     &     & x_4 & x_5 & x_6 &     &     &     &           & \B_4   \\
    &     &     &     & x_5 & x_6 & x_7 &     &     &           & \B_5   \\
    &     &     &     &     & x_6 & x_7 & x_8 &     &           & \B_6   \\
    &     &     &     &     &     & x_7 & x_8 & x_9 &           & \B_7   \\
    &     &     &     &     &     &     & x_8 & x_9 & x_{10}    & \B_8   
    \end{pNiceMatrix}
    $};
\node[box=orange, below right=of n1] (n2)   % <---
    {$
    \begin{pNiceMatrix}[last-col=11]
x_1 & x_2 & x_3 &     &     &     &     &     &     &           & \B_1   \\
    & x_2 & x_3 & x_4 &     &     &     &     &     &           & \B_2   \\
    &     &     & x_4 & x_5 & x_6 &     &     &     &           & \B_3   \\
    &     &     &     & x_5 & x_6 & x_7 &     &     &           & \B_4   \\
    &     &     &     &     &     & x_7 & x_8 & x_9 &           & \B_5   \\
    &     &     &     &     &     &     & x_8 & x_9 & x_{10}    & \B_6
    \end{pNiceMatrix}
    $};
\path[arr]  (n1.east) to[out=0,in=0, looseness=2]    (n2.center);   % <---
    \end{tikzpicture}
\end{document}

如您所见,MWE 引入了一些与主题无关的更改,在我看来,这些更改使 matric 图像更美观,代码更简短、更一致。与您的 MWE 相比,所有更改在代码中均以 标记% <---

的定义B插入到`tikzpicture中,因此仅限于此图像。但是,您可以将此定义移动到文档前言中,然后在文档中的任何位置使用。

在此处输入图片描述

附录: 我不清楚节点之间的箭头有什么用途。所以我现在猜你可能喜欢下图所示的箭头:

在此处输入图片描述

上图的 MWE 为:

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,    
                bending,        
                positioning}    
\usepackage{nicematrix}

\begin{document}
    \begin{tikzpicture}[
node distance = 6mm and -6mm,   
   box/.style = {draw=#1, rounded corners, thick, fill=#1!15},       % <---
   arr/.style = {draw=red!80!black,line width=2mm, shorten >=-0.5ex, % <---
                 -{Stealth[width=0pt 3,inset=0pt 1, flex]} }         % <---
                    ]
\newcommand\B{\mathbf{B}}   
\node[box=cyan] (n1)    
    {$
    \begin{pNiceMatrix}[last-col=11]
x_1 & x_2 & x_3 &     &     &     &     &     &     &           & \B_1   \\
    & x_2 & x_3 & x_4 &     &     &     &     &     &           & \B_2   \\
    &     & x_3 & x_4 & x_5 &     &     &     &     &           & \B_3   \\
    &     &     & x_4 & x_5 & x_6 &     &     &     &           & \B_4   \\
    &     &     &     & x_5 & x_6 & x_7 &     &     &           & \B_5   \\
    &     &     &     &     & x_6 & x_7 & x_8 &     &           & \B_6   \\
    &     &     &     &     &     & x_7 & x_8 & x_9 &           & \B_7   \\
    &     &     &     &     &     &     & x_8 & x_9 & x_{10}    & \B_8   
    \end{pNiceMatrix}
    $};
\node[box=orange, below right=of n1] (n2)   
    {$
    \begin{pNiceMatrix}[last-col=11]
x_1 & x_2 & x_3 &     &     &     &     &     &     &           & \B_1   \\
    & x_2 & x_3 & x_4 &     &     &     &     &     &           & \B_2   \\
    &     &     & x_4 & x_5 & x_6 &     &     &     &           & \B_3   \\
    &     &     &     & x_5 & x_6 & x_7 &     &     &           & \B_4   \\
    &     &     &     &     &     & x_7 & x_8 & x_9 &           & \B_5   \\
    &     &     &     &     &     &     & x_8 & x_9 & x_{10}    & \B_6
    \end{pNiceMatrix}
    $};
\scoped[opacity=0.3,transparency group]                     % <---
\path[arr]  (n1.east)   to[bend left=45]    (n2.center);    % <---
    \end{tikzpicture}
\end{document}

与上述 MWE 相比,所有变化在代码中均用 % <--- 标记

答案2

请清理您的前言,不要多次加载包(graphicxanimatebooktabs在此示例中未使用,booktabs并且nicematrix都加载了两次)。

此外,在这种情况下,remember picture您可能不需要这些选项。overlay

至于您的问题:只需覆盖其中一个节点的fill和选项即可设置另一种颜色:draw

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows}
\usepackage{nicematrix}

\begin{document}

    \begin{tikzpicture}[
        expl/.style={draw=orange,fill=orange!30,rounded corners,text width=9cm},
        arrow/.style={red!80!black,ultra thick,->,>=latex}
    ]
    
        \node[expl, draw=cyan, fill=cyan!30]  % <- add options here (or at the other node)
        (rhootext)
        at (5.5,-3.0cm)
        {\[
            \setcounter{MaxMatrixCols}{20}
                \begin{pNiceMatrix}[last-col = 12]
                x_1 & x_2 & x_3 &     &     &     &     &     &     &        &  & \mathbf{B_1} \\
                & x_2 & x_3 & x_4 &     &     &     &     &     &        &  & \mathbf{B_2} \\
                &     & x_3 & x_4 & x_5 &     &     &     &     &        &  & \mathbf{B_3} \\
                &     &     & x_4 & x_5 & x_6 &     &     &     &        &  & \mathbf{B_4} \\
                &     &     &     & x_5 & x_6 & x_7 &     &     &        &  & \mathbf{B_5} \\
                &     &     &     &     & x_6 & x_7 & x_8 &     &        &  & \mathbf{B_6} \\
                &     &     &     &     &     & x_7 & x_8 & x_9 &        &  & \mathbf{B_7} \\
                &     &     &     &     &     &     & x_8 & x_9 & x_{10} &  & \mathbf{B_8}
            \end{pNiceMatrix}
            \]};
    
        \node[expl]
        (rhotext)
        at (14.5,-8.8cm)
        {\[
            \setcounter{MaxMatrixCols}{20}
            \begin{pNiceMatrix}[last-col = 11]
                x_1 & x_2 & x_3 &     &     &     &     &     &      &        &  \mathbf{B_1} \\
                & x_2 & x_3 & x_4 &     &     &     &     &      &        &  \mathbf{B_2} \\
                &     &     & x_4 & x_5 & x_6 &     &     &      &        &  \mathbf{B_3} \\
                &     &     &     & x_5 & x_6 & x_7 &     &      &        &  \mathbf{B_4} \\
                &     &     &     &     &     & x_7 & x_8 & x_9  &        &  \mathbf{B_5} \\
                &     &     &     &     &     &     & x_8 & x_9  & x_{10} &  \mathbf{B_6} \\
            \end{pNiceMatrix}
            \]};
    
        \draw[arrow]
        (rhootext.east) to[out=0,in=0]([yshift=0.5ex]{rhotext});
    \end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容