填充矩阵中的块并设置矩阵的边界

填充矩阵中的块并设置矩阵的边界

我想用橙色填充下面矩阵的非空块,如图所示:

图片

并将 m 放在其边框上(每个块的中心)。在此阶段,我的代码如下:

\documentclass[10pt,xcolor={dvipsnames,table}]{beamer}

\mode<article> % only for the article version
{
  \usepackage{fullpage}
  \usepackage{hyperref}
}


\mode<presentation>
{
  %\setbeamertemplate{background canvas}[vertical shading][bottom=red!10,top=blue!10]
  \setbeamercovered{transparent}
  \usefonttheme{serif}
  \usecolortheme{crane}
}

\setbeamercovered{dynamic}
\setbeamertemplate{items}[circle]

\usepackage{tikz}
\usetikzlibrary{tikzmark}

\begin{document}

\begin{frame}

\begin{equation*}
\mathbf{D} = \left[
\begin{array}{cccccccc}
   \tikz[baseline]{
\node[fill=orange,anchor=base,align=center]{$1$};} & 0 & 0 & 0 & 0 & 0 & 0 & \ldots \\
  0 & 1 & 1 & 0 & 0 & 0 & 0 & \ldots \\
  0 & 1 & -1 & 0 & 0 & 0 & 0 & \ldots \\
  0 & 0 & 0 & 1 & 1 & 1 & 1 & \ldots \\
  0 & 0 & 0 & 1 & -1 & 1 & -1 & \ldots \\
  0 & 0 & 0 & 1 & 1 & -1 & -1 & \ldots \\
  0 & 0 & 0 & 1 & -1 & -1 & 1 & \ldots \\
  \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \ddots
\end{array}
\right]
\end{equation*}

\end{frame}

\end{document}

答案1

作为tikzpicture,使用fitmatrixpositioning

\documentclass[10pt,xcolor={dvipsnames,table}]{beamer}
\setbeamercovered{dynamic}
\setbeamertemplate{items}[circle]

\usepackage{tikz}
\usetikzlibrary{fit,
                matrix,
                positioning}

\begin{document}

\begin{frame}[fragile]

\[
\mathbf{D} = 
\tikz[baseline=(current bounding box.center),
      lbl/.style = {font=\footnotesize, text=orange},
      node distance = 2mm and 2mm]
      {
\matrix (m) [matrix of math nodes,
             nodes={minimum size=1em, inner sep=0pt, anchor=center},
             column sep=10pt, row sep=10pt,
             left delimiter={[},right delimiter={]},
             ]
{
0       & 1         & 1         & 0         & 0         & 0         & 0         & \ldots \\
0       & 1         & -1        & 0         & 0         & 0         & 0         & \ldots \\
0       & 0         & 0         & 1         & 1         & 1         & 1         & \ldots \\
0       & 0         & 0         & 1         & -1        & 1         & -1        & \ldots \\
0       & 0         & 0         & 1         & 1         & -1        & -1        & \ldots \\
0       & 0         & 0         & 1         & -1        & -1        & 1         & \ldots \\
\vdots  & \vdots    & \vdots    & \vdots    & \vdots    & \vdots    & \vdots    & \ddots \\
};
\node (a) [fit=(m-1-1), fill=orange!50, fill opacity = 0.5] {};
\node (b) [fit=(m-2-2) (m-3-3), draw=red] {};
\node (c) [fit=(m-4-4) (m-6-6), draw=red] {};
%
\node[lbl, above=of a |- m.north] {$m=0$};
\node[lbl, above=of b |- m.north] {$m=1$};
\node[lbl, above=of c |- m.north] {$m=2$};
%
\node[lbl, right=of a -| m.east] {$m=0$};
\node[lbl, right=of b -| m.east] {$m=1$};
\node[lbl, right=of c -| m.east] {$m=2$};
    }% end of tikz
\]

\end{frame}

\end{document}

在此处输入图片描述

答案2

使用({bNiceMatrix}nicematrix需要多次编译)。

\documentclass[10pt,xcolor=dvipsnames]{beamer}
\setbeamercovered{dynamic}
\setbeamertemplate{items}[circle]

\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}

\begin{frame}[fragile]


\renewcommand{\arraystretch}{1.4}
\NiceMatrixOptions
  {
    code-for-first-row = \color{orange} ,
    code-for-last-col = \color{orange} ,
  }

\[
\mathbf{D} = 
\begin{bNiceMatrix}[first-row,last-col,margin,colortbl-like]
  m=0     & \multicolumn{2}{c}{m=1} & \multicolumn{3}{c}{m=2} \\
  \cellcolor{orange!50}
  0       & 1         & 1         & 0         & 0         & 0         & 0         & \ldots  & m=0\\
  0       & \Block[draw=red]{2-2}{}
            1         & -1        & 0         & 0         & 0         & 0         & \ldots  & \Block{2-1}{m=1} \\
  0       & 0         & 0         & 1         & 1         & 1         & 1         & \ldots \\
  0       & 0         & 0         & \Block[draw=red]{3-3}{}
                                    1         & -1        & 1         & -1        & \ldots & \Block{3-1}{m=2}\\
  0       & 0         & 0         & 1         & 1         & -1        & -1        & \ldots \\
  0       & 0         & 0         & 1         & -1        & -1        & 1         & \ldots \\
  \vdots  & \vdots    & \vdots    & \vdots    & \vdots    & \vdots    & \vdots    & \ddots \\
\end{bNiceMatrix}
\]

\end{frame}

\end{document}

上述代码的输出

相关内容