[Tex/LaTex] How to update an existing node

beamertikz-pgf

I'm drawing a node and after a \pause I'd like to add a label to the node. What I have so far simply repaints the node, but I hope that there is a nicer way:

\tikzstyle{vertex}=[circle,fill=black!25,minimum size=20pt,inner sep=0pt]
\begin{figure}
 \begin{tikzpicture}[scale=1.8, auto,swap]
     \node[vertex] (v_1) at (1,2) {$v_1$};
     \pause
     \node[vertex,label={[color=blue]80:$12$}] (v_1) at (1,2) {$v_1$}; %here I have to give name, node text and position again...
\end{tikzpicture}
\end{figure}    

Best Answer

There are two ways to do it.

Method 1

You can use beamer's overlay commands such as \visible

\node[vertex,label={[color=blue]80:{{\visible<2>{$12$}}}}] (v_1) at (1,2) {$v_1$};

Note that an extra pair of braces {} is needed around \visible with this method. See explanation here.

Method 2

You can define a key, say visible on, as suggested in this answer, and use it in the label for your node:

\node[vertex,label={[color=blue,visible on=<2>]80:$12$}] (v_1) at (1,2) {$v_1$};

where visible on is defined as

\tikzset{
  invisible/.style={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
  },
}

By the way, when defining a TikZ style, it's better to use \tikzset rather than the deprecated \tikzstyle. So your style vertex can be defined as

\tikzset{vertex/.style={circle,fill=black!25,minimum size=20pt,inner sep=0pt}}