# Beamer/TikZ/PGF: Applying a key only for one slide (similar to uncover or only)

I want that a key that is only applied in specific slides/frames just like uncover or only. Here's an example:

\documentclass{beamer}
\usepackage{tikz}

\begin{document}

\begin{frame}

\begin{tikzpicture}
\draw [fill=orange] (0,0) rectangle (1,1);
\uncover<1>{\node at (2,0) [anchor=west] {1};}
\uncover<2>{\node at (2,0) [anchor=west] {2 -- should be only orange here};}
\uncover<3>{\node at (2,0) [anchor=west] {3};}
\end{tikzpicture}

\end{frame}

\end{document}


The output looks like this:

Problem

I want for example that the fill key is only applied to the second frame - something like this: fill=<2>orange.

I tried to understand this related question but the code of the accepted answer is too complicated for me. But I guess it has something to do with it.

Any ideas?

You can define a style that uses overlay specifications:

\documentclass{beamer}
\usepackage{tikz}

\tikzset{onslide/.code args={<#1>#2}{%
\only<#1>{\pgfkeysalso{#2}} % \pgfkeysalso doesn't change the path
}}

\begin{document}

\begin{frame}

\begin{tikzpicture}
\draw [onslide=<2>{fill=orange}] (0,0) rectangle (1,1);
\uncover<1>{\node at (2,0) [anchor=west] {1};}
\uncover<2>{\node at (2,0) [anchor=west] {2 -- should be only orange here};}
\uncover<3>{\node at (2,0) [anchor=west] {3};}
\end{tikzpicture}

\end{frame}

\end{document}


• Thanks. But why can't I just write \draw [\only<#2>{fill=orange}] (0,0) rectangle (1,1);´ – Dr. Manuel Kuehner Feb 9 '14 at 17:15
• @ManuelKuehner because the TikZ parser expects to see valid keys inside the brackets and \only is not a valid TikZ key; defining the style solves the problem, as now the onslide style is a valid key. – Gonzalo Medina Feb 9 '14 at 17:17
• Thanks again. So \only is valid when I put it in a style but not when I use it directly. Isn't that strange? – Dr. Manuel Kuehner Feb 9 '14 at 17:22
• @ManuelKuehner when you define a style using .code args, the definition you put in is actually (nearly) arbitrary LaTeX code, and thus you can use \only there. But the stuff you put in square brackets following \draw cannot just be anything. It has to be the name of a style. (A PGF key, technically.) – David Z Feb 10 '14 at 3:22

A particularly straightforward way to do this is to just use \tikzset inside \only:

\documentclass{beamer}
\usepackage{tikz}

\begin{document}
\begin{frame}
\begin{tikzpicture}
\only<2>{\tikzset{every path/.style={fill=orange}}}
\draw (0,0) rectangle (1,1);
\uncover<1>{\node at (2,0) [anchor=west] {1};}
\uncover<2>{\node at (2,0) [anchor=west] {2 -- should be only orange here};}
\uncover<3>{\node at (2,0) [anchor=west] {3};}
\end{tikzpicture}
\end{frame}
\end{document}


Alternatively, you could define an overlay-aware version of \tikzset:

\documentclass{beamer}
\usepackage{tikz}
\renewcommand<>\tikzset[1]{\only#2{\beameroriginal{\tikzset}{#1}}}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\tikzset<2>{every path/.style={fill=orange}}
\draw (0,0) rectangle (1,1);
\uncover<1>{\node at (2,0) [anchor=west] {1};}
\uncover<2>{\node at (2,0) [anchor=west] {2 -- should be only orange here};}
\uncover<3>{\node at (2,0) [anchor=west] {3};}
\end{tikzpicture}
\end{frame}
\end{document}


Both versions are functionally the same, but the latter is slightly more convenient if you have to use it a lot.

Another approach, using the aobs-tikz package that defines apposite TikZ styles for overlays:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{overlay-beamer-styles}

\begin{document}

\begin{frame}

\begin{tikzpicture}
\draw [background fill=orange,fill on=<2>] (0,0) rectangle (1,1);
\node[visible on=<1>] at (2,0) [anchor=west] {1};
\node[visible on=<2>]  at (2,0) [anchor=west] {2 -- should be only orange here};
\node[visible on=<3>]  at (2,0) [anchor=west] {3};
\end{tikzpicture}

\end{frame}

\end{document}
`

The result: