Search code examples
latexbeamertikz

Using Tikz positioning with \onslide\visible\only inside beamer


I have the following image: enter image description here

and I want its elements to appear in a certain order in my beamer presentation. For the moment, I'm trying to make a_1, a_2 appear in a second slide. I'm using this code:

\documentclass{beamer}
\usepackage{textcomp}
\usepackage{tikz}
\usetheme{Madrid}
\begin{document}
\begin{frame}{}
 \usetikzlibrary{shapes,arrows, positioning, calc}  
 
\tikzset{%
  block/.style    = {rounded corners, draw, thick, circle, minimum height = 3em,
    minimum width = 3em, fill = yellow!50},
  point/.style    = {coordinate}, % Input
}
\begin{tikzpicture}[auto, thick, node distance=2cm, >=triangle 45]
%\node[block] (A1) at (0,0) {$A_1$};
%\node[block, right  = 1cm of A1] (A2) {$A_2$};
\node[below  = of A1] (a1) {{\visible<2->{$a_1$}}};
\node[below  = of A2] (a2) {{\visible<2->{$a_2$}}};
\node[below  = of A1] (a1) {$a_1$};
\node[below  = of A2] (a2) {$a_2$};
\draw[->] (A1.south) ++(0,-0.3) -- ++(0, -1.3);
\draw[->] (A2.south) ++(0,-0.3) -- ++(0, -1.3);
\end{tikzpicture}
\end{frame}
\end{document}

but all I get is this:

I get errors like: "unknown arrow tip kind 'triangle 45'" and also "unknown operator 'o' or 'of ' in (of A1). It's my first time using Tikz, and I'm not really practical with beamer tools like \onslide, \only or \visible. I think I could create different images, one for each frame, and then add them with \includegraphics and \pause, but it would be more practical if I managed to achieve the same result without creating different pictures. Any help would be really appreciated.


Solution

    • load your tikz libraries in the preamble, not inside the frame

    • you can use the overlay-beamer-styles library to control the appearance of the nodes

    \documentclass{beamer}
    \usepackage{textcomp}
    \usepackage{tikz}
    \usetheme{Madrid}
    
     \usetikzlibrary{shapes,arrows, positioning, calc}  
     \usetikzlibrary{overlay-beamer-styles}
     
    \tikzset{%
      block/.style    = {rounded corners, draw, thick, circle, minimum height = 3em,
        minimum width = 3em, fill = yellow!50},
      point/.style    = {coordinate}, % Input
    }
    
    
    \begin{document}
    \begin{frame}{}
    \begin{tikzpicture}[auto, thick, node distance=2cm, >=triangle 45]
    \node[block] (A1) at (0,0) {$A_1$};
    \node[block, right  = 1cm of A1] (A2) {$A_2$};
    \node[below  = of A1, visible on=<2->] (a1) {$a_1$};
    \node[below  = of A2, visible on=<2->] (a2) {$a_2$};
    \draw[->] (A1.south) ++(0,-0.3) -- ++(0, -1.3);
    \draw[->] (A2.south) ++(0,-0.3) -- ++(0, -1.3);
    \end{tikzpicture}
    \end{frame}
    \end{document}
    

    enter image description here