如何使用 TikZ 节点属性制作 Beamer 覆盖?

如何使用 TikZ 节点属性制作 Beamer 覆盖?

作为后续问题Beamer 中的 Tikz 动画人物,我怎样才能为节点的特定属性制作动画?

平均能量损失

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{graphs}

\tikzset{
  node distance = 3 cm,
  graph vertex/.style={
    circle,
    draw,
    minimum size=7.5mm,
  },
  marked/.style={
    color=white,
    fill=black,
  },
}

\begin{document}
\begin{frame}
  \centering
    \begin{tikzpicture}
      \node[graph vertex, marked] (1) at (0 , 3) {$v_1$};
      \node[graph vertex,       ] (2) at (0 , 6) {$v_2$};
      \node[graph vertex,       ] (3) at (3 , 3) {$v_3$};
      \node[graph vertex, marked] (4) at (3 , 6) {$v_4$};
      \node[graph vertex,       ] (5) at (5 , 8) {$v_5$};

      \graph {
        (1) -- (3) -- (4) -- (2) --[bend left] (5),
        (1) -- (2),
        (1) -- (4),
        (3) --[bend right] (5)
      };
  \end{tikzpicture}
\end{frame}

\begin{frame}
  \centering
  \begin{tikzpicture}
      \node[graph vertex, marked] (1) at (0 , 3) {$v_1$};
      \node[graph vertex,       ] (2) at (0 , 6) {$v_2$};
      \node[graph vertex,       ] (3) at (3 , 3) {$v_3$};
      \node[graph vertex, marked] (4) at (3 , 6) {$v_4$};
      \node[graph vertex, marked] (5) at (5 , 8) {$v_5$};

      \graph {
        (1) -- (3) -- (4) -- (2) --[bend left] (5),
        (1) -- (2),
        (1) -- (4),
        (3) --[bend right] (5)
      };
  \end{tikzpicture}
\end{frame}

\begin{frame}
  \centering
  \begin{tikzpicture}
      \node[graph vertex, marked] (1) at (0 , 3) {$v_1$};
      \node[graph vertex,       ] (2) at (0 , 6) {$v_2$};
      \node[graph vertex,       ] (3) at (3 , 3) {$v_3$};
      \node[graph vertex,       ] (4) at (3 , 6) {$v_4$};
      \node[graph vertex, marked] (5) at (5 , 8) {$v_5$};

      \graph {
        (1) -- (3) -- (4) -- (2) --[bend left] (5),
        (1) -- (2),
        (1) -- (4),
        (3) --[bend right] (5)
      };
  \end{tikzpicture}
\end{frame}
\end{document}

我想让这个语法变成类似

\begin{frame}
  \centering
    \begin{tikzpicture}
      \node[graph vertex, marked      ] (1) at (0 , 3) {$v_1$};
      \node[graph vertex,             ] (2) at (0 , 6) {$v_2$};
      \node[graph vertex,             ] (3) at (3 , 3) {$v_3$};
      \node[graph vertex, marked=<1-2>] (4) at (3 , 6) {$v_4$};
      \node[graph vertex, marked=<2-3>] (5) at (5 , 8) {$v_5$};

      \graph {
        (1) -- (3) -- (4) -- (2) --[bend left] (5),
        (1) -- (2),
        (1) -- (4),
        (3) --[bend right] (5)
      };
  \end{tikzpicture}
\end{frame}

我也对其他语法选项持开放态度,只要它“或多或少”符合上述要求(即不重复代码,可读性强)。

尝试overlay-beamer-styles

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{graphs}
\usetikzlibrary{overlay-beamer-styles}

\tikzset{
  node distance = 3 cm,
  graph vertex/.style={
    circle,
    draw,
    minimum size=7.5mm,
  },
  marked/.style={
    color=white,
    fill=black,
  },
  marked on/.style={alt=#1{}{marked}},
}

\begin{document}
\begin{frame}
  \centering
    \begin{tikzpicture}
      \node[graph vertex, marked         ] (1) at (0 , 3) {$v_1$};
      \node[graph vertex,                ] (2) at (0 , 6) {$v_2$};
      \node[graph vertex,                ] (3) at (3 , 3) {$v_3$};
      \node[graph vertex, marked on=<1-2>] (4) at (3 , 6) {$v_4$};
      \node[graph vertex, marked on=<2-3>] (5) at (5 , 8) {$v_5$};

      \graph {
        (1) -- (3) -- (4) -- (2) --[bend left] (5),
        (1) -- (2),
        (1) -- (4),
        (3) --[bend right] (5)
      };
  \end{tikzpicture}
\end{frame}
\end{document}

答案1

overlay-beamer-styles如果您更改代码行,您的代码将起作用

  marked on/.style={alt=#1{}{marked}},

  marked on/.style={alt=#1{marked}{}},

代码:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{graphs}
\usetikzlibrary{overlay-beamer-styles}

\tikzset{
  node distance = 3 cm,
  graph vertex/.style={
    circle,
    draw,
    minimum size=7.5mm,
  },
  marked/.style={
    color=white,
    fill=black,
  },
  marked on/.style={alt=#1{marked}{}},
}

\makeatletter
\def\c@slideinframe{\beamer@slideinframe}
\makeatother

\begin{document}
\begin{frame}
\frametitle{Test slide \arabic{slideinframe}}
  \centering
    \begin{tikzpicture}
      \node[graph vertex, marked         ] (1) at (0 , 3) {$v_1$};
      \node[graph vertex,                ] (2) at (0 , 6) {$v_2$};
      \node[graph vertex,                ] (3) at (3 , 3) {$v_3$};
      \node[graph vertex, marked on=<1-2>] (4) at (3 , 6) {$v_4$};
      \node[graph vertex, marked on=<2-3>] (5) at (5 , 8) {$v_5$};

      \graph {
        (1) -- (3) -- (4) -- (2) --[bend left] (5),
        (1) -- (2),
        (1) -- (4),
        (3) --[bend right] (5)
      };
  \end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

相关内容