TikZ-PGF – Solving TikZ Graph with Disappearing Edges Issue in Beamer

beamergraphstikz-pgf

I have a graph on a frame in beamer, drawn with tikz and I would like to dismantle it after time.
The original graph looks like this:

\begin{frame}{Eksempel}
\begin{tikzpicture}[node distance=1.45cm, thick,
                      main node/.style={circle, draw, font=\sffamily\bfseries}]

    \node[main node] (1)                    {1};
    \node[main node] (3) [below left  of=1] {3};
    \node[main node] (4) [below right of=1] {4};
    \node[main node] (2) [above right of=4] {2};
    \node[main node] (6) [below right of=4] {6};

     %\draw[->, visible on=<2->] (1) -- (2);
     \path[->]
    (1) edge (2)
    (4) edge (2)
    (6) edge (2)
    (1) edge (3)
    (4) edge (1)
    (3) edge (4);
  \end{tikzpicture}
\end{frame}

I want to make edges disappear and after the edges disappear, the nodes not having more edges get green. For example, on the next slide, all edges to node 2 should disappear, and on the next one, the node 2 itself should disappear.

I have tried stuff like visible on or onslide, but all of them give me error messages. Also, one could maybe use \pause, but this is quite weird if I dismantle a graph instead of putting stuff into it.

Best Answer

This could serve as a starting point.

(With the help of https://tex.stackexchange.com/a/6155/263192)

enter image description here

\documentclass{beamer}
\usepackage{tikz}

\tikzset{onslide/.code args={<#1>#2}{% from https://tex.stackexchange.com/a/6155/263192
  \only<#1>{\pgfkeysalso{#2}}
}}

\begin{document}
\begin{frame}{Eksempel}
\begin{tikzpicture}[
    node distance=1.45cm, thick,
    main node/.style={circle, draw, font=\sffamily\bfseries}
]
    \node[main node] (1)                    {1};
    \node[main node] (3) [below left  of=1] {3};
    \node[main node] (4) [below right of=1] {4};
    \node<-3>[main node,onslide=<3>{green}] (2) [above right of=4] {2};
    \node<-4>[main node] (6) [below right of=4] {6}; % <-4> forces an additional overlay in which node 2 disappears

    \path<1>[->] (1) edge (2)
        (4) edge (2)
        (6) edge (2);
    \path[->] (1) edge (3)
        (4) edge (1)
        (3) edge (4);
\end{tikzpicture}
\end{frame}
\end{document}
Related Question