4

I'd like to highlight different parts of a tikz diagram in beamer slides using something like the \only command. I was able to do this at the level of nodes, which is probably fine, but as I have some long node definitions, I was wondering if I could put \only inside of the \node

e.g.

\node [block \only<1>{,fill=blue!50}] (start) {start};

won't work (unsurprisingly).

The MWE posted below is an OK solution for my problem, but I'm curious if there is a solution that is more atomic (i.e. works within the \node command rather than outside of it. I guess that this is not possible if we are confined to just latex.

\documentclass[compress,red,notes]{beamer}

\usepackage{tikz}

\tikzstyle{line} = [draw, very thick, color=black!50]

\tikzstyle{block} = [rectangle, draw,
    text centered, node distance=4em]


\begin{document}

\begin{frame}
\frametitle{An tikz diagram.}
\begin{figure}
\centering
\begin{tikzpicture}[scale=0.5, every node/.style={scale=0.5}]

    \only<1>\node [block, fill=blue!50] (start) {start};
    \only<2>\node [block] (start) {start};

    \node [block, below of=start] (end) {end};

    \path [line] (start) -- (end);
\end{tikzpicture}
\end{figure}
\end{frame}

\end{document}

Based on @Kevin C's suggestion to look at @Daniel's answer, I have the following non-working solution:

\documentclass[compress,red,notes]{beamer}

\usepackage{tikz}

\tikzstyle{line} = [draw, very thick, color=black!50]

\tikzstyle{block} = [rectangle, draw,
    text centered, node distance=4em]

\tikzset{
  invisible/.style={#1{}},
  visible on/.style={alt={{color=blue!50}{invisible}}},
  alt/.code args={<#1>#2#3}{%
    \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
  },
}


\begin{document}

\begin{frame}
\frametitle{An tikz diagram.}
\begin{figure}
\centering
\begin{tikzpicture}[scale=0.5, every node/.style={scale=0.5}]

    \node [block, visible on=<2>] (start) {start};

    \node [block, below of=start] (end) {end};

    \path [line] (start) -- (end);
\end{tikzpicture}
\end{figure}
\end{frame}

\end{document}
  • I think this is the answer to your question: tex.stackexchange.com/a/55849/18228 – Herr K. Jun 5 '14 at 21:03
  • Thanks, that does look like what I was hoping for! I've updated the question a bit. – bbarker Jun 5 '14 at 21:36
  • Actually the solution to your problem is slightly easier (code-wise). See my answer. – Herr K. Jun 5 '14 at 22:06
2

To set certain keys to take effect on specified slides, you can define a style slightly simpler than Daniel's visible on. The following definition for a onslide style should suffice:

% usage: onslide=<overlay spec>{keys}
onslide/.code args={<#1>#2}{\only<#1>{\pgfkeysalso{#2}}} 

Code

\documentclass[compress,red,notes]{beamer}
\usepackage{tikz}
  \tikzset{
    line/.style={draw, very thick, color=black!50},
    block/.style={rectangle, draw, text centered, node distance=4em},
    onslide/.code args={<#1>#2}{\only<#1>{\pgfkeysalso{#2}}}, 
  }

\begin{document}

\begin{frame}
\frametitle{An tikz diagram.}
\begin{figure}
\centering
\begin{tikzpicture}[scale=0.5, every node/.style={scale=0.5}]
    \node [block, onslide=<2>{fill=blue!50}] (start) {start};

    \node [block, below of=start] (end) {end};

    \path [line] (start) -- (end);
\end{tikzpicture}
\end{figure}
\end{frame}

\end{document}

Output

enter image description here

  • This is very cool - more incentive to better understand PGF/TikZ someday ... – bbarker Jun 5 '14 at 22:35
  • If you use the overlay-beamer-styles from aobs-tikz package, you can say \node [block, background fill=blue!50,fill on=<2>] (start) {start};. – Claudio Fiandrino Jun 6 '14 at 6:10
  • @ClaudioFiandrino: Thanks for the pointer! Didn't realize there's been a package for this. Nice work! – Herr K. Jun 6 '14 at 14:43

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.