7

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}    
mort
  • 1,671

2 Answers2

8

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}}
Herr K.
  • 17,946
  • 4
  • 61
  • 118
7

If you want to use native TikZ/beamer syntax, use the late options.

Code

\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}
 \begin{tikzpicture}[scale=1.8, auto, swap, vertex/.style={circle, fill=black!25, minimum size=20pt, inner sep=0pt}]
     \node[vertex] (v_1) at (1,2) {$v_1$};
     \pause
     \path [late options={name=v_1,label={[color=blue]80:$12$}}];
     % or: \path (v_1) [late options={label={[color=blue]80:$12$}}];
  \end{tikzpicture}
\end{frame}
\end{document}
Qrrbrbirlbel
  • 119,821