[Tex/LaTex] How to place multiple TikZ figures in the same possition/height in beamer

beamercolumnssubfloatstikz-pgf

Recently I discovered that I can create tikz figures inside my presentation beamer file. Before I use to create pdf files separately and import them into my presentation. By applying the new process I came across with a problem that I can not figure out how to solve it.

I am tying to use two separate tikz figures in one slide that will appear one after the other with the use of \visible<1-2>{} effect. I have done this before by importing figures or pdf files successfully but now I got stuck.

I can not figure out how to make the same vertical and horizontal spacing for all figures.

A sample of code demonstrates my problem. Between slide 2 and 3 the problem is easy to spot. The figures are moved in different position.

I used \vspace{} and I manage to get them a bit closer but not exactly the result that I want.

Does anybody have any idea how to improve my output?

Thank you in advance for your time and effort reading and replying to my question.

\documentclass[xcolor=pdftex,dvipsnames,table]{beamer}
\usepackage[english]{babel}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric , arrows , positioning}

\mode<presentation>
\usetheme{Dresden} % so so
\setbeamertemplate{blocks}[rounded][shadow=true]
\setbeamertemplate{navigation symbols}{} %take out the navigation symbols
\captionsetup{labelformat=simple}
\usefonttheme[stillsansseriflarge]{structureitalicserif}
\expandafter\def\expandafter\insertshorttitle\expandafter{%
  \insertshorttitle\hfill\insertframenumber\,/\,\inserttotalframenumber}%page numbering

\begin{document}

\begin{frame}{Frame Title}
\setbeamercovered{dynamic}%Makes the text appear before it presents nice!!!! 
    \begin{columns}[t] % contents are top vertically aligned
      \begin{column}[T]{.5\textwidth} % each column can also be its own environment
        \begin{itemize}
            \item<+-| alert@+> \invisible<2-4>{Comment 1}
            \item<+-| alert@+> \invisible<3-4>{Comment 2}
            \item<+-| alert@+> \invisible<4-4>{Comment 3}
            \item<+-| alert@+> My last comment.
          \end{itemize}  
      \end{column}
      \begin{column}[t]{.5\textwidth} % alternative top-align that's better for graphics
      \begin{figure}
        \visible<1-2>{%
          \begin{subfigure}[b]{1.0\linewidth}
           \vspace*{-1.cm}
            \caption{Sub caption 1 \label{fig:captionSmallOne}}
            \resizebox{\textwidth}{!}{%
              \begin{tikzpicture}[xscale=1.0]
              \draw [thick] (0.0, 2.0) -- (10, 2.0);
              \node[align=center, above] at (0.9, 2.0)%
                {Sample 1};
              \draw [thick] (3.0, 2.0) -- (3.0, 3.1);
              \node[align=center, above] at (3.0, 3.1)%
                {Line 1};
              \draw [thick] (6.0, 2.0) -- (6.0, 3.1);
              \node[align=center, above] at (6.0, 3.1)%
                {Line 2};
              \draw [thick] (9.0, 2.0) -- (9.0, 3.1);
              \node[align=center, above] at (9.0, 3.1)%
                {Line 3};
              \draw [fill=green] (3, 2.0) rectangle (6.0, 2.9);
              \draw [fill=orange] (3, 0.0) rectangle (6.5, 0.9);
              \draw [thick] (0.0, 0.0) -- (10, 0.0);
              \node[align=center, above] at (0.9,0.0)%
                {Sample 2};
              \draw [thick] (3.0,.0) -- (3.0, 2.0);
              \draw [thick] (3.5,-.3) -- (3.5,1.1);
              \node[align=center, below] at (3.5,-0.3)%
                {Line 4};
              \draw [thick] (6.5,-.3) -- (6.5,1.1);
              \node[align=center, below] at (6.5,-0.3)%
                {Line 5};
              \draw [thick] (9.5,-.3) -- (9.5,1.1);
              \node[align=center, below] at (9.5,-0.3)%
                {Line 6};
              \draw [->,thick] (4.2, 4.2) -- (4.2, 3.7);
              \node[align=center, above] at (4.2, 4.1)%
                {Event 1};
              \draw [thick, dashed] (6.0, 2.0) -- (6.5, 1.0);
              \draw [thick, dashed] (4.2, 0.0) -- (4.2, 3.6);
            \end{tikzpicture}
            }%
            %\end{adjustbox}
          \end{subfigure} \hfill
        } % End visible 1-2
        \visible<3-4>{%%
          \begin{subfigure}[b]{1.0\linewidth}
            \vspace*{-3.0cm}
            \caption{Sub caption 2 \label{fig:captionSmallTwo}}
              \resizebox{\textwidth}{!}{%
                \begin{tikzpicture}[xscale=1.0]
                  \draw [thick] (0.0, 2.0) -- (10, 2.0);
                  \node[align=center, above] at (0.9, 2.0)%
                    {TestS 1};
                  \draw [thick] (3.0, 2.0) -- (3.0, 3.1);
                  \node[align=center, above] at (3.0, 3.1)%
                    {Line 1};
                  \draw [thick] (6.0, 2.0) -- (6.0, 3.1);
                  \node[align=center, above] at (6.0, 3.1)%
                    {Line 2};
                  \draw [thick] (9.0, 2.0) -- (9.0, 3.1);
                  \node[align=center, above] at (9.0, 3.1)%
                    {Line 3};
                  \draw [fill=green] (3, 2.0) rectangle (6.0, 2.9);
                  \draw [fill=orange] (3, 0.0) rectangle (3.5, 0.9);
                  \draw [thick] (0.0, 0.0) -- (10, 0.0);
                  \node[align=center, above] at (0.9,0.0)%
                    {Sample 2};
                  \draw [thick] (3.0,.0) -- (3.0, 2.0);
                  \draw [thick] (3.5,-.3) -- (3.5,1.1);
                  \node[align=center, below] at (3.5,-0.3)%
                    {Line 4};
                  \draw [thick] (6.5,-.3) -- (6.5,1.1);
                  \node[align=center, below] at (6.5,-0.3)%
                    {Line 5};
                  \draw [thick] (9.5,-.3) -- (9.5,1.1);
                  \node[align=center, below] at (9.5,-0.3)%
                    {Line 6};
                  \draw [->,thick] (3.3, 4.2) -- (3.3, 3.7);
                  \node[align=center, above] at (3.2, 4.1)%
                    {Event 2};
                  \draw [thick, dashed] (6.0, 2.0) -- (3.5, 1.1);
                  \draw [thick, dashed] (3.3, 0.0) -- (3.3, 3.6);
                \end{tikzpicture}
              }%
              %\end{adjustbox}
            \end{subfigure} \hfill
          } % End visible 3-4
        \captionsetup{justification=centering} %Center a two line caption
        \caption{The big caption} \protect\label{fig:big}
      \end{figure}
    \end{column}
  \end{columns}
\end{frame}
\end{document}

Best Answer

Update

In this updated version I still used \only instead of \visible, and used the visible on style to gratly simplify the code; with this new approach the images won't "jump" since most of the figure remains constant (overlayarea could still be used, but I think it's not required anymore). The subfigure counter has to be manually updated:

\documentclass[xcolor=pdftex,dvipsnames,table]{beamer}
\usepackage[english]{babel}
\usepackage[compatibility=false]{caption}
\usepackage{subcaption}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric , arrows , positioning}

\tikzset{
    invisible/.style={opacity=0,text opacity=0},
    visible on/.style={alt=#1{}{invisible}},
    alt/.code args={<#1>#2#3}{%
      \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} 
    }
}

\mode<presentation>
\usetheme{Dresden} % so so
\setbeamertemplate{blocks}[rounded][shadow=true]
\setbeamertemplate{navigation symbols}{} %take out the navigation symbols
\captionsetup{labelformat=simple}
\usefonttheme[stillsansseriflarge]{structureitalicserif}
\expandafter\def\expandafter\insertshorttitle\expandafter{%
  \insertshorttitle\hfill\insertframenumber\,/\,\inserttotalframenumber}%page numbering

\begin{document}

\begin{frame}{Frame Title}
\setbeamercovered{dynamic}%Makes the text appear before it presents nice!!!! 
    \begin{columns}[t] % contents are top vertically aligned
      \begin{column}[T]{.5\textwidth} % each column can also be its own environment
        \begin{itemize}
            \item<+-| alert@+> \invisible<2-4>{Comment 1}
            \item<+-| alert@+> \invisible<3-4>{Comment 2}
            \item<+-| alert@+> \invisible<4-4>{Comment 3}
            \item<+-| alert@+> My last comment.
          \end{itemize}  
      \end{column}
      \begin{column}[T]{.5\textwidth} % alternative top-align that's better for graphics
      \begin{figure}
          \begin{subfigure}{1.0\linewidth}
            \only<1-2>{\caption{Sub caption 1 \label{fig:captionSmallOne}}}
            \only<3-4>{\stepcounter{subfigure}\caption{Sub caption 2 \label{fig:captionSmallTwo}}}
            \resizebox{\textwidth}{!}{%
              \begin{tikzpicture}[xscale=1.0,baseline]
              \draw [thick] (0.0, 2.0) -- (10, 2.0);
              \node[align=center, above,visible on=<1-2>] at (0.9, 2.0)%
                {Sample 1};
              \node[align=center, above,visible on=<3-4>] at (0.9, 2.0)%
                {TestS 1};
              \draw [thick] (3.0, 2.0) -- (3.0, 3.1);
              \node[align=center, above] at (3.0, 3.1)%
                {Line 1};
              \draw [thick] (6.0, 2.0) -- (6.0, 3.1);
              \node[align=center, above] at (6.0, 3.1)%
                {Line 2};
              \draw [thick] (9.0, 2.0) -- (9.0, 3.1);
              \node[align=center, above] at (9.0, 3.1)%
                {Line 3};
              \draw [fill=green] (3, 2.0) rectangle (6.0, 2.9);
              \draw [fill=orange] (3, 0.0) rectangle (6.5, 0.9);
              \draw [thick] (0.0, 0.0) -- (10, 0.0);
              \node[align=center, above] at (0.9,0.0)%
                {Sample 2};
              \draw [thick] (3.0,.0) -- (3.0, 2.0);
              \draw [thick] (3.5,-.3) -- (3.5,1.1);
              \node[align=center, below] at (3.5,-0.3)%
                {Line 4};
              \draw [thick] (6.5,-.3) -- (6.5,1.1);
              \node[align=center, below] at (6.5,-0.3)%
                {Line 5};
              \draw [thick] (9.5,-.3) -- (9.5,1.1);
              \node[align=center, below] at (9.5,-0.3)%
                {Line 6};
              \draw [->,thick,visible on=<1-2>] (4.2, 4.2) -- (4.2, 3.7);
              \draw [->,thick,visible on=<3-4>] (3.3, 4.2) -- (3.3, 3.7);
              \node[align=center, above,visible on=<1-2>] at (4.2, 4.1)%
                {Event 1};
              \node[align=center, above,visible on=<3-4>] at (3.2, 4.1)%
                {Event 2};
              \draw [thick, dashed,visible on=<1-2>] (6.0, 2.0) -- (6.5, 1.0);
              \draw [thick, dashed,visible on=<1-2>] (4.2, 0.0) -- (4.2, 3.6);
              \draw [thick, dashed,visible on=<3-4>] (6.0, 2.0) -- (3.5, 1.1);
              \draw [thick, dashed,visible on=<3-4>] (3.3, 0.0) -- (3.3, 3.6);
            \end{tikzpicture}
            }%
          \end{subfigure} \hfill
        \captionsetup{justification=centering} %Center a two line caption
        \caption{The big caption} \protect\label{fig:big}
      \end{figure}
    \end{column}
  \end{columns}
