用边界着色矩阵中的细胞

用边界着色矩阵中的细胞

我正在尝试为矩阵中具有非空条目的单元格着色,但似乎\cellcolor不起作用。有什么解决办法吗?此外,第一列(矩阵外)的垂直点具有正常大小。我的代码如下:

\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[table]{xcolor}
\usepackage{amsmath}
\usepackage{kbordermatrix}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\newcommand\x{\times}


\begin{document}

\begin{frame}

\begin{equation*}
\kbordermatrix{
  & 2^{0} & 2^{1} & & 2^{2} & \ldots & 2^{m} & \ldots & n & \ldots & 2^{m+1} & \ldots \\
  2^{0} & {\cellcolor{orange}1} & 0 & 0 & \ldots & \ldots & 0 & 0 & \ldots & 0 & \ldots & \ldots \\
  2^{1} & 0 & {\cellcolor{orange}1} & {\cellcolor{orange}1} & \ldots & \ldots & 0 & 0 & \ldots & 0 & \ldots & \ldots \\
  & 0 & {\cellcolor{orange}1} & {\cellcolor{orange}-1} & \ldots & \ldots & 0 & 0 & \ldots & 0 & \ldots & \ldots \\
  2^{2} & \vdots & \vdots & \vdots & \ddots & & \vdots & \vdots & & \vdots & \\
  \vdots & \vdots & \vdots & \vdots & & \ddots & \vdots & \vdots & & \vdots & \\
  2^{m} & 0 & 0 & 0 & \ldots & \ldots & {\cellcolor{orange}\x} & {\cellcolor{orange}\x} & {\cellcolor{orange}\ldots} & {\cellcolor{orange}\x} & \ldots & \ldots \\
  \vdots & 0 & 0 & 0 & \ldots & \ldots & {\cellcolor{orange}\x} & {\cellcolor{orange}\x} & {\cellcolor{orange}\ldots} & {\cellcolor{orange}\x} & \ldots & \ldots \\
  k & \vdots & \vdots & \vdots & &        & {\cellcolor{orange}\vdots} & {\cellcolor{orange}\vdots} & {\cellcolor{orange}\ddots} & {\cellcolor{orange}\vdots} & \\
  \vdots & 0 & 0 & 0 & \ldots & \ldots & {\cellcolor{orange}\x} & {\cellcolor{orange}\x} & {\cellcolor{orange}\ldots} & {\cellcolor{orange}\x} & \ldots & \ldots \\
  2^{m+1} & \vdots & \vdots & \vdots & & & \vdots & \vdots & \vdots & \vdots & \ddots & \\
  \vdots & \vdots & \vdots & \vdots & & & \vdots & \vdots & \vdots & \vdots & & \ddots
  }
\end{equation*}

\end{frame}

\end{document}

答案1

如果您喜欢使用kbordermatrix,那么您将面临一些矩阵单元着色的问题。首先,它\cellcolor不起作用,因为它是为表格设计的;其次,可能的解决方案是使用colorbox,但结果并不令人满意:

在此处输入图片描述

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

