2

In an animation using Tikz in beamer, I want to draw a bigger box around the boxes in the first two columns i.e. box11, box12, box21 and box22 (as I draw in the very last slide using a different method). The bigger box should be animated, i.e., appear on a new slide.

\documentclass{beamer}
\mode<presentation>{\usetheme{Madrid}}

\usepackage{tikz}
\usetikzlibrary{arrows.meta,backgrounds,fit,matrix}


\tikzstyle{bigbox} = [minimum size=3cm,draw=blue!50, thick, fill=blue!10, rounded corners, rectangle]
\tikzstyle{box} = [minimum size=1.5cm, rounded corners,rectangle, fill=blue!50]
\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{document}


\begin{frame}[t]
    

\begin{tikzpicture}[
every edge/.style = {draw, thick, -Stealth}
                        ]

\node (box11) [box] {11};

\node<+(1)-> (box12) [box, right of=box11,xshift=2cm] {12};

\node<+(1)-> (box13) [box, right of=box12,xshift=2cm] {13};

\node<+(1)-> (box21) [box, below of=box11,yshift=-2cm] {21};

\node<+(1)-> (box22) [box, right of=box21,xshift=2cm] {22};

\node<+(1)-> (box23) [box, right of=box22,xshift=2cm] {23};

%\node (box) [bigbox] {P};

\path[transform canvas={yshift=+2mm}]<+(1)->
    (box11) edge (box12)
    (box12) edge (box13)
    (box21) edge (box22)
    (box22) edge (box23);
    
\path[transform canvas={yshift=-2mm}]<+(1)->
    (box12) edge (box11)
    (box13) edge (box12)
    (box22) edge (box21)
    (box23) edge (box22);

\path[transform canvas={xshift=+2mm}]<+(1)->
    (box12) edge (box22)
    (box13) edge (box23);
    
\path[transform canvas={xshift=-2mm}]<+(1)->
    (box22) edge (box12)
    (box23) edge (box13);

\node<+(1)-> at (1,-1.5) [font=\sffamily] {Text};
\end{tikzpicture}
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
    

\begin{tikzpicture}
\tikzstyle{bigbox} = [minimum size=3cm,draw=blue!50, thick, fill=blue!10, rounded corners, rectangle]
\tikzstyle{box} = [minimum size=1.5cm, rounded corners,rectangle, fill=blue!50]


\matrix[row sep=20mm, column sep=15mm, inner sep=7mm, bigbox, matrix of nodes, every node/.style=box] {
11 & 12\\
21 & 22\\
};

\node (h1) [yshift=0mm] {Text};

\matrix[xshift=6cm,row sep=20mm, column sep=15mm, inner sep=7mm, matrix of nodes, every node/.style=box] {
13\\
23\\
};


\end{tikzpicture}
\end{frame}


\end{document}

1 Answer 1

2

You can use the background library so that the box that includes the others appears in the background.

Here, I did not use this library (I put the code between comments), I used the native possibility of TikZ.

\pgfdeclarelayer{background layer}
\pgfsetlayers{background layer,main}

screenshot

\documentclass{beamer}
\mode<presentation>{\usetheme{Madrid}}

\usepackage{tikz}
\usetikzlibrary{arrows.meta,backgrounds,fit,matrix}
%\usetikzlibrary{backgrounds}

\tikzstyle{bigbox} = [minimum size=3cm,draw=blue!50, thick, fill=blue!10, rounded corners, rectangle]
\tikzstyle{box} = [minimum size=1.5cm, rounded corners,rectangle, fill=blue!50]
\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{document}
\pgfdeclarelayer{background layer}
\pgfsetlayers{background layer,main}

\begin{frame}[t]
    

\begin{tikzpicture}[
every edge/.style = {draw, thick, -Stealth}
                        ]
\path (-1,-1)rectangle(1,1);
\node (box11) [box] {11};

\node<+(1)-> (box12) [box, right of=box11,xshift=2cm] {12};

\node<+(1)-> (box13) [box, right of=box12,xshift=2cm] {13};

\node<+(1)-> (box21) [box, below of=box11,yshift=-2cm] {21};

\node<+(1)-> (box22) [box, right of=box21,xshift=2cm] {22};

\node<+(1)-> (box23) [box, right of=box22,xshift=2cm] {23};

%\node (box) [bigbox] {P};

\path[transform canvas={yshift=+2mm}]<+(1)->
    (box11) edge (box12)
    (box12) edge (box13)
    (box21) edge (box22)
    (box22) edge (box23);
    
\path[transform canvas={yshift=-2mm}]<+(1)->
    (box12) edge (box11)
    (box13) edge (box12)
    (box22) edge (box21)
    (box23) edge (box22);

\path[transform canvas={xshift=+2mm}]<+(1)->
    (box12) edge (box22)
    (box13) edge (box23);
    
\path[transform canvas={xshift=-2mm}]<+(1)->
    (box22) edge (box12)
    (box23) edge (box13);
    
%\begin{scope}[on background layer={color=yellow}]
%\node<+->[draw,fill=green!50,fill opacity=.5,fit= (box11) (box22)]{box};
%\end{scope}

\begin{pgfonlayer}{background layer}
\node<+->[draw,fill=green!50,fit= (box11) (box22)]{box};
\end{pgfonlayer}
\end{tikzpicture}
\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile]
%    
%
%\begin{tikzpicture}
%\tikzstyle{bigbox} = [minimum size=3cm,draw=blue!50, thick, fill=blue!10, rounded corners, rectangle]
%\tikzstyle{box} = [minimum size=1.5cm, rounded corners,rectangle, fill=blue!50]
%
%
%\matrix[row sep=20mm, column sep=15mm, inner sep=7mm, bigbox, matrix of nodes, every node/.style=box] {
%11 & 12\\
%21 & 22\\
%};
%
%\node (h1) [yshift=0mm] {Text};
%
%\matrix[xshift=6cm,row sep=20mm, column sep=15mm, inner sep=7mm, matrix of nodes, every node/.style=box] {
%13\\
%23\\
%};
%
%
%\end{tikzpicture}
%\end{frame}


\end{document}
7
  • I noticed a small problem in this. All the small nodes shift a bit down when the big node is added in background. Is it possible to fix this?
    – Curious
    Commented Aug 8, 2020 at 8:19
  • @Curious It's because of the bounding box of the growing figure. Just make it bigger from the start. I've just added the following code at the very beginning to enlarge this bounding box. \path (-1,-1)rectangle(1,1);
    – AndréC
    Commented Aug 8, 2020 at 8:34
  • I am not sure if I missed something, but now it shifts a bit right.
    – Curious
    Commented Aug 8, 2020 at 9:03
  • I don't have that problem. What engine do you compile with? pdfLaTeX? LuaLaTeX ? other ? Try this \path(-1,-4)rectangle(7,1);
    – AndréC
    Commented Aug 8, 2020 at 9:09
  • I use pdfLaTeX. Anyway, sorry for that. \path (-1,-1)rectangle(1,1); works perfectly in the above code. I figured out that it is something else which conflicts the positioning when I put it in my complete code.
    – Curious
    Commented Aug 8, 2020 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .