如何修复投影仪框架中的 tikzpicture 垂直位置?

如何修复投影仪框架中的 tikzpicture 垂直位置?

在下面的 MWE 中,我必须将相同的图形和下面的一些内容放在一个框架中。它可以工作,但是当从第 1 帧切换到第 2 帧时,tikzpicture 元素会发生偏移,从而产生不良的视觉效果。我该如何消除这种偏移?

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

\begin{document}

\begin{frame}
  \frametitle{FRAME 1}
  \centering
  \begin{tikzpicture}
    \node[draw=black] (a) {A};
    \node[draw=black, right=of a] (b) {B};
    \draw (a) -- (b);
  \end{tikzpicture}
  \begin{block}{Block on frame 1}
    \begin{itemize}
    \item Itemize 1 on frame 1
    \item Itemize 2 on frame 2
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}
  \frametitle{FRAME 2}
  \centering
  \begin{tikzpicture}
    \node[draw=black] (a) {A};
    \node[draw=black, right=of a] (b) {B};
    \draw (a) -- (b);
  \end{tikzpicture}
  \begin{block}{Block on frame 1}
    \begin{itemize}
    \item Itemize 1 on frame 2
    \item Itemize 2 on frame 2
    \item Itemize 3 on frame 2
    \end{itemize}
  \end{block}
\end{frame}

\end{document}

答案1

两个选项:

  1. t作为两个 s 的可选参数使用frame;这样,框架的内容将顶部对齐:

    \documentclass{beamer}
    \usepackage{tikz}
    \usetikzlibrary{positioning}
    
    \begin{document}
    
    \begin{frame}[t]
      \frametitle{FRAME 1}
      \centering
      \begin{tikzpicture}
        \node[draw=black] (a) {A};
        \node[draw=black, right=of a] (b) {B};
        \draw (a) -- (b);
      \end{tikzpicture}
      \begin{block}{Block on frame 1}
        \begin{itemize}
        \item Itemize 1 on frame 1
        \item Itemize 2 on frame 1
        \end{itemize}
      \end{block}
    \end{frame}
    
    \begin{frame}[t]
      \frametitle{FRAME 2}
      \centering
      \begin{tikzpicture}
        \node[draw=black] (a) {A};
        \node[draw=black, right=of a] (b) {B};
        \draw (a) -- (b);
      \end{tikzpicture}
      \begin{block}{Block on frame 2}
        \begin{itemize}
        \item Itemize 1 on frame 2
        \item Itemize 2 on frame 2
        \item Itemize 3 on frame 2
        \end{itemize}
      \end{block}
      \begin{tikzpicture}
        \node[draw=cyan,fill=orange!30] (c) {C};
        \node[draw=cyan,fill=orange!30,below=of c] (d) {D};
        \draw (c) -- (d);
      \end{tikzpicture}
    \end{frame}
    
    \end{document}
    

    在此处输入图片描述

  2. 使用overlayarea环境(根据需要调整环境的高度)。这允许您在第二帧的末尾拥有其他类型的对象,并且仍将保留常见对象的垂直位置:

    \documentclass{beamer}
    \usepackage{tikz}
    \usetikzlibrary{positioning}
    
    \begin{document}
    
    \begin{frame}
      \frametitle{FRAME 1}
    \begin{overlayarea}{\textwidth}{.6\textheight}
      \centering
      \begin{tikzpicture}
        \node[draw=black] (a) {A};
        \node[draw=black, right=of a] (b) {B};
        \draw (a) -- (b);
      \end{tikzpicture}
      \begin{block}{Block on frame 1}
        \begin{itemize}
        \item Itemize 1 on frame 1
        \item Itemize 2 on frame 1
        \end{itemize}
      \end{block}
    \end{overlayarea}
    \end{frame}
    
    \begin{frame}
      \frametitle{FRAME 2}
    \begin{overlayarea}{\textwidth}{.6\textheight}
      \centering
      \begin{tikzpicture}
        \node[draw=black] (a) {A};
        \node[draw=black, right=of a] (b) {B};
        \draw (a) -- (b);
      \end{tikzpicture}
      \begin{block}{Block on frame 2}
        \begin{itemize}
        \item Itemize 1 on frame 2
        \item Itemize 2 on frame 2
        \item Itemize 3 on frame 2
        \end{itemize}
      \end{block}
    \end{overlayarea}
    \end{frame}
    
    \end{document}
    

    结果动画:

    在此处输入图片描述

    第二帧中还有一些物体:

    \documentclass{beamer}
    \usepackage{tikz}
    \usetikzlibrary{positioning}
    
    \begin{document}
    
    \begin{frame}
      \frametitle{FRAME 1}
    \begin{overlayarea}{\textwidth}{.6\textheight}
      \centering
      \begin{tikzpicture}
        \node[draw=black] (a) {A};
        \node[draw=black, right=of a] (b) {B};
        \draw (a) -- (b);
      \end{tikzpicture}
      \begin{block}{Block on frame 1}
        \begin{itemize}
        \item Itemize 1 on frame 1
        \item Itemize 2 on frame 1
        \end{itemize}
      \end{block}
    \end{overlayarea}
    \end{frame}
    
    \begin{frame}
      \frametitle{FRAME 2}
    \begin{overlayarea}{\textwidth}{.6\textheight}
      \centering
      \begin{tikzpicture}
        \node[draw=black] (a) {A};
        \node[draw=black, right=of a] (b) {B};
        \draw (a) -- (b);
      \end{tikzpicture}
      \begin{block}{Block on frame 2}
        \begin{itemize}
        \item Itemize 1 on frame 2
        \item Itemize 2 on frame 2
        \item Itemize 3 on frame 2
        \end{itemize}
      \end{block}
      \begin{tikzpicture}
        \node[draw=cyan,fill=orange!30] (c) {C};
        \node[draw=cyan,fill=orange!30,below=of c] (d) {D};
        \draw (c) -- (d);
      \end{tikzpicture}
    \end{overlayarea}
    \end{frame}
    
    \end{document}
    

    结果:

    在此处输入图片描述

