创建交互式幻灯片 (beamer) #2

创建交互式幻灯片 (beamer) #2

这是这个

在上一个问题中,我询问了如何创建包含两部分的交互式幻灯片:一个左侧部分和一个右侧部分;通常集中在左侧部分。

现在,我需要右侧部分的帮助。请考虑以下示例:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzstyle{int}=[draw, fill=blue!20, minimum size=2em]
\tikzstyle{init} = [pin edge={to-,thin,black}]

    \begin{document}
    \begin{frame}
    \begin{minipage}{0.2\textwidth}
    \begin{enumerate}
    \item this is 1
    \item this is 2
    \end{enumerate}
    \end{minipage}
    \hfill
    \begin{minipage}{0.75\textwidth}
    \only<1> {

    \begin{tikzpicture}[node distance=2.5cm,auto,>=latex']
        \node [int, pin={[init]above:$v_0$}] (a) {$\frac{1}{s}$};
        \node (b) [left of=a,node distance=2cm, coordinate] {a};
        \node [int, pin={[init]above:$p_0$}] (c) [right of=a] {$\frac{1}{s}$};
        \node [coordinate] (end) [right of=c, node distance=2cm]{};
        \path[->] (b) edge node {$a$} (a);
        \path[->] (a) edge node {$v$} (c);
    \end{tikzpicture}}
    \only<2> {
    \begin{tikzpicture}[node distance=2.5cm,auto,>=latex']
        \node [int, pin={[init]above:$v_0$}] (a) {$\frac{1}{s}$};
        \node (b) [left of=a,node distance=2cm, coordinate] {a};
        \node [int, pin={[init]above:$p_0$}] (c) [right of=a] {$\frac{1}{s}$};
        \node [coordinate] (end) [right of=c, node distance=2cm]{};
        \path[->] (b) edge node {$a$} (a);
        \path[->] (a) edge node {$v$} (c);
        \draw[->] (c) edge node {$p$} (end) ;
    \end{tikzpicture}}
    \end{minipage}
    \end{frame}

    \end{document}

免责声明:图片来源均无耻地抄袭自这里

您会明显注意到,除了第二个中的小改动外tikzpicture,复制了相同的代码。我该如何避免这种情况?在上一个问题中,我使用两个单独的文件并包含图像解决了这个问题。但是,难道没有一些巧妙的方法吗?

答案1

避免代码重复的一个选择是使用visible onthis answer思维导图 tikzpicture 在 beamer 中 (逐步显示);另外,考虑到 TikZ 中的路径是覆盖感知的,所以这也可能帮助您避免不必要地重复代码或使用“外部”覆盖规范:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}

\tikzset{
  int/.style={
    draw, 
    fill=blue!20, 
    minimum size=2em
  },  
  init/.style={pin edge={to-,thin,black}}
}

\tikzset{
  % use option [visible on=<+->] to uncover parts of a tikzpicture
  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{minipage}{0.2\textwidth}
    \begin{enumerate}
    \item this is 1
    \item this is 2
    \end{enumerate}
    \end{minipage}
    \hfill
    \begin{minipage}{0.75\textwidth}
    \begin{tikzpicture}[node distance=1.5cm,auto,>=latex']
        \node [int, pin={[init]above:$v_0$}] (a) {$\frac{1}{s}$};
        \node (b) [left = of a,coordinate] {a};
        \node [int, pin={[init]above:$p_0$}] (c) [right =of a] {$\frac{1}{s}$};
        \node [coordinate] (end) [right = of c]{};
        \path[->] (b) edge node {$a$} (a);
        \path[->] (a) edge node {$v$} (c);
        \draw[->,visible on=<2>] (c) edge node {$p$} (end) ;
    \end{tikzpicture}
    \end{minipage}
    \end{frame}

    \end{document}

在此处输入图片描述

风格也是aobs-tikz包裹。

我改变了原来的代码\tikzset,并使用了=of语法(带有positioning库),而不是有问题的of=语法。

以下是代码,结合了两个问题的解决方案:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,tikzmark}

\newcounter{tmp}
\newcommand<>\Highlight[1]{%
  \stepcounter{tmp}%
  \only#2{%
    \begin{tikzpicture}[remember picture,overlay]
      \fill[green!60!black,opacity=0.5] 
      ([xshift=-.2em,yshift=2ex]pic cs:start-\thetmp)
        rectangle  
      ([xshift=.2em,yshift=-1ex]pic cs:end-\thetmp);
    \end{tikzpicture}}%
  \tikzmark{start-\thetmp}#1\tikzmark{end-\thetmp}%
}

\tikzset{
  int/.style={
    draw, 
    fill=blue!20, 
    minimum size=2em
  },  
  init/.style={pin edge={to-,thin,black}}
}

\tikzset{
  % use option [visible on=<+->] to uncover parts of a tikzpicture
  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{minipage}{0.2\textwidth}
\begin{enumerate}
\item \Highlight<+>{this is 1}
\item \Highlight<+>{this is 2}
\end{enumerate}
\end{minipage}
\hfill
\begin{minipage}{0.75\textwidth}
\begin{tikzpicture}[node distance=1.5cm,auto,>=latex']
    \node [int, pin={[init]above:$v_0$}] (a) {$\frac{1}{s}$};
    \node (b) [left = of a,coordinate] {a};
    \node [int, pin={[init]above:$p_0$}] (c) [right =of a] {$\frac{1}{s}$};
    \node [coordinate] (end) [right = of c]{};
    \path[->] (b) edge node {$a$} (a);
    \path[->] (a) edge node {$v$} (c);
    \draw[->,visible on=<2>] (c) edge node {$p$} (end) ;
\end{tikzpicture}
\end{minipage}
\end{frame}

\end{document}

在此处输入图片描述

相关内容