9

As a follow-up question to Tikz animated figure in Beamer, how can I animate a particular attribute of a node?

MWE

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{graphs}

\tikzset{
  node distance = 3 cm,
  graph vertex/.style={
    circle,
    draw,
    minimum size=7.5mm,
  },
  marked/.style={
    color=white,
    fill=black,
  },
}

\begin{document}
\begin{frame}
  \centering
    \begin{tikzpicture}
      \node[graph vertex, marked] (1) at (0 , 3) {$v_1$};
      \node[graph vertex,       ] (2) at (0 , 6) {$v_2$};
      \node[graph vertex,       ] (3) at (3 , 3) {$v_3$};
      \node[graph vertex, marked] (4) at (3 , 6) {$v_4$};
      \node[graph vertex,       ] (5) at (5 , 8) {$v_5$};

      \graph {
        (1) -- (3) -- (4) -- (2) --[bend left] (5),
        (1) -- (2),
        (1) -- (4),
        (3) --[bend right] (5)
      };
  \end{tikzpicture}
\end{frame}

\begin{frame}
  \centering
  \begin{tikzpicture}
      \node[graph vertex, marked] (1) at (0 , 3) {$v_1$};
      \node[graph vertex,       ] (2) at (0 , 6) {$v_2$};
      \node[graph vertex,       ] (3) at (3 , 3) {$v_3$};
      \node[graph vertex, marked] (4) at (3 , 6) {$v_4$};
      \node[graph vertex, marked] (5) at (5 , 8) {$v_5$};

      \graph {
        (1) -- (3) -- (4) -- (2) --[bend left] (5),
        (1) -- (2),
        (1) -- (4),
        (3) --[bend right] (5)
      };
  \end{tikzpicture}
\end{frame}

\begin{frame}
  \centering
  \begin{tikzpicture}
      \node[graph vertex, marked] (1) at (0 , 3) {$v_1$};
      \node[graph vertex,       ] (2) at (0 , 6) {$v_2$};
      \node[graph vertex,       ] (3) at (3 , 3) {$v_3$};
      \node[graph vertex,       ] (4) at (3 , 6) {$v_4$};
      \node[graph vertex, marked] (5) at (5 , 8) {$v_5$};

      \graph {
        (1) -- (3) -- (4) -- (2) --[bend left] (5),
        (1) -- (2),
        (1) -- (4),
        (3) --[bend right] (5)
      };
  \end{tikzpicture}
\end{frame}
\end{document}

I'd like to get this syntax to something like

\begin{frame}
  \centering
    \begin{tikzpicture}
      \node[graph vertex, marked      ] (1) at (0 , 3) {$v_1$};
      \node[graph vertex,             ] (2) at (0 , 6) {$v_2$};
      \node[graph vertex,             ] (3) at (3 , 3) {$v_3$};
      \node[graph vertex, marked=<1-2>] (4) at (3 , 6) {$v_4$};
      \node[graph vertex, marked=<2-3>] (5) at (5 , 8) {$v_5$};

      \graph {
        (1) -- (3) -- (4) -- (2) --[bend left] (5),
        (1) -- (2),
        (1) -- (4),
        (3) --[bend right] (5)
      };
  \end{tikzpicture}
\end{frame}

I'm open to other syntax options as well, as long as it 'more or less' adheres to the above (i.e. doesn't duplicate code, is readable).

Attempt with overlay-beamer-styles

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{graphs}
\usetikzlibrary{overlay-beamer-styles}

