我有一个用一些颜色初始化但没有值的矩阵。在 beamer 叠加的帮助下,我想在下一步修改矩阵,更改一些颜色并插入一些值。我附上了一个图,这样您就可以清楚地了解我想要实现的目标(顶部是空矩阵,底部是填充后的矩阵):
我试图用新的矩阵覆盖整个矩阵,但这存在几个问题:
- 诸如“某些标签”之类的文本在第二张幻灯片中被覆盖并且看起来更粗。
- 如果我需要执行 N 步,而不是仅仅 2 步,那么每次重写整个矩阵确实很繁琐。
我知道我并不真正了解 TikZ 和 Beamer 中的所有可能性,所以可能有一些我遗漏的东西,可以让我做我想做的事情。有人知道我可以做什么来更新叠加步骤之间的矩阵节点吗?
答案1
这是一个非常简单的方法(灵感来自我自己对在 TikZ 中有条件地选择样式)。
结果
代码
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{frame}[fragile]
\frametitle{Progressive matrix}
\tikzset{
% some styles for nodes
orange node/.style={draw,fill=orange!50,text=black},
gray node/.style={draw,fill=gray!50,text=black},
gray empty node/.style={draw,fill=gray!50,text=gray!50},
red node/.style={draw,fill=red!50,text=blue},
% styles for each overlay
my styles 1/.style={
nodes={gray empty node},
},
my styles 2/.style={
nodes={gray empty node},
column 1/.style={nodes=gray node},
},
my styles 3/.style={
nodes={orange node},
column 1/.style={nodes=gray node},
},
my styles 4/.style={
nodes={orange node},
column 1/.style={nodes=gray node},
row 2 column 2/.style={nodes=red node},
},
}
% apply overlay's styles
\only<1>{\tikzset{my styles/.style={my styles 1}}}
\only<2>{\tikzset{my styles/.style={my styles 2}}}
\only<3>{\tikzset{my styles/.style={my styles 3}}}
\only<4>{\tikzset{my styles/.style={my styles 4}}}
\centering
\begin{tikzpicture}
\matrix[matrix of nodes,
nodes={minimum width=2cm,minimum height=2cm,font=\bfseries},
column sep=-\pgflinewidth,row sep=-\pgflinewidth,my styles]{
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
};
\end{tikzpicture}
\end{frame}
\end{document}
答案2
这是一个使用逗号分隔列表的可能解决方案;该列表\elist
允许您指定矩阵的条目和单元格的填充颜色;您分别调用\entries
和\centries
来填充矩阵并填充单元格:
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}
\definecolor{myora}{RGB}{235,97,61}
\newcounter{entry}
\newcounter{tmp}
\loop\ifnum\value{entry}<9
\stepcounter{entry}
\expandafter\def\csname entry\romannumeral\value{entry}\endcsname{}
\repeat
\setcounter{entry}{0}
\loop\ifnum\value{entry}<9
\stepcounter{entry}
\expandafter\def\csname centry\romannumeral\value{entry}\endcsname{}
\repeat
\makeatletter
\def\entries{%
\setcounter{tmp}{0}
\@for\temp@a:=\elist\do
{\stepcounter{tmp}
\expandafter\edef\csname entry\romannumeral\value{tmp}\endcsname{\temp@a}
}
}
\def\centries{%
\setcounter{tmp}{0}
\@for\temp@a:=\elist\do
{\stepcounter{tmp}
\expandafter\edef\csname centry\romannumeral\value{tmp}\endcsname{\temp@a}
}
}
\makeatother
\def\elist{\noexpand\phantom{1},\noexpand\phantom{1},\noexpand\phantom{1},\noexpand\phantom{1},\noexpand\phantom{1},\noexpand\phantom{1},\noexpand\phantom{1},\noexpand\phantom{1},\noexpand\phantom{1}}\entries
\begin{document}
\begin{frame}
\only<1>{
\def\elist{gray!50,gray!50,gray!50,gray!50,gray!50,gray!50,gray!50,gray!50,gray!50}\centries
}
\only<2>{
\def\elist{3,6,9,12,15,18,21,24,27}\entries
\def\elist{gray!50,myora,myora,gray!50,myora,myora,gray!50,myora,myora}\centries
}
\only<3>{
\def\elist{2,4,6,8,10,12,14,16,18}\entries
\def\elist{myora,gray!50,myora,myora,gray!50,myora,myora,gray!50,myora}\centries
}
\only<4>{
\def\elist{-1,-2,-3,-4,-5,-6,-7,-8,-9}\entries
\def\elist{myora,myora,gray!50,myora,myora,gray!50,myora,myora,gray!50}\centries
}
\only<5>{
\def\elist{-3,-6,-9,-12,-15,-18,-21,-24,-27}\entries
\def\elist{myora,gray!50,myora,gray!50,myora,gray!50,myora,gray!50,myora}\centries
}
\only<6>{
\def\elist{1,0,1,0,1,0,1,0,1}\entries
\def\elist{gray!50,myora,gray!50,myora,gray!50,myora,gray!50,myora,gray!50}\centries
}
\centering
\begin{tikzpicture}
\matrix[matrix of nodes,every node/.style={draw,text width=2cm,align=center},ampersand replacement=\&,nodes in empty cells] (mat)
{
|[fill=\centryi]|\entryi \& |[fill=\centryii]|\entryii \& |[fill=\centryiii]|\entryiii \\
|[fill=\centryiv]|\entryiv \& |[fill=\centryv]|\entryv \& |[fill=\centryvi]|\entryvi \\
|[fill=\centryvii]|\entryvii \& |[fill=\centryviii]|\entryviii \& |[fill=\centryix]|\entryix \\
};
\node[anchor=south] at (mat.north) {Some Label};
\end{tikzpicture}\par
\end{frame}
\end{document}
生成的文档的动画: