我使用下面的代码。
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning,fit}
\begin{document}
\begin{frame}
\begin{figure}
\begin{tikzpicture}[scale=0.5]
\tikzstyle{vv}=[semithick, draw, circle, inner sep=0.5pt]
\tikzstyle{bl}=[xshift=0.6cm]
\tikzstyle{br}=[xshift=-0.6cm]
\uncover<1-2>{
\node [vv] (1) at (0,0) {1};
\node [vv] (2) [right=of 1] {2};
\node [vv] (3) [right=of 2] {3};
\node [vv] (5) [below left=of 3, bl] {5};
\node [vv] (4) [below right=of 1, br] {4};
\node [vv] (6) [below left=of 4, bl] {6};
\node [vv] (7) [right=of 6] {7};
\node [vv] (8) [right=of 7] {8};
\draw [semithick] (1)--(4)--(2)--(5)--(3);
\draw [semithick] (1)--(5)--(6);
\draw [semithick] (6)--(4)--(7)--(5)--(8);
\begin{scope}[on background layer]
\node (b0) [fill=black!20,rounded corners,fit=(1)(2)(3)(4)(5)(6)(7)(8), inner sep=-1.3cm] {};
\end{scope}
\node (original) [below=of 7, yshift=5mm] {(a) Original Graph};
}
\uncover<2>{
\begin{scope}[xshift=10cm]
\node [vv] (21) at (0,0) {2};
\node [vv] (31) [right=of 21] {3};
\node [vv] (51) [below left=of 31, bl] {5};
\node [vv] (41) [below left=of 21, bl] {4};
\node [vv] (71) [below left=of 51, bl] {7};
\node [vv] (81) [below right=of 51, br] {8};
\draw [semithick] (41)--(21)--(51)--(31);
\draw [semithick] (71)--(51)--(81);
\end{scope}
\begin{scope}[on background layer]
\node (b1) [fill=blue!20,rounded corners,fit=(21)(31)(41)(51)(71)(81), inner sep=-1.1cm] {};
\end{scope}
\node (sub1) [below=of 71, yshift=5mm] {~~~~~(b) Subgraph 1};
}
\uncover<2>{
\begin{scope}[xshift=15.5cm]
\node [vv] (12) at (0,0) {1};
\node [vv] (42) [below right=of 12, br] {4};
\node [vv] (52) [right=of 42] {5};
\node [vv] (62) [below left=of 42, bl] {6};
\node [vv] (72) [right=of 62] {7};
\draw [semithick] (62)--(42)--(12)--(52)--(62);
\draw [semithick] (42)--(72);
\end{scope}
\begin{scope}[on background layer]
\node (b2) [fill=blue!20,rounded corners,fit=(12)(42)(52)(62)(72), inner sep=-1.1cm] {};
\end{scope}
\node (sub2) [below=of 72, yshift=5mm] {(c) Subgraph 2~~~~~};
}
\uncover<2>{
\path (b0) to node {=} (b1);
\path (b1) to node {+} (b2);
}
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}
代码效果如下。
我们看到第一张幻灯片中出现了淡蓝色背景,但是第二张幻灯片中应该有淡蓝色背景,该如何设置呢?
答案1
这个问题可能与如何beamer
使用background
层有关,我不知道如何解决它。但对于这个特殊情况,很容易找到解决方法,因为可以将图绘制为这样matrix
可以避免我们使用fit
节点和background
层。
ATikZ matrix
是node
包含多个内部节点的节点,可以fill
像任何常规节点一样进行操作。此容器节点与其内容位于同一层,因此无需background
像节点一样放置在同一层fit
。
使用节点时matrix
您不需要移动,scopes
因为positioning
可以使用选项。label
已用于标题。
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning,fit,matrix}
\begin{document}
\begin{frame}
\begin{figure}
\begin{tikzpicture}[%
scale=0.5,
vv/.style={semithick, draw, circle, inner sep=0.5pt},
bl/.style={xshift=0.6cm},
br/.style={xshift=-0.6cm},
mymatrix/.style={matrix of nodes, nodes={vv}, rounded corners,
ampersand replacement=\&, column sep=.3cm, row sep=1cm}
]
\uncover<1-2>{
\matrix (A) [mymatrix, fill=black!20, label=below:(a) Original Graph]
{|(1)|1 \& \& |(2)|2 \& \& |(3)|3\\
\& |(4)|4 \& \& |(5)|5\\
|(6)|6 \& \& |(7)|7 \& \& |(8)|8\\
};
\draw [semithick] (1)--(4)--(2)--(5)--(3);
\draw [semithick] (1)--(5)--(6);
\draw [semithick] (6)--(4)--(7)--(5)--(8);
}
\uncover<2>{
\matrix (B) [mymatrix, fill=blue!20, label=below:(b) Subgraph 1, right=of A]
{\& |(21)|2 \& \& |(31)|3\\
|(41)|4 \& \& |(51)|5\\
\& |(71)|7 \& \& |(81)|8\\
};
\draw [semithick] (41)--(21)--(51)--(31);
\draw [semithick] (71)--(51)--(81);
}
\uncover<2>{
\matrix (C) [mymatrix, fill=blue!20, label=below:(c) Subgraph 2, right=of B]
{|(12)|1 \\
\& |(42)|4 \& \& |(52)|5\\
|(62)|6 \& \& |(72)|7 \\
};
\draw [semithick] (62)--(42)--(12)--(52)--(62);
\draw [semithick] (42)--(72);
}
\uncover<2>{
\path (A) to node {=} (B);
\path (B) to node {+} (C);
}
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}
可以得到更通用的解决方案丹尼尔的visible on/.style
从思维导图 tikzpicture 在 beamer 中 (逐步显示)。