我正在尝试使用 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}
但是,第二个根本无法编译通过。
matrix
、chains
和beamer
彼此不兼容吗?有什么方法可以解决这个问题吗?
答案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}