4

In a beamer presentation I am using tikz to draw graphs. To highlight part of the graph I am using overlay and the fit library as in the following MWE:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,fit}

\begin{document}
\begin{frame}
  \frametitle{TEST}
  \centering
  \begin{tikzpicture}
    \node[draw=black, rectangle]             (a) {A};
    \node[draw=black, rectangle, right=of a] (b) {B};
    \draw (a) -- (b);
    \node<2>[draw=red, line width=1.5pt, fit=(a) (b)]{};
  \end{tikzpicture}
  \begin{itemize}
  \item A
  \item B
  \end{itemize}
\end{frame}
\end{document}

This solution works fine, excepted that there is a little shift of the whole frame when the user click the mouse to reveal the higliht rectangle drawn with fit. I guess that this comes from the fact that addiding this fit node changes the bounds of the tikzpicture. How can I remove this annoying shift behaviour ?

Manuel Selva
  • 1,839

3 Answers3

5

A solution, which avoids printing a white frame or using transparency: The node is placed in the first slide, but not drawn. Because of the missing draw operators, the bounding box is updated manually using the node.

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,fit}

\begin{document}
\begin{frame}
  \frametitle{TEST}
  \centering
  \begin{tikzpicture}
    \node[draw=black, rectangle]             (a) {A};
    \node[draw=black, rectangle, right=of a] (b) {B};
    \draw (a) -- (b);
    \node<1>(tmp)[line width=1.5pt, fit=(a) (b)]{}
      (tmp.south west) (tmp.north east); % update bounding box
    \node<2>[draw=red, line width=1.5pt, fit=(a) (b)]{};
  \end{tikzpicture}
  \begin{itemize}
  \item A
  \item B
  \end{itemize}
\end{frame}
\end{document}
Heiko Oberdiek
  • 271,626
  • I don't understand what (tmp.south west) (tmp.north east) do. I understand you create a tmp node which fits nodes a and b but I don't know how calling coordinates tmp.south west and tmp.north east updates anything. Could you explain it? – Ignasi Jun 23 '15 at 12:12
  • @Ignasi (tmp.south west) (tmp.north east) are simple "moveto" operations, which updates the bounding box of the tikzpicture. – Heiko Oberdiek Jun 23 '15 at 12:18
  • what is the meaning o <1> in \node<1>? – Viesturs Jan 28 '18 at 10:47
  • 1
    @Viesturs Point brackets are used for overlay specifications, a feature of class beamer, see chapter "9 Overlay specifications" in its documentation. – Heiko Oberdiek Jan 28 '18 at 10:54
  • If before slide 1 I insert 2 other slides will I have to update <1> to <3>? – Viesturs Jan 28 '18 at 10:59
  • 1
    @Viesturs Please, read the documentation; it also contains a tutorial, section "3.10 Using Overlay Specifications". – Heiko Oberdiek Jan 28 '18 at 11:03
5

There is also a TikZ library for this problem (overlay-beamer-styles).

The solution with this library:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,fit}
\usetikzlibrary{overlay-beamer-styles}

\begin{document}

\begin{frame}
  \frametitle{TEST}
  \centering
  \begin{tikzpicture}
    \node[draw=black, rectangle]             (a) {A};
    \node[draw=black, rectangle, right=of a] (b) {B};
    \node[draw=red,line width=1.5pt, fit=(a) (b), draw on=<2->]{};
    \draw (a) -- (b);
  \end{tikzpicture}
  \begin{itemize}
  \item A
  \item B
  \end{itemize}
\end{frame}
\end{document}
1

Here's a solution using Beamer's overlayarea. The only real disadvantage is that you have to specify the height of the area.

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,fit}

\begin{document}
\begin{frame}
  \frametitle{TEST}
  \begin{overlayarea}{\linewidth}{.3\textheight}
  \centering
    \begin{tikzpicture}
    \only<1-2>{
      \node[draw=black, rectangle]             (a) {A};
      \node[draw=black, rectangle, right=of a] (b) {B};
      \draw (a) -- (b);}
    \only<2>{
      \node<2>[draw=red, line width=1.5pt, fit=(a) (b)]{};}
    \end{tikzpicture}
  \end{overlayarea}
    \begin{itemize}
    \item A
    \item B
    \end{itemize}
\end{frame}
\end{document}

stop wobble with <code>overlayarea</code>

cfr
  • 198,882