\end{frame}

\end{document}

enter image description here

First version

You can use overlayarea and \only instead of \visible:

\documentclass[xcolor=pdftex,dvipsnames,table]{beamer}
\usepackage[english]{babel}
\usepackage[compatibility=false]{caption}
\usepackage{subcaption}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric , arrows , positioning}

\mode<presentation>
\usetheme{Dresden} % so so
\setbeamertemplate{blocks}[rounded][shadow=true]
\setbeamertemplate{navigation symbols}{} %take out the navigation symbols
\captionsetup{labelformat=simple}
\usefonttheme[stillsansseriflarge]{structureitalicserif}
\expandafter\def\expandafter\insertshorttitle\expandafter{%
  \insertshorttitle\hfill\insertframenumber\,/\,\inserttotalframenumber}%page numbering

\begin{document}

\begin{frame}{Frame Title}
\setbeamercovered{dynamic}%Makes the text appear before it presents nice!!!! 
    \begin{columns}[t] % contents are top vertically aligned
    \begin{overlayarea}{\textwidth}{\textheight}
      \begin{column}[T]{.5\textwidth} % each column can also be its own environment
        \begin{itemize}
            \item<+-| alert@+> \invisible<2-4>{Comment 1}
            \item<+-| alert@+> \invisible<3-4>{Comment 2}
            \item<+-| alert@+> \invisible<4-4>{Comment 3}
            \item<+-| alert@+> My last comment.
          \end{itemize}  
      \end{column}
      \begin{column}[t]{.5\textwidth} % alternative top-align that's better for graphics
      \begin{figure}
        \only<1-2>{%
          \begin{subfigure}[t]{1.0\linewidth}
            \caption{Sub caption 1 \label{fig:captionSmallOne}}
            \resizebox{\textwidth}{!}{%
              \begin{tikzpicture}[xscale=1.0]
              \draw [thick] (0.0, 2.0) -- (10, 2.0);
              \node[align=center, above] at (0.9, 2.0)%
                {Sample 1};
              \draw [thick] (3.0, 2.0) -- (3.0, 3.1);
              \node[align=center, above] at (3.0, 3.1)%
                {Line 1};
              \draw [thick] (6.0, 2.0) -- (6.0, 3.1);
              \node[align=center, above] at (6.0, 3.1)%
                {Line 2};
              \draw [thick] (9.0, 2.0) -- (9.0, 3.1);
              \node[align=center, above] at (9.0, 3.1)%
                {Line 3};
              \draw [fill=green] (3, 2.0) rectangle (6.0, 2.9);
              \draw [fill=orange] (3, 0.0) rectangle (6.5, 0.9);
              \draw [thick] (0.0, 0.0) -- (10, 0.0);
              \node[align=center, above] at (0.9,0.0)%
                {Sample 2};
              \draw [thick] (3.0,.0) -- (3.0, 2.0);
              \draw [thick] (3.5,-.3) -- (3.5,1.1);
              \node[align=center, below] at (3.5,-0.3)%
                {Line 4};
              \draw [thick] (6.5,-.3) -- (6.5,1.1);
              \node[align=center, below] at (6.5,-0.3)%
                {Line 5};
              \draw [thick] (9.5,-.3) -- (9.5,1.1);
              \node[align=center, below] at (9.5,-0.3)%
                {Line 6};
              \draw [->,thick] (4.2, 4.2) -- (4.2, 3.7);
              \node[align=center, above] at (4.2, 4.1)%
                {Event 1};
              \draw [thick, dashed] (6.0, 2.0) -- (6.5, 1.0);
              \draw [thick, dashed] (4.2, 0.0) -- (4.2, 3.6);
            \end{tikzpicture}
            }%
            %\end{adjustbox}
          \end{subfigure} \hfill
        } % End visible 1-2
        \only<3-4>{%%
          \begin{subfigure}[t]{1.0\linewidth}
            \caption{Sub caption 2 \label{fig:captionSmallTwo}}
              \resizebox{\textwidth}{!}{%
                \begin{tikzpicture}[xscale=1.0]
                  \draw [thick] (0.0, 2.0) -- (10, 2.0);
                  \node[align=center, above] at (0.9, 2.0)%
                    {TestS 1};
                  \draw [thick] (3.0, 2.0) -- (3.0, 3.1);
                  \node[align=center, above] at (3.0, 3.1)%
                    {Line 1};
                  \draw [thick] (6.0, 2.0) -- (6.0, 3.1);
                  \node[align=center, above] at (6.0, 3.1)%
                    {Line 2};
                  \draw [thick] (9.0, 2.0) -- (9.0, 3.1);
                  \node[align=center, above] at (9.0, 3.1)%
                    {Line 3};
                  \draw [fill=green] (3, 2.0) rectangle (6.0, 2.9);
                  \draw [fill=orange] (3, 0.0) rectangle (3.5, 0.9);
                  \draw [thick] (0.0, 0.0) -- (10, 0.0);
                  \node[align=center, above] at (0.9,0.0)%
                    {Sample 2};
                  \draw [thick] (3.0,.0) -- (3.0, 2.0);
                  \draw [thick] (3.5,-.3) -- (3.5,1.1);
                  \node[align=center, below] at (3.5,-0.3)%
                    {Line 4};
                  \draw [thick] (6.5,-.3) -- (6.5,1.1);
                  \node[align=center, below] at (6.5,-0.3)%
                    {Line 5};
                  \draw [thick] (9.5,-.3) -- (9.5,1.1);
                  \node[align=center, below] at (9.5,-0.3)%
                    {Line 6};
                  \draw [->,thick] (3.3, 4.2) -- (3.3, 3.7);
                  \node[align=center, above] at (3.2, 4.1)%
                    {Event 2};
                  \draw [thick, dashed] (6.0, 2.0) -- (3.5, 1.1);
                  \draw [thick, dashed] (3.3, 0.0) -- (3.3, 3.6);
                \end{tikzpicture}
              }%
              %\end{adjustbox}
            \end{subfigure} \hfill
          } % End visible 3-4
        \captionsetup{justification=centering} %Center a two line caption
        \caption{The big caption} \protect\label{fig:big}
      \end{figure}
    \end{column}
  \end{overlayarea}
  \end{columns}
\end{frame}
\end{document}

enter image description here

Remarks

  • Instead of two separate tikzpicture environments, you can use only one and the fact that TikZ's constructs are overlay-aware. This will considerably shorten the code and will also take care of the problem with jumping images.

  • Notice that is not advisable to use the subcaption package together with beamer; you should use subfig instead; in any case, if you decide to use subcaption, you need to use caption in compatibility=false mode, so load the packages like this:

    \usepackage[compatibility=false]{caption}
    \usepackage{subcaption}