# How to fix tikzpicture size?

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 ?

• One way is to fake it by adding \node<1>[draw=white, line width=1.5pt, fit=(a) (b)]{}; into your tikzpicture above the \node<2>... line... I'm sure there is a better way, so I won't put this as an answer :-) Jun 23 '15 at 10:50
• That would have been my first guess, too. If you add opacity=0 it doesn't hide anything in the background. Jun 23 '15 at 10:56
• I think avoiding-jumping-frames-in-beamer is a duplicate. Would you agree? Jun 23 '15 at 11:19

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}


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}

• 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? 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. Jun 23 '15 at 12:18
• what is the meaning o <1> in \node<1>? Jan 28 '18 at 10:47
• @Viesturs Point brackets are used for overlay specifications, a feature of class beamer, see chapter "9 Overlay specifications" in its documentation. Jan 28 '18 at 10:54
• @Viesturs Please, read the documentation; it also contains a tutorial, section "3.10 Using Overlay Specifications". Jan 28 '18 at 11:03

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}