从 tikz 树中删除节点

从 tikz 树中删除节点

考虑以下 TeX 文档:

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{trees}

\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}[overlay,remember picture,
      level 1/.style={level distance=12mm,every child/.style={edge from parent/.style={->,solid,draw=blue}} },
      level 2/.style={sibling distance=12mm,every child/.style={edge from parent/.style={->,solid,draw=blue}}},
      level 3/.style={sibling distance=8mm,every child/.style={edge from parent/.style={->,solid,draw=blue}}},
      semithick]

      \node[draw=none,text=red] {S}
      child[visible on=<2->] {node[draw=none,text=red] (a)  {T}
        child[visible on=<3>] {node[draw=none,text=red] (b) {L}}
        child[visible on=<4->] {node[draw=none,text=red] (c) {K}}
        };

    \end{tikzpicture}
\end{frame}
\end{document}

目的是删除节点“b”并添加另一个节点“c”。两个节点都需要作为节点 a 的子节点出现并出现在同一位置。

答案1

不确定我是否完全理解,但是这能达到你想要的效果吗?

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{trees}

\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}[overlay,remember picture,
      level 1/.style={level distance=12mm,every child/.style={edge from parent/.style={->,solid,draw=blue}} },
      level 2/.style={sibling distance=12mm,every child/.style={edge from parent/.style={->,solid,draw=blue}}},
      level 3/.style={sibling distance=8mm,every child/.style={edge from parent/.style={->,solid,draw=blue}}},
      semithick]

      \node[draw=none,text=red] {S}
      child[visible on=<2->] {node[draw=none,text=red] (a)  {\only<2,4->{T}\only<3>{L}}
%        child[visible on=<3>] {node[draw=none,text=red] (b) {L}}
        child[visible on=<4->] {node[draw=none,text=red] (c) {K}}
        };

    \end{tikzpicture}
\end{frame}
\end{document}

相关内容