答案2

问题是每个框架都是垂直居中的。修复该问题的一种方法是使用\pause

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

\begin{document}

\begin{frame}
  \frametitle{FRAME 1}
  \centering
  \begin{tikzpicture}
    \node[draw=black] (a) {A};
    \node[draw=black, right=of a] (b) {B};
    \draw (a) -- (b);
  \end{tikzpicture}
  \begin{block}{Block on frame 1}
    \begin{itemize}
    \item Itemize 1 on frame 1
    \item Itemize 2 on frame 2
  \pause
    \item Itemize 3 on frame 2
    \end{itemize}
  \end{block}
\end{frame}

\end{document}

另一种方法是添加精确的空间量来弥补高度差异。唉,beamer这并不容易。坦率地说,捏造比计算更容易(大约1.22\baselineskip)。

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

\newsavebox{\tempboxa}
\newsavebox{\tempboxb}

\begin{document}

\makeatletter
\savebox{\tempboxa}{\begin{minipage}{\textwidth}%
\let\beamer@cramped=\medskip
  \begin{itemize}
    \item Itemize 1 on frame 1
    \item Itemize 2 on frame 2
  \end{itemize}%
\end{minipage}}

\savebox{\tempboxb}{\begin{minipage}{\textwidth}%
\let\beamer@cramped=\medskip
  \begin{itemize}
    \item Itemize 1 on frame 1
    \item Itemize 2 on frame 2
    \item Itemize 3 on frame 2
  \end{itemize}%
\end{minipage}}
\makeatother

\begin{frame}
  \frametitle{FRAME 1}
  \centering
  \begin{tikzpicture}
    \node[draw=black] (a) {A};
    \node[draw=black, right=of a] (b) {B};
    \draw (a) -- (b);
  \end{tikzpicture}
  \begin{block}{Block on frame 1}

    \usebox{\tempboxa}

    \vspace{\dimexpr \ht\tempboxb+\dp\tempboxb-\ht\tempboxa-\dp\tempboxa}
  \end{block}
\end{frame}

\begin{frame}
  \frametitle{FRAME 2}
  \centering
  \begin{tikzpicture}
    \node[draw=black] (a) {A};
    \node[draw=black, right=of a] (b) {B};
    \draw (a) -- (b);
  \end{tikzpicture}
  \begin{block}{Block on frame 2}

    \usebox{\tempboxb}
  \end{block}
\end{frame}

\end{document}

相关内容