8

Possible Duplicate:
How to make beamer overlays with Tikz node

I'd like to draw a figure in TikZ for a beamer presentation, and color various nodes differently in different overlays of the same frame. I am able to use \only<> to put nodes and paths only in some overlays, using \only<>, but I can't seem to change styles using it.

Is it possible to change styles for different overlays? Can you think of a another way to color the nodes?

An example:

\documentclass{beamer}
\usepackage{pgf}
\usepackage{tikz}
\begin{document}
\begin{frame}{Frame}
  \begin{tikzpicture}
    \path (0,0) node[draw,rectangle] { 1 } --
          (2,0) node[draw,rectangle] { 2 } --
          (4,0) node[draw,rectangle] { 3 };
  \end{tikzpicture}
\end{frame}
\end{document}

Suppose I want to make the first node fill color become gray from the second overlay on, and the second node to become gray on the next overlay, and so on. How would you recommend doing it?

3
  • 2
    When reading your question, Related questions appear on the right side. The first one is How to make beamer overlays with Tikz node. Did you look at it?
    – Ignasi
    Feb 13, 2012 at 22:06
  • I'm afraid I haven't. I posted my question accidentally on stack overflow, and it didn't show up there. The answers to the question you linked look exactly like what I wanted.
    – haggai_e
    Feb 14, 2012 at 6:00
  • I think this one should be closed as exact duplicate. The question Ingasi refers to covers all that is asked for; the answer by Matthew to this other question appears to be the perfect answer for this particular problem.
    – Daniel
    Feb 14, 2012 at 9:56

1 Answer 1

11

This is sort of annoying, since although the options are expanded, putting an \only in them will break; presumably, it contains some kind of non-expandable assignment. Therefore, it appears that the easiest solution is to define macros containing (or not) a color that you'd like to "uncover" on your nodes, and then conditionally redefine them using \only outside the options. To wit:

\documentclass{beamer}
%%% Unnecessary; tikz loads it
% \usepackage{pgf} 
\usepackage{tikz}
\begin{document}
\begin{frame}{Frame}
 \newcommand*\nodeonecolor{}
 \newcommand*\nodetwocolor{}
 \newcommand*\nodethreecolor{}
 \only<2->{\renewcommand*\nodeonecolor{red}}
 \only<3->{\renewcommand*\nodetwocolor{green}}
 \only<4->{\renewcommand*\nodethreecolor{blue}}
  \begin{tikzpicture}
    \path (0,0) node[draw,rectangle,\nodeonecolor] { 1 } --
          (2,0) node[draw,rectangle,\nodetwocolor] { 2 } --
          (4,0) node[draw,rectangle,\nodethreecolor] { 3 };
  \end{tikzpicture}
\end{frame}
\end{document}

See, \nodeonecolor (and others) starts empty, so the boxes are uncolored (black). Then, on slides 2, 3, and 4, these macros are defined to be other colors, so the node options are different even though the code itself is the same.

4
  • 2
    That said, the answer in the other question is much better and I'm embarrassed not to be promoting a pgfkeys solution.
    – Ryan Reich
    Feb 14, 2012 at 2:04
  • 1
    I knew you'd be a fan of that. That's one of my personal favorite answers I've submitted here. Feb 14, 2012 at 2:53
  • It is definitely a Good Answer.
    – Ryan Reich
    Feb 14, 2012 at 4:37
  • Or a "Nice Answer" in the SE parlance. Feb 15, 2012 at 14:16

Not the answer you're looking for? Browse other questions tagged .