\usepackage[table]{xcolor}
\usepackage{kbordermatrix}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{amsmath}
\newcommand\x{$\times$}
\newcommand\CB[1]{\colorbox{orange!30}{#1}}

\begin{document}

\begin{frame}

\[
\kbordermatrix{
        & 2^{0}  & 2^{1}  &         & 2^{2}  & \ldots & 2^{m}   & \ldots & n      & \ldots & 2^{m+1} & \ldots \\
2^{0}   & \CB{1} & 0      & 0       & \ldots & \ldots & 0       & 0      & \ldots & 0      & \ldots  & \ldots \\
2^{1}   & 0      & \CB{1} & \CB{1}  & \ldots & \ldots & 0       & 0      & \ldots & 0      & \ldots  & \ldots \\
        & 0      & \CB{1} & \CB{-1} & \ldots & \ldots & 0       & 0      & \ldots & 0      & \ldots  & \ldots \\
2^{2}   & \vdots & \vdots & \vdots  & \ddots &        & \vdots  & \vdots &        & \vdots & \\
\vdots  & \vdots & \vdots & \vdots  &        & \ddots & \vdots  & \vdots &        & \vdots & \\
2^{m}   & 0      & 0      & 0       & \ldots & \ldots & \CB{\x} &\CB{\x} & \CB{$\ldots$} 
                                                                                  &\CB{\x} & \ldots & \ldots  \\
\vdots  & 0      & 0      & 0       & \ldots & \ldots & \CB{\x} &\CB{\x} & \CB{$\ldots$} 
                                                                                  &\CB{\x} & \ldots & \ldots  \\
k       & \vdots & \vdots & \vdots  &        &        & \CB{$\vdots$} 
                                                                &\CB{$\vdots$} 
                                                                         &\CB{$\ddots$} 
                                                                                  & \CB{$\vdots$}     &         \\
\vdots  & 0      & 0      & 0       & \ldots & \ldots & \CB{\x} &\CB{\x} &\CB{$\ldots$} 
                                                                                  &\CB{\x} & \ldots & \ldots  \\
2^{m+1} & \vdots & \vdots & \vdots  &        &        & \vdots  & \vdots & \vdots & \vdots & \ddots &         \\
\vdots  & \vdots & \vdots & \vdots  &        &        & \vdots  & \vdots & \vdots & \vdots &        & \ddots
  }
\]

\end{frame}

\end{document}

编辑: 为了获得更好的结果,您应该遵循我在回答您上一个问题时给出的建议:

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

\usepackage{tikz}
\usetikzlibrary{fit,
                matrix,
                positioning}
\tikzset{baseline = (current bounding box.center),
node distance= 1mm and 2mm,
    M/.style = {matrix of math nodes,
                nodes in empty cells,
                nodes={minimum size=2em, inner sep=4pt, anchor=center},
                column sep=2pt, row sep=2pt,
                inner xsep=2pt, inner ysep=-2pt
                },
   CB/.style = {fill=orange!50},
  lbl/.style = {text=gray, left=of #1}
        }
\newcommand\svd{\raisebox{-2ex}{$\smash\vdots$}}
\newcommand\sdd{\raisebox{-2ex}{$\smash\ddots$}}
\newcommand\x{\times}

\begin{document}

\begin{frame}[fragile]
\[
\mathbf{D} = \tikz{
\matrix (n) [M, left delimiter={[},right delimiter={]}]
{
|[CB]| 1     
      & 0        & 0        & \ldots        & 0            & 0             & \ldots        & 0      & \ldots  & \ldots  \\
0     & |[CB]| 1 & |[CB]| 1 & \ldots        & 0            & 0             & \ldots        & 0      & \ldots  & \ldots  \\
0     & |[CB]| 1 & |[CB]| -1& \ldots        & 0            & 0             & \ldots        & 0      & \ldots  & \ldots  \\
\svd  & \sdd     & \svd     & |[CB]| \svd   & |[CB]| \svd  & |[CB]| \svd   & |[CB]| \svd   & \svd   & \ldots  & \ldots  \\
0     & 0        & 0        & |[CB]| \ldots & |[CB]| \x    & |[CB]| \x     & |[CB]| \ldots & \x     & \ldots  & \ldots  \\
0     & 0        & 0        & |[CB]| \ldots & |[CB]| \x    & |[CB]| \x     & |[CB]| \ldots & \x     & \ldots  & \ldots  \\
\svd  & \svd     &          & |[CB]| \svd   & |[CB]| \svd  & |[CB]| \sdd   & |[CB]| \svd   &        &         &         \\
0     & 0        & 0        & \ldots        & \x           & \x            & \ldots        & \x     & \ldots  & \ldots  \\
\svd  & \svd     & \svd     &               & \svd         & \svd          & \svd          & \svd   & \sdd    &         \\
\svd  & \svd     & \svd     &               & \svd         & \svd          & \svd          & \svd   &         & \sdd    \\
};
\node[lbl=n-1-1.west] {$2^{0}$};
\node[lbl=n-2-1] {$2^{1}$}; 
\node[lbl=n-3-1] {$2^{2}$}; 
\node[lbl=n-4-1] {$\svd$}; 
\node[lbl=n-5-1] {$2^{m}$}; 
\node[lbl=n-6-1] {$\svd$};
\node[lbl=n-7-1] {$k$};      
\node[lbl=n-8-1] {$\svd$}; 
\node[lbl=n-9-1] {$2^{m+1}$}; 
\node[lbl=n-10-1] {$\svd$};  
%
   }% end of tikz
\]
\end{frame}

\end{document}

在此处输入图片描述

答案2

{bNiceMatrix}nicematrix.

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

\usepackage{xcolor}
\usepackage{nicematrix}



\newcommand\x{\times}
\newcommand\CB[1]{\Block[fill=orange!30,draw=white,line-width=1pt]{}{#1}}

\begin{document}

\begin{frame}

\setcounter{MaxMatrixCols}{12}
\renewcommand{\arraystretch}{1.2}

\[
\begin{bNiceMatrix}[first-row,first-col,margin]
        & 2^{0}  & 2^{1}  &         & 2^{2}  & \ldots & 2^{m}   & \ldots & n      & \ldots & 2^{m+1} & \ldots \\
2^{0}   & \CB{1} & 0      & 0       & \ldots & \ldots & 0       & 0      & \ldots & 0      & \ldots  & \ldots \\
2^{1}   & 0      & \CB{1} & \CB{1}  & \ldots & \ldots & 0       & 0      & \ldots & 0      & \ldots  & \ldots \\
        & 0      & \CB{1} & \CB{-1} & \ldots & \ldots & 0       & 0      & \ldots & 0      & \ldots  & \ldots \\
2^{2}   & \vdots & \vdots & \vdots  & \ddots &        & \vdots  & \vdots &        & \vdots & \\
\vdots  & \vdots & \vdots & \vdots  &        & \ddots & \vdots  & \vdots &        & \vdots & \\
2^{m}   & 0      & 0      & 0       & \ldots & \ldots & \CB{\x} &\CB{\x} & \CB{\ldots} 
                                                                                  &\CB{\x} & \ldots & \ldots  \\
\vdots  & 0      & 0      & 0       & \ldots & \ldots & \CB{\x} &\CB{\x} & \CB{\ldots} 
                                                                                  &\CB{\x} & \ldots & \ldots  \\
k       & \vdots & \vdots & \vdots  &        &        & \CB{\vdots} 
                                                                &\CB{\vdots} 
                                                                         &\CB{\ddots} 
                                                                                  & \CB{\vdots}     &         \\
\vdots  & 0      & 0      & 0       & \ldots & \ldots & \CB{\x} &\CB{\x} &\CB{\ldots} 
                                                                                  &\CB{\x} & \ldots & \ldots  \\
2^{m+1} & \vdots & \vdots & \vdots  &        &        & \vdots  & \vdots & \vdots & \vdots & \ddots &         \\
\vdots  & \vdots & \vdots & \vdots  &        &        & \vdots  & \vdots & \vdots & \vdots &        & \ddots
\end{bNiceMatrix}
\]

\end{frame}

\end{document}

上述代码的输出

相关内容