\tikzset{
  node distance = 3 cm,
  graph vertex/.style={
    circle,
    draw,
    minimum size=7.5mm,
  },
  marked/.style={
    color=white,
    fill=black,
  },
  marked on/.style={alt=#1{}{marked}},
}

\begin{document}
\begin{frame}
  \centering
    \begin{tikzpicture}
      \node[graph vertex, marked         ] (1) at (0 , 3) {$v_1$};
      \node[graph vertex,                ] (2) at (0 , 6) {$v_2$};
      \node[graph vertex,                ] (3) at (3 , 3) {$v_3$};
      \node[graph vertex, marked on=<1-2>] (4) at (3 , 6) {$v_4$};
      \node[graph vertex, marked on=<2-3>] (5) at (5 , 8) {$v_5$};

      \graph {
        (1) -- (3) -- (4) -- (2) --[bend left] (5),
        (1) -- (2),
        (1) -- (4),
        (3) --[bend right] (5)
      };
  \end{tikzpicture}
\end{frame}
\end{document}
  • You can have a look to the aobs-tikz package: it allows, for instance, to change shading, filling, or even make double the border of a node with the syntax you were proposing. – Claudio Fiandrino May 4 '14 at 17:01
  • @ClaudioFiandrino This looks promising, thanks for uploading it! I can't seem to get the syntax right though; I've edited-in a buggy attempt to get it working. – Sean Allred May 4 '14 at 17:08
  • unfortunately, at the moment I can't answer; I'll do it tomorrow. – Claudio Fiandrino May 4 '14 at 17:15
  • @ClaudioFiandrino Many thanks in advance :) – Sean Allred May 4 '14 at 17:16
7

Your code using overlay-beamer-styles will work if you change the code line

  marked on/.style={alt=#1{}{marked}},

to

  marked on/.style={alt=#1{marked}{}},

The code:

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

\tikzset{
  node distance = 3 cm,
  graph vertex/.style={
    circle,
    draw,
    minimum size=7.5mm,
  },
  marked/.style={
    color=white,
    fill=black,
  },
  marked on/.style={alt=#1{marked}{}},
}

\makeatletter
\def\c@slideinframe{\beamer@slideinframe}
\makeatother

\begin{document}
\begin{frame}
\frametitle{Test slide \arabic{slideinframe}}
  \centering
    \begin{tikzpicture}
      \node[graph vertex, marked         ] (1) at (0 , 3) {$v_1$};
      \node[graph vertex,                ] (2) at (0 , 6) {$v_2$};
      \node[graph vertex,                ] (3) at (3 , 3) {$v_3$};
      \node[graph vertex, marked on=<1-2>] (4) at (3 , 6) {$v_4$};
      \node[graph vertex, marked on=<2-3>] (5) at (5 , 8) {$v_5$};

      \graph {
        (1) -- (3) -- (4) -- (2) --[bend left] (5),
        (1) -- (2),
        (1) -- (4),
        (3) --[bend right] (5)
      };
  \end{tikzpicture}
\end{frame}
\end{document}

enter image description here

  • 1
    I might be going crazy, but what is the difference between the two snippets you first give? (Also, you should see tex.stackexchange.com/questions/175232) – Sean Allred May 4 '14 at 22:31
  • 1
    @SeanAllred In the first answer I accidentally pasted at the end a version I had designed without using Claudio's library (which I didn't know before this answer); then I removed that version since the code using the library simplifies. – Gonzalo Medina May 4 '14 at 22:45
  • 1
    +1: yesterday I couldn't post an answer (home laptop is not running yet TikZ 3.0.0 ;)), but this is even better that what I had in mind. It demonstrates how to create new styles, something I did not considered, but it has a potential to simplify a lot the code with respect to How to overlay tikz matrix in beamer? for example. – Claudio Fiandrino May 5 '14 at 6:15
  • @ClaudioFiandrino I didn't know your library before this post. Really nice! However, using something like \node[graph vertex, marked on=<2-3,5-7>] (5) at (5 , 8) {$v_5$}; in the example code will trigger an error: Runaway argument? \pgfkeys@mainstop \def \pgfkeysdefaultpath {/}\ifx \tikz@node@content \ETC. ! File ended while scanning use of \pgfkeys@code. <inserted text> \par <*> a.tex. Is not possible to give more complex overlay specifications? – Gonzalo Medina May 5 '14 at 18:02
  • 2
    @GonzaloMedina: it is, but I think you have to protect the argument with braces if there are commas (same issue I was asking in Addressing overlay specifications in TikZ-pictures. – Claudio Fiandrino May 5 '14 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.