如何阻止 tikz 矩阵在 beamer 中跳动

如何阻止 tikz 矩阵在 beamer 中跳动

我有一系列幻灯片/过渡,显示一个矩阵,我在列和圆圈数字之间插入一些间隙,并在某个点在底部添加一个额外的行。我希望矩阵在垂直方向上保持在同一位置,并且当我所做的只是圈出一个数字或在矩阵底部添加一行时,所有元素(被框起来的)都保持在同一位置(请参阅示例以了解这究竟意味着什么)。基本上,我希望它尽可能停止跳来跳去。我该怎么做?

这是一个相当长的例子,显示了这个问题:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, calc, matrix}
\newcommand\Circle[1]{%
    \tikz[baseline=(char.base)]\node[circle,draw,inner sep=2pt] (char) {#1};}

\begin{document}

\begin{frame}[fragile]
    \frametitle{title}
    \begin{center}
        \begin{tikzpicture}
        \matrix (m) [matrix of nodes,
        nodes={draw, minimum size=8mm},
        column sep=3mm,
        row sep=2mm,
        row 1/.style={nodes={draw=none}}]
        {
            0 & 1 & 2 & 3 & 4   \\[-3mm]
            6 & 2 & 4 & 4 & 5   \\
            8 & 4 & 5 & 6 & 6   \\
            7 & 5 & 2 & 7 & 5   \\
            6 & 5 & 1 & 6 & 5   \\
            5 & 9 & 0 & 2 & 4   \\
        };
        \end{tikzpicture}
    \end{center}

    \begin{itemize}
        \item A line of text
    \end{itemize}
\end{frame}

\begin{frame}[fragile]
    \frametitle{title}
    \begin{center}
        \begin{tikzpicture}
        \matrix (m) [matrix of nodes,
        nodes={draw, minimum size=8mm},
        column sep=3mm,
        row sep=2mm,
        row 1/.style={nodes={draw=none}}]
        {
            0 & 1 & 2 & 3 & 4   \\[-3mm]
            6 & 2 & 4 & 4 & 5   \\
            \Circle{8} & 4 & \Circle{5} & 6 & \Circle{6}   \\
            7 & 5 & 2 & \Circle{7} & 5   \\
            6 & 5 & 1 & 6 & 5   \\
            5 & \Circle{9} & 0 & 2 & 4   \\
            &   &   &   &     \\
            \hline \\
            8 & 9 & 5 & 7 & 6   \\
        };
        \end{tikzpicture}
    \end{center}

    \begin{itemize}
        \item Some text
    \end{itemize}
\end{frame}


\begin{frame}[fragile]
    \frametitle{title}
    \begin{center}
        \begin{tikzpicture}
        \matrix (m) [matrix of nodes,
        nodes={draw, minimum size=8mm},
        column sep=3mm,
        row sep=2mm,
        row 1/.style={nodes={draw=none}}]
        {
            0 & 1 &[5mm] 2 &[5mm] 3 & 4   \\[-3mm]
            6 & 2 & 4 & 4 & 5   \\
            8 & 4 & \Circle{5} & 6 & 6   \\
            7 & 5 & 2 & 7 & 5   \\
            6 & 5 & 1 & 6 & 5   \\
            5 & 9 & 0 & 2 & 4   \\
            &   &   &   &     \\
            \hline \\
            8 & 9 & 5 & 7 & 6   \\
        };
        \end{tikzpicture}
    \end{center}

\end{frame}

\begin{frame}[fragile]
    \frametitle{title}
    \begin{center}
        \begin{tikzpicture}
        \matrix (m) [matrix of nodes,
        nodes={draw, minimum size=8mm},
        column sep=3mm,
        row sep=2mm,
        row 1/.style={nodes={draw=none}}]
        {
            0 & 1 &[5mm] 2 & 3 & 4   \\[-3mm]
            6 & 2 & 4 & 4 & 5   \\
            8 & 4 & 5 & 6 & 6   \\
            7 & 5 & 2 & 7 & 5   \\
            6 & 5 & 1 & 6 & 5   \\
            5 & 9 & 0 & 2 & 4   \\
            &   &   &   &     \\
            \hline \\
            8 & 9 & 5 & 7 & 6   \\
        };
        \end{tikzpicture}
    \end{center}
\end{frame}

\begin{frame}[fragile]
    \frametitle{title}
    \begin{center}
        \begin{tikzpicture}
        \matrix (m) [matrix of nodes,
        nodes={draw, minimum size=8mm},
        column sep=3mm,
        row sep=2mm,
        row 1/.style={nodes={draw=none}}]
        {
            0 & 1 & 2  &[5mm] 3 &[5mm]  4   \\[-3mm]
            6 & 2 & 4 & 4 & 5   \\
            8 & 4 & 5 & 6 & 6   \\
            7 & 5 & 2 & 7 & 5   \\
            6 & 5 & 1 & 6 & 5   \\
            5 & 9 & 0 & 2 & 4   \\
            &   &   &   &     \\
            \hline \\
            8 & 9 & 5 & 7 & 6   \\
        };
        \end{tikzpicture}
    \end{center}
\end{frame}

\begin{frame}[fragile]
    \frametitle{title}
    \begin{overlayarea}{\linewidth}{\textheight}
    \begin{center}
        \begin{tikzpicture}
        \matrix (m) [matrix of nodes,
        nodes={draw, minimum size=8mm},
        column sep=3mm,
        row sep=2mm,
        row 1/.style={nodes={draw=none}}]
        {
            0 & 1 & 2  &[5mm] 3 &[5mm]  4   \\[-3mm]
            6 & 2 & 4 & 4 & 5   \\
            8 & 4 & 5 & 6 & 6   \\
            7 & 5 & 2 & \Circle{7} & 5   \\
            6 & 5 & 1 & 6 & 5   \\
            5 & 9 & 0 & 2 & 4   \\
            &   &   &   &     \\
            \hline \\
            8 & 9 & 5 & 7 & 6   \\
        };
        \end{tikzpicture}
    \end{center}
    \pause
    \begin{itemize}
        \item More text 
    \end{itemize}
\end{overlayarea}
\end{frame}
\end{document}

答案1

我只需绘制一次矩阵,然后使用它overlay-beamer-styles来添加更改。您的\Circled命令嵌套了s,我用一种样式和一种变体tikzpicture替换了它,这种样式和变体仅在某些特定叠加层上添加圆圈。例如circledcircled on

|[circled on=<{2,6,7}>]| 7

仅在覆盖层 2、6 和 7 上圈出 7 个节点。我还添加了一个示例,说明如何使用键alt更改特定覆盖层上特定列的列分隔符,

alt=<2>{column 2/.style={column sep=7mm},column 3/.style={column sep=7mm}}{},

但我没有热情去实现所有这些目标。

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, calc, matrix,overlay-beamer-styles}
\tikzset{circled/.style={path picture={
 \draw let \p1=($(path picture bounding box.north east)-(path picture bounding
 box.south west)$),\n1={min(\x1,\y1)/2.2} in
 (path picture bounding box.center) circle[radius=\n1];}},
circled on/.style={alt=#1{circled}{}}}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{title}
\centering
\begin{tikzpicture}
    \matrix (m) [matrix of nodes,
    nodes={draw, minimum size=8mm},
    column sep=3mm,
    alt=<2>{column 2/.style={column sep=7mm},column 3/.style={column sep=7mm}}{},
    % you need to add the other variations of the column seps here
    row sep=2mm,
    row 7/.style={visible on=<4-5>},
    row 1/.style={nodes={draw=none}}] (mat)
    {
        0 & 1 & 2 & 3 & 4   \\[-3mm]
        6 & 2 & 4 & 4 & 5   \\
         |[circled on=<2>]| 8 & 4 &  |[circled on=<{2,3}>]| 5 & 6 &  |[circled on=<2>]| 6   \\
        7 & 5 & 2 & |[circled on=<{2,6,7}>]| 7 & 5   \\
        6 & 5 & 1 & 6 & 5   \\
        5 & |[circled on=<2>]| 9 & 0 & 2 & 4   \\[2mm]
        8 & 9 & 5 & 7 & 6   \\
    };
    \path (mat-6-1.south) -- (mat-7-1.north) coordinate[midway](aux);
    \draw[visible on=<{4,5}>] (mat.west|-aux) -- (mat.east|-aux);
    \end{tikzpicture}


\only<1>{\begin{itemize}
 \item A line of text
\end{itemize}}
\only<2>{\begin{itemize}
 \item Some text
\end{itemize}}
\only<7>{\begin{itemize}
 \item More text
\end{itemize}}
\end{frame}
\end{document}

在此处输入图片描述

如您所见,不再有跳转,代码也更短了(并且,尽管您不一定这样看待它,但最重要的是不再有嵌套tikzpicture)。

附录:添加了仅在某些覆盖层上可见的额外行,并用一条线分隔。可见性由

row 7/.style={visible on=<4-5>},

\draw[visible on=<{4,5}>] (mat.west|-aux) -- (mat.east|-aux);

分别。

相关内容