[Tex/LaTex] Beamer overlay specifications for a tikzpicture

beameroverlaystikz-pgf

Overlay specifications can be added to the TikZ commands.

\node<2> (a) {only visible on slide 2};

Is this also possible for the whole tikzpicture environment?

\begin{tikzpicture}[<2>]
  \node (a) {only visible on slide 2};
  \node (b) {also only visible on slide 2};
\end{tikzpicture}

This can be emulated by enclosing the environment in \only<2>{ ... } but this is impossible when the tikzpicture is used in a \newenvironment.

Best Answer

As usual, these problems can be easily solved thanks to the style visible on defined by Daniel (see Mindmap tikzpicture in beamer (reveal step by step)).

Moreover, it works without problems with custom environments. Demonstration:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage{tikz}

 \tikzset{
    invisible/.style={opacity=0,text opacity=0},
    visible on/.style={alt={#1{}{invisible}}},
    alt/.code args={<#1>#2#3}{%
      \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
    },
  }

\newenvironment{mytikz}[1][]{\begin{tikzpicture}[#1]}{\end{tikzpicture}} 

\begin{document}
\begin{frame}
\begin{tikzpicture}[visible on=<2->]
  \node at (0,0) (a) {only visible on slide 2};
  \node at (2,2) (b) {also only visible on slide 2};
\end{tikzpicture}

Some text here:
\begin{itemize}
\item one
\item two
\end{itemize}

\begin{center}
\begin{mytikz}[visible on=<3>]
 \draw[top color=orange,bottom color=magenta!80!purple]
 (0,0) rectangle(2,1);
 \draw[top color=magenta!80!purple,bottom color=green,radius=0.5]
 (4,0.5) circle ;
\end{mytikz}
\end{center}
\end{frame}
\end{document}

The result:

enter image description here

Related Question