# How to get pgfonlayer to work correctly inside onslide?

I am trying to use onslide to group different picture elements together and control when and how long they appear on the slide. At the same time I am trying to use pgfonlayer to make sure some things are drawn "underneath" other things to ensure the right occlusion.

I think they should be completely independent of each other, but I am finding that when I put elements into the pgfonlayer environment (which is inside the onslide), it seems to put all the items in the same pgf background layer into something equivalent to onslide<1->. Almost like the pgfonlayer cancels the effect of the onslide.

To be more precise I can have something like this (this is a sketch of the problem -- A,B,C,D are real graphics, not just letters):

\onslide<1>{
B
\begin{pgfonlayer}{bg}
A % A should be occluded by B
\end{pgfonlayer}
}

\onslide<2>{
D
\begin{pgfonlayer}{bg}
C % C should be occluded by D
\end{pgfonlayer}
}


and I get the effect that A and C are always visible.

Is there an easy way to fix this? Any help would be greatly appreciated.

Thanks,

Ambrose

ps. Here is a full source code example:

\documentclass[10pt]{beamer}
\title{onslide vs pgfonlayer}
\author[My Team]{My Name}
\date{\today}
\usepackage{tikz}
\begin{document}

\begin{frame}[t]
\frametitle{First using pgfonlayer only -- works.}

On this first slide the light colors are on top, even though they are
drawn before the darker colors.  {\bf pgfonlayer} is used to achieve
this.

\pgfdeclarelayer{bg}
\pgfsetlayers{bg,main}

\begin{figure}
\begin{tikzpicture}
\draw[fill=blue!10] (0,1) circle (1cm);
\begin{pgfonlayer}{bg}
\draw[fill=blue] (0,0) circle (1cm);
\end{pgfonlayer}
\draw[fill=red!10] (3,1) circle (1cm);
\begin{pgfonlayer}{bg}
\draw[fill=red] (3,0) circle (1cm);
\end{pgfonlayer}
\end{tikzpicture}
\end{figure}

\end{frame}

\begin{frame}[t]
On this second slide I attempt to use {\bf onslide} to show only one
side at a time.  First the blues then the reds.
\pgfdeclarelayer{bg}
\pgfsetlayers{bg,main}

\begin{figure}
\begin{tikzpicture}
\onslide<1>{
\draw[fill=blue!10] (0,1) circle (1cm);
\begin{pgfonlayer}{bg}
\draw[fill=blue] (0,0) circle (1cm);
\end{pgfonlayer}
}
\onslide<2>{
\draw[fill=red!10] (3,1) circle (1cm);
\begin{pgfonlayer}{bg}
\draw[fill=red] (3,0) circle (1cm);
\end{pgfonlayer}
}

\end{tikzpicture}
\end{figure}

First we see light blue with both dark colors, ...\pause and then we
see light red with both dark colors.  It seems that because I put the
darker colors into the bg layer, the onslide groupings I put around
each basic color do not work

\end{frame}
\end{document}

-
Thanks for the responses (both work nicely!). Just hope there is a long term fix because having to specify the same information (the onslide argument) in two places makes source files slightly more difficult to maintain. Thanks again!!! – Ambrose Aug 22 '12 at 18:12

You are right: the pgfonlayer environment cancels the effect of the \onslide!

A workaround:

\documentclass[10pt]{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}[t]
\frametitle{pgfonlayer and onslide...}
\pgfdeclarelayer{bg}
\pgfsetlayers{bg,main}
\begin{figure}
\begin{tikzpicture}
\onslide<1>{
\draw[fill=blue!10] (0,1) circle (1cm);
\begin{pgfonlayer}{bg}
\onslide<1>{
\draw[fill=blue] (0,0) circle (1cm);
}
\end{pgfonlayer}
}
\onslide<2>{
\draw[fill=red!10] (3,1) circle (1cm);
\begin{pgfonlayer}{bg}
\onslide<2>{
\draw[fill=red] (3,0) circle (1cm);
}
\end{pgfonlayer}
}
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}


-

I think this is one of those instances that beamer and TikZ miscommunicate. The easiest solution is to add specific overlay specs to the draw commands on the background layer which makes the overlay specification become an \only (if I'm not mistaken). So, it's deliberately instructed to remove what happened on that slide unlike \onslide.

I would strongly recommend using the backgrounds library of TikZ which is tailored just for this task. I don't know exactly all the things it does but it's best to let TikZ do the layering if it's just the background that we are interested in.

\documentclass[10pt]{beamer}
\title{onslide vs pgfonlayer}
\author[My Team]{My Name}
\date{\today}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}

\begin{frame}[t]{First using pgfonlayer only -- works.}

On this first slide the light colors are on top, even though they are
drawn before the darker colors.  \textbf{pgfonlayer} is used to achieve
this.

\begin{figure}
\begin{tikzpicture}
\draw[fill=blue!10] (0,1) circle (1cm);
\begin{scope}[on background layer]
\draw[fill=blue] (0,0) circle (1cm);
\end{scope}
\draw[fill=red!10] (3,1) circle (1cm);
\begin{scope}[on background layer]
\draw[fill=red] (3,0) circle (1cm);
\end{scope}
\end{tikzpicture}
\end{figure}

\end{frame}

On this second slide the light colors are on top, even though they are
drawn before the darker colors.  \textbf{pgfonlayer} is used to achieve
this.

\begin{figure}
\begin{tikzpicture}
\onslide<1>{
\draw[fill=blue!10] (0,1) circle (1cm);
\begin{scope}[on background layer]
\draw<1>[fill=blue] (0,0) circle (1cm);
\end{scope}
}
\onslide<2>{
\draw[fill=red!10] (3,1) circle (1cm);
\begin{scope}[on background layer]
\draw<2>[fill=red] (3,0) circle (1cm);
\end{scope}
}

\end{tikzpicture}
\end{figure}

First \pause and second
\end{frame}
\end{document}


A small detail: \bf is depracated, use \textbf for such use.

-