在投影机动画中使用矩阵和链

在投影机动画中使用矩阵和链

我正在尝试使用 beamer 生成这样的 gif

在此处输入图片描述

我可以使用以下代码\node来让它工作\draw

\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}

对于简单的流程图来说,这是可以的,但对于复杂的流程图,我想在代码中\matrix使用\chain

\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}

但是,第二个根本无法编译通过。

matrixchainsbeamer彼此不兼容吗?有什么方法可以解决这个问题吗?

答案1

对于第二种方法,您需要一个fragile框架(或者可以使用 & 符号替换,但我认为框架fragile更简单)。

\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}

在此处输入图片描述

相关内容