我正在使用用户 Daniel 描述的非常优雅的方法这里尝试逐渐显示我的 tikz 树。简要的想法是,树始终完全绘制以避免“跳跃”节点,即使它们是“不可见的”,也要使它们的坐标可用,但在那些不应该被看到的节点上将不透明度设置为 0。
揭晓后,我的完整树应该是这样的(我提前为我的手机相机质量不佳道歉):
这是我的代码:
\documentclass{beamer}
\usepackage{tikz}
\usepackage[utf8]{inputenc}
\usetikzlibrary{trees}
% Daniel's proposal for "uncovering" parts of a tikz-tree %
\tikzset{
invisible/.style={opacity=0},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
},
}
\begin{document}
\begin{frame}
\begin{tikzpicture}[
nor/.style={circle,thick,solid,draw=black,fill=none,inner sep=0.5mm, minimum size=5mm},
acc/.style={circle,thick,solid,draw=green!50!black,fill=green!20,inner sep=0.5mm, minimum size=5mm},
rej/.style={circle,thick,solid,draw=red!50,fill=red!20,inner sep=0.5mm, minimum size=5mm},
level 1/.style={every child/.style={edge from parent/.style={dashed,draw,nearly opaque}} },
level 2/.style={sibling distance=19mm,every child/.style={edge from parent/.style={solid,draw}}},
level 3/.style={sibling distance=8mm,every child/.style={edge from parent/.style={dashed,draw,nearly opaque}} },
semithick]
\node[draw=none] (root) {}
child[level distance=12mm,visible on=<1->] { node[nor] {$x$}
child[visible on=<2->] {node[acc] {$e_1(x)$} }
child[visible on=<2->] {node[nor] {$e_2(x)$}
child[visible on=<6->] child[visible on=<6->] }
child[visible on=<2->] {node[rej] {$e_3(x)$} }
child[visible on=<2->] {node[nor] {$e_4(x)$}
child[visible on=<6->] child[visible on=<6->] child[visible on=<6->] }
};
\end{tikzpicture}
\end{frame}
\end{document}
第 2 级的 4 个同级节点被正确隐藏,包括它们与 (x) 节点的实线边缘。正如预期的那样,它们出现在第二张幻灯片中,然后保持可见。
但是,从第一张幻灯片开始,树底部的虚线边缘就会显示出来——即使叶节点设置为从第 6 张幻灯片开始可见。
为什么会发生这种情况?这种做法被接受为答案,并在原始问题中获得了很多赞扬,所以它肯定有效。它只是部分适用于我,因为我似乎对如何child
将其样式继承到其下方的所有节点有错误的理解……或者也许是级别也是手动设置样式的问题(尽管这似乎不会影响级别 2)。
关于如何修复此问题有什么想法?
答案1
前言
由于 MHaaZ 的评论,我尝试调查他的代码到底出了什么问题,但我发现我原来的答案不够正确。我会把它留在这里作为记录,但我更愿意写一个新的。
新答案
您的代码的唯一问题是使用nearly opaque
选项level 1
和level 3
样式定义。我仍然不太明白它是如何visible on
工作的,但它opacity=0
为所有不可见的部分设置并nearly opaque
更改此初始值。
我可以提供两种解决方案。第一种是确保visible on
在设置后应用该选项opacity
。这样,当边或节点可见时,它们将保持不透明度。类似
level 1/.style={every child/.style={edge from parent/.style={dashed,draw,nearly opaque, visible on=<2->}}},
将解决问题(看看原始答案代码如下)。
如果只想nearly opaque
选择fade
虚线,另一种解决方案是使用whiten
它们。我认为dashed, draw=black!30
或类似的方法会产生类似的效果并避免与冲突visible on
。使用第二种解决方案的代码将是:
\documentclass{beamer}
\usepackage{tikz}
\usepackage{inputenc}[utf8]
\usetikzlibrary{trees}
% Daniel's proposal for "uncovering" parts of a tikz-tree %
\tikzset{
invisible/.style={opacity=0},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
},
}
\begin{document}
\begin{frame}
\begin{tikzpicture}[
nor/.style={circle,thick,solid,draw=black,fill=none,inner sep=0.5mm, minimum size=5mm},
acc/.style={circle,thick,solid,draw=green!50!black,fill=green!20,inner sep=0.5mm, minimum size=5mm},
rej/.style={circle,thick,solid,draw=red!50,fill=red!20,inner sep=0.5mm, minimum size=5mm},
level 1/.style={every child/.style={edge from parent/.style={dashed,draw=black!30}} },
level 2/.style={sibling distance=19mm,every child/.style={edge from parent/.style={solid,draw=black}}},
level 3/.style={sibling distance=8mm,every child/.style={edge from parent/.style={dashed,draw=black!30}} },
semithick]
\node[draw=none] (root) {}
child[level distance=12mm,visible on=<1->] { node[nor] {$x$}
child[visible on=<2->] {node[acc] {$e_1(x)$} }
child[visible on=<2->] {node[nor] {$e_2(x)$}
child[visible on=<6->] child[visible on=<6->] }
child[visible on=<2->] {node[rej] {$e_3(x)$} }
child[visible on=<2->] {node[nor] {$e_4(x)$}
child[visible on=<6->] child[visible on=<6->] child[visible on=<6->] }
};
\end{tikzpicture}
\end{frame}
\end{document}
最后一帧看起来如下:
原始答案
Anode
及其对应项edge from parent
可以用不同的操作绘制,因此您可以(或必须)将visible on
选项应用于每个特定的edge
。即使应用于visible on
特定level
样式,您也可以在特定节点定义内更改它。
\documentclass{beamer}
\usepackage{tikz}
\usepackage[utf8]{inputenc}
\usetikzlibrary{trees}
% Daniel's proposal for "uncovering" parts of a tikz-tree %
\tikzset{
invisible/.style={opacity=0},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
},
}
\begin{document}
\begin{frame}
\begin{tikzpicture}[
ball/.style={circle, thick, solid,inner sep=0.5mm, minimum size=5mm},
nor/.style={ball,draw=black,fill=none},
acc/.style={ball,draw=green!50!black,fill=green!20},
rej/.style={ball,draw=red!50,fill=red!20},
level 1/.style={every child/.style={edge from parent/.style={dashed,draw,nearly opaque, visible on=<2->}}},
level 2/.style={sibling distance=19mm,every child/.style={edge from parent/.style={solid,draw,visible on=<2->}}},
level 3/.style={sibling distance=8mm,every child/.style={edge from parent/.style={dashed,draw,nearly opaque, visible on=<5->}} },
semithick]
\node[draw=none] (root) {}
child[level distance=12mm,visible on=<1->] { node[nor] {$x$}
child[visible on=<2->] {node[acc] {$e_1(x)$} }
child[visible on=<3->] {node[nor] {$e_2(x)$} edge from parent[red, visible on=<4->]
child[visible on=<6->] child[visible on=<6->] }
child[visible on=<2->] {node[rej] {$e_3(x)$} }
child[visible on=<3->] {node[nor] {$e_4(x)$}
child[visible on=<6->] {} child[visible on=<6->] child[visible on=<6->] }
};
\end{tikzpicture}
\end{frame}
\end{document}