4

I use the code below.

\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}

The effect of the code is the following. enter image description here

enter image description here

And we can see that the light-blue backgrounds appear in the first slide. However, they should be in the second slide. But how to set?

daleif
  • 54,450
Wieshawn
  • 273

1 Answers1

1

The problem it's probably related with how beamer uses background layers and I don't know how to solve it. But for this particular case, it's easy to find a workaround because the graph can be drawn as a matrix which avoid us from using fit nodes and background layers.

A TikZ matrix is a node which contains several inner nodes and can be fill-ed like any regular node. This container node it's on same layer than its contents and it's not necessary to put on background layer like fit nodes.

With matrix nodes you don't need shifted scopes because positioning options can be used. label has been used for captions.

\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=&amp;, column sep=.3cm, row sep=1cm} ]

\uncover<1-2>{ \matrix (A) [mymatrix, fill=black!20, label=below:(a) Original Graph] {|(1)|1 &amp; &amp; |(2)|2 &amp; &amp; |(3)|3\ &amp; |(4)|4 &amp; &amp; |(5)|5\ |(6)|6 &amp; &amp; |(7)|7 &amp; &amp; |(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] {&amp; |(21)|2 &amp; &amp; |(31)|3\ |(41)|4 &amp; &amp; |(51)|5\ &amp; |(71)|7 &amp; &amp; |(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 \ &amp; |(42)|4 &amp; &amp; |(52)|5\ |(62)|6 &amp; &amp; |(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}

enter image description here

A more general solution can be obtained applying Daniel's visible on/.style from Mindmap tikzpicture in beamer (reveal step by step).

Ignasi
  • 136,588