beamer tikz add tag
topnush
I have a basic graph as follows:


```
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}


\begin{document}
\begin{frame}{Title}
\tikzstyle{vertex}=[very thick,draw=black, circle,minimum size=18pt,inner sep=0pt]
\tikzstyle{edge} = [draw,thick,-]

\begin{figure}
\begin{tikzpicture}[scale=1.8, auto,swap]
    % First we draw the vertices
    \foreach \pos/\name in {{(0,2)/a}, {(2,1)/b}, {(4,1)/c},
                            {(0,0)/d}, {(3,0)/e}, {(2,-1)/f}, {(4,-1)/g}}
        \node[vertex] (\name) at \pos {$\name$};
    % Connect vertices with edges and draw weights
    \foreach \source/ \dest  in {b/a, c/b,d/a,d/b,
                                         e/b, e/c,e/d,
                                         f/d,f/e,
                                         g/e,g/f}
        \path[edge] (\source) -- node {} (\dest);
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}`
```


I would like to start with the same graph with no edges but showing all the nodes and then be able to add and remove edges from the set listed in the code in successive transitions.  One edge will be added or deleted per transition which I will specify.

How can you do that?
Top Answer
samcarter
You could specify the overlays as third variable in your for-loop:

```
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}


\begin{document}
\begin{frame}{Title}
\tikzset{
  vertex/.style={
    very thick,
    draw=black, 
    circle,
    minimum size=18pt,
    inner sep=0pt
  },
  edge/.style={
    draw,
    thick
  }
}


\begin{figure}
\begin{tikzpicture}[scale=1.8, auto,swap]
    % First we draw the vertices
    \foreach \pos/\name in {{(0,2)/a}, {(2,1)/b}, {(4,1)/c},
                            {(0,0)/d}, {(3,0)/e}, {(2,-1)/f}, {(4,-1)/g}}
        \node[vertex] (\name) at \pos {$\name$};
    % Connect vertices with edges and draw weights
    \foreach \source/ \dest / \myoverlay  in {
      b/a/1-2, 
      c/b/3,
      d/a/4,
      d/b/5,
      e/b/6, 
      e/c/7,
      e/d/8-,
      f/d/9,
      f/e/10,
      g/e/11,
      g/f/12
    }{
        \path<\myoverlay>[edge] (\source) -- node {} (\dest);
    }
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}
```

(please note that as [@marmot mentioned in comments](https://topanswers.xyz/transcript?room=1399&id=71335#c71335), `\tikzstyle` is deprecated, you should use `\tikzset` instead)
Answer #2
marmot
Here is a proposal. You can add and remove elements from the list of edges with the pgf keys `add edges` and `remove edges`, respectively. Since you are saying that you want to draw the result each time, this is built into these keys. Adding and removing is achieved by steering the visibility via `overlay-beamer-styles`. 

```
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{overlay-beamer-styles}
\makeatletter
\tikzset{tgraph/.cd,nstep/.initial=0,
initialize/.code={\tikzset{tgraph/nstep=0}},
next/.code={\edef\pgfutil@tmpb{\the\numexpr\pgfkeysvalueof{/tikz/tgraph/nstep}+1}%
\tikzset{tgraph/nstep=\pgfutil@tmpb}%
},
edges/.initial={},
add edges/.code={\tikzset{tgraph/next}%
\edef\pgfutil@tmpa{\pgfkeysvalueof{/tikz/tgraph/edges}}%
\ifx\pgfutil@tmpa\pgfutil@empty
\edef\pgfutil@tmpa{#1}%
%\typeout{empty}%
\else
\edef\pgfutil@tmpa{\pgfutil@tmpa,#1}%
\fi
\tikzset{tgraph/edges=\pgfutil@tmpa}%
\tikzset{tgraph/draw edges={visible on=<\pgfkeysvalueof{/tikz/tgraph/nstep}>}}%
},
remove edges/.code={\tikzset{tgraph/next}%
\pgfutil@for\pgfutil@tmpg:={#1}\do{%
\edef\pgfutil@tmph{\noexpand\@removeelement{\pgfutil@tmpg}{\pgfkeysvalueof{/tikz/tgraph/edges}}{\noexpand\pgfutil@tmpd}}%
\pgfutil@tmph
\tikzset{tgraph/edges=\pgfutil@tmpd}%
}%
\tikzset{tgraph/draw edges={visible on=<\pgfkeysvalueof{/tikz/tgraph/nstep}>}}%
},
draw edges/.code={%
\edef\pgfutil@tmpd{\pgfkeysvalueof{/tikz/tgraph/edges}}%
\ifx\pgfutil@tmpd\pgfutil@empty
\typeout{No edges specified.}
\else
\path[#1] foreach \pgfutil@tmpe/\pgfutil@tmpf  in \pgfutil@tmpd
        { (\pgfutil@tmpe) edge[tgraph/Edge] (\pgfutil@tmpf)};
\fi}}
\makeatother

\begin{document}
\begin{frame}
\frametitle{Title}
\tikzset{% \tikzstyle is deprecated
vertex/.style={very thick,draw=black, circle,minimum size=18pt,inner sep=0pt},
tgraph/Edge/.style={draw,thick,-}}

\begin{figure}
\begin{tikzpicture}[scale=1.8, auto,swap]
    % First we draw the vertices
    \path foreach \pos/\name in {{(0,2)/a}, {(2,1)/b}, {(4,1)/c},
                            {(0,0)/d}, {(3,0)/e}, {(2,-1)/f}, {(4,-1)/g}}
        {node[vertex] (\name) at \pos {$\name$}};
	%{b/a,c/b,d/a,d/b,e/b, e/c,e/d,f/d,f/e,g/e,g/f},	
	\tikzset{tgraph/.cd,add edges={b/a},add edges={c/b},
	add edges={d/a},remove edges={b/a,c/b},
	add edges={b/a,c/b,d/b,e/b},remove edges={d/b}}
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}
```

![ani.gif](/image?hash=af51eace976f6653eae60aea284e0d15af3ccff1cf80a6b0fa12394b4565f0c6)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.