我在 tikz 中绘制了一棵树,想将其转换为另一棵树。树的结构相同,只是标签(以及标签的颜色)发生了变化。因此,之前: 之后: 我希望动画逐行进行:首先改变底行,然后改变中间行,然后改变顶行。
我怎样才能以最少的代码重复来实现这一点?我已经看到了一个很好的例子展示如何逐帧更改格式,但我想更改标签和格式。
两棵树的代码如下。
\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\node at (5cm, 0cm) {the}
child {node at (-1.5cm, -.3cm) {quick}
child {node at (-1cm, -.3cm) {brown}}
child {node at (1cm, -.3cm) {fox}}
}
child {node at (1.7cm,-.3cm){jumped}
child {node at (-.5cm, -.3cm) {over}}
child {node at (.8cm, -.3cm) {the}}
};
\end{tikzpicture}
\end{frame}
\begin{frame}
\begin{tikzpicture}
\node [red] at (5cm, 0cm) {now}
child {node [red] at (-1.5cm, -.3cm) {is}
child {node [red] at (-1cm, -.3cm) {the}}
child {node [red] at (1cm, -.3cm) {time}}
}
child {node [red] at (1.7cm,-.3cm){for}
child {node [red] at (-.5cm, -.3cm) {all}}
child {node [red] at (.8cm, -.3cm) {good}}
};
\end{tikzpicture}
\end{frame}
\end{document}
答案1
您可以使用\alt
宏来交换覆盖层之间的文本。
为了避免树在覆盖之间移动,我建议将其与节点的固定宽度相结合,并\strut
赋予它们相同的高度:
\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}
\begin{tikzpicture}[every node/.style={text width=1.3cm,align=center}]
\node at (5cm, 0cm) {\strut\alt<3->{\alert{now}}{the}}
child {node at (-1.5cm, -.3cm) {quick}
child {node at (-1cm, -.3cm) {brown}}
child {node at (1cm, -.3cm) {fox}}
}
child {node at (1.7cm,-.3cm){jumped}
child {node at (-.5cm, -.3cm) {over}}
child {node at (.8cm, -.3cm) {the}}
};
\end{tikzpicture}
\end{frame}
\end{document}