[Tex/LaTex] How to get pgfonlayer to work correctly inside onslide

beamertikz-pgf

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]
  \frametitle{Next, adding onslide -- fails.}
  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}

Best Answer

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}

enter image description here