[Tex/LaTex] How to fix tikzpicture size

beameroverlaystikz-pgf

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 ?

Best Answer

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}
Related Question