2

I am trying to use beamer to generate a gif like this

enter image description here

I am able to get it working with \node and \draw with the following code

\documentclass[border=5mm, usenames, dvipsnames]{beamer}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning, calc, arrows.meta, matrix, chains, scopes}
\usetikzlibrary{overlay-beamer-styles}
\begin{document}
    \tikzset{myrect/.style n args = {4}{font=\footnotesize \ttfamily, align=left, rounded corners, rectangle, minimum width=#1cm, minimum height=#2cm, fill=#3, draw=#4}}
    \tikzset{myrect/.default={3}{1}{white}{black}}
    \tikzset{mycon/.style = {rounded corners}}
    \begin{frame}[t]
        \begin{tikzpicture}
            \node [myrect] (a) {A};
            \node [myrect, right = of a, visible on=<2->] (b) {B};
            \node [myrect, right = of b, visible on=<3->] (c) {C};
            \draw [mycon, ->, visible on=<4->] (a) to (b);
            \draw [mycon, ->, visible on=<5->] (b) to (c);
        \end{tikzpicture}
    \end{frame}
\end{document}

It is ok for simple flowchart, but for complex ones, I'd like to utilize \matrix and \chain in the code

\documentclass[border=5mm, usenames, dvipsnames]{beamer}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning, calc, arrows.meta, matrix, chains, scopes}
\usetikzlibrary{overlay-beamer-styles}
\begin{document}
    \tikzset{myrect/.style n args = {4}{font=\footnotesize \ttfamily, align=left, rounded corners, rectangle, minimum width=#1cm, minimum height=#2cm, fill=#3, draw=#4}}
    \tikzset{myrect/.default={3}{1}{white}{black}}
    \tikzset{mycon/.style = {rounded corners}}
    \begin{frame}[t]
        \begin{tikzpicture}
            \matrix (layer1) [matrix of nodes, nodes={myrect}, column sep=1cm] {
                A &
                |[visible on=<2->]| B &
                |[visible on=<3->]| C \\
            };
            {
                [start chain,every on chain/.style={join}, every join/.style={->}]
                \chainin (layer1-1-1) [];
                \chainin (layer1-1-2) [visible on=<4->];
                \chainin (layer1-1-3) [visible on=<5->];
            }
        \end{tikzpicture}
    \end{frame}
\end{document}

However, the second one simply can't compile OK.

Are matrix, chains and beamer not compatible with each other? Is there any ways to solve this?

2

For your second approach you need a fragile frame (or alternatively an ampersand replacement, but I think a fragile frame is easier).

\documentclass[border=5mm, usenames, dvipsnames]{beamer}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning, calc, arrows.meta, matrix, chains, scopes}
\usetikzlibrary{overlay-beamer-styles}
\begin{document}
    \tikzset{myrect/.style n args = {4}{font=\footnotesize \ttfamily, align=left, rounded corners, rectangle, minimum width=#1cm, minimum height=#2cm, fill=#3, draw=#4}}
    \tikzset{myrect/.default={3}{1}{white}{black}}
    \tikzset{mycon/.style = {rounded corners}}
    \begin{frame}[t,fragile]
        \begin{tikzpicture}
            \matrix (layer1) [matrix of nodes, nodes={myrect}, column sep=1cm] {
                A &
                |[visible on=<2->]| B &
                |[visible on=<3->]| C \\
            };
            {
                [start chain,every on chain/.style={join}, every join/.style={->}]
                \chainin (layer1-1-1) [];
                \only<4->{\chainin (layer1-1-2) [];}
                \only<5->{\chainin (layer1-1-3) [];}
            }
        \end{tikzpicture}
    \end{frame}
\end{document}

enter image description here

| improve this answer | |
  • So quick I get the solution! I have to say, LaTeX is so mysterious, but thanks to guiders like you, help me grasp more and more power of it. – Eric Sun Aug 17 '18 at 8:59
  • @EricSun You're welcome! However I have to disagree, LaTeX is not mysterious, but TikZ certainly is :) – samcarter_is_at_topanswers.xyz Aug 17 '18 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.