如何在 Beamer 中使用 Tikz Matrix 正确叠加?

如何在 Beamer 中使用 Tikz Matrix 正确叠加?

有人知道为什么我的最后一个节点在移动吗?我正在尝试覆盖,如这个帖子。 非常感谢!

\documentclass[xcolor=dvipsnames,10pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

% Vectors
\begin{frame}[fragile]{Vectors}
    \begin{figure}
        \centering
        \begin{tikzpicture} [nodes in empty cells, nodes={minimum width=0.7cm, minimum height=0.7cm}, row sep=-\pgflinewidth, column sep=-\pgflinewidth] 
            border/.style={draw}

            \matrix(vector)[matrix of nodes, ampersand replacement=\&, row 1/.style={nodes={draw=none, minimum width=0.3cm}}, nodes={draw}] {
                \scriptsize{0} \& \scriptsize{1} \& \scriptsize{2} \& \scriptsize{3} \& \scriptsize{4} \& \scriptsize{5} \& \scriptsize{6} \& \scriptsize{7} \& \scriptsize{8} \& \scriptsize{9} \& \scriptsize{10} 
                \only<1>{\\ |[fill=red!70]| G \& C\& A \& T \& C \& G \& C \& A \& G \& T \& A}
                \only<2>{\\ G \& |[fill=red!70]| C\& A \& T \& C \& G \& C \& A \& G \& T \& A}
                \only<3>{\\ G \& C\& |[fill=red!70]| A \& T \& C \& G \& C \& A \& G \& T \& A}
                \only<4>{\\ G \& C\& A \& |[fill=red!70]| T \& C \& G \& C \& A \& G \& T \& A}
                \only<5>{\\ G \& C\& A \& |[fill=red!70]| T \& |[fill=red!70]| C \& G \& C \& A \& G \& T \& A}
                \only<6>{\\ G \& C\& A \& |[fill=red!70]| T \& |[fill=red!70]| C \& |[fill=red!70]| G \& C \& A \& G \& T \& A}
                \only<7>{\\ G \& C\& A \& |[fill=red!70]| T \& |[fill=red!70]| C \& |[fill=red!70]| G \& |[fill=red!70]| C \& A \& G \& T \& A}
                \only<8>{\\ G \& C\& A \& |[fill=red!70]| T \& |[fill=red!70]| C \& |[fill=red!70]| G \& |[fill=red!70]| C \& |[fill=red!70]| A \& G \& T \& A}\\
            };
        \end{tikzpicture}

        \vspace{-.3in}
        \begin{tikzpicture} [nodes in empty cells, nodes={minimum width=0.7cm, minimum height=0.7cm}, row sep=-\pgflinewidth, column sep=-\pgflinewidth] 
            border/.style={draw}

            \matrix(vector)[matrix of nodes, ampersand replacement=\&, row 1/.style={nodes={draw=none, text width=0.3cm}}, nodes={draw}] {
                \& \& \& \& \&
                \only<1>{\\ |[fill=red!70]| T \& C \& G \& C \& A}
                \only<2>{\\ |[fill=red!70]| T \& C \& G \& C \& A}
                \only<3>{\\ |[fill=red!70]| T \& C \& G \& C \& A}
                \only<4>{\\ |[fill=red!70]| T \& C \& G \& C \& A}
                \only<5>{\\ |[fill=red!70]| T \& |[fill=red!70]| C \& G \& C \& A}
                \only<6>{\\ |[fill=red!70]| T \& |[fill=red!70]| C \& |[fill=red!70]| G \& C \& A}
                \only<7>{\\ |[fill=red!70]| T \& |[fill=red!70]| C \& |[fill=red!70]| G \& |[fill=red!70]| C \& A}
                \only<8>{\\ |[fill=red!70]| T \& |[fill=red!70]| C \& |[fill=red!70]| G \& |[fill=red!70]| C \& |[fill=red!70]| A}\\
            };
        \end{tikzpicture}
    \end{figure}
\end{frame}

\end{document}

答案1

说实话,我并没有试图找出为什么某些东西会跳转。这是为了宣传overlay-beamer-styles,它允许您压缩代码并避免跳转。语法或多或少是不言自明的。

\documentclass[xcolor=dvipsnames,10pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{overlay-beamer-styles}
\begin{document}

% Vectors
\begin{frame}[fragile]{Vectors}
    \begin{figure}
        \centering
        \begin{tikzpicture} [nodes in empty cells, nodes={minimum width=0.7cm, minimum height=0.7cm}, row sep=-\pgflinewidth, column sep=-\pgflinewidth] 
            border/.style={draw}

            \matrix(vector)[matrix of nodes, ampersand replacement=\&, row 1/.style={nodes={draw=none, minimum width=0.3cm}}, nodes={draw}] {
                \scriptsize{0} \& \scriptsize{1} \& \scriptsize{2} \& \scriptsize{3} \& \scriptsize{4} \& \scriptsize{5} \& \scriptsize{6} \& \scriptsize{7} \& \scriptsize{8} \& \scriptsize{9} \& \scriptsize{10} 
                \\ 
                |[fill=red!70,fill on=<1>]| G \& 
                |[fill=red!70,fill on=<2>]| C \& 
                |[fill=red!70,fill on=<3>]| A \& 
                |[fill=red!70,fill on=<4-8>]| T \& 
                |[fill=red!70,fill on=<5-8>]| C \& 
                |[fill=red!70,fill on=<6-8>]| G \& 
                |[fill=red!70,fill on=<7-8>]| C \& 
                |[fill=red!70,fill on=<8>]| A \& G \& T \& A
                \\
            };
        \end{tikzpicture}

        \vspace{-.3in}
        \begin{tikzpicture} [nodes in empty cells, nodes={minimum width=0.7cm, minimum height=0.7cm}, row sep=-\pgflinewidth, column sep=-\pgflinewidth] 
            border/.style={draw}

            \matrix(vector)[matrix of nodes, ampersand replacement=\&, row 1/.style={nodes={draw=none, text width=0.3cm}}, nodes={draw}] {
                \& \& \& \& \& \\ 
                |[fill=red!70]| T \& 
                |[fill=red!70,fill on=<5-8>]| C \& 
                |[fill=red!70,fill on=<6-8>]| G \& 
                |[fill=red!70,fill on=<7-8>]| C \& 
                |[fill=red!70,fill on=<8>]| A \\
            };
        \end{tikzpicture}
    \end{figure}
\end{frame}
\end{document}

在此处输入图片描述

fill=red!70您可以通过制作第二行的通用样式并使用将两个矩阵tikzpicture相对放置在一个矩阵中来进一步缩短代码positioning

\documentclass[xcolor=dvipsnames,10pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\usetikzlibrary{overlay-beamer-styles}
\begin{document}

% Vectors
\begin{frame}[fragile]{Vectors}
    \begin{figure}
        \centering
        \begin{tikzpicture} [nodes in empty cells, nodes={minimum width=0.7cm,
        minimum height=0.7cm}, row sep=-\pgflinewidth, column
        sep=-\pgflinewidth,
        my mat/.style={matrix of nodes, ampersand replacement=\&, 
           row 1/.style={nodes={draw=none, minimum width=0.3cm}}, 
            row 2/.style={nodes={draw,fill=red!70}}}] 
            border/.style={draw}

            \matrix(vector1)[my mat,row 1/.append style={font=\scriptsize}] {
                0 \& 1 \& 2 \& 3 \& 4 \& 5 \& 6 \& 7 \& 8 \& 9 \& 10 \\ 
                |[fill on=<1>]| G \& 
                |[fill on=<2>]| C \& 
                |[fill on=<3>]| A \& 
                |[fill on=<4-8>]| T \& 
                |[fill on=<5-8>]| C \& 
                |[fill on=<6-8>]| G \& 
                |[fill on=<7-8>]| C \& 
                |[fill on=<8>]| A \& 
                |[fill=white]| G \& 
                |[fill=white]|T \& 
                |[fill=white]|A
                \\
            };

            \matrix(vector2)[my mat,below=1mm of vector1] {
                \& \& \& \& \& \\ 
                |[fill on=<4-8>]| T \& 
                |[fill on=<5-8>]| C \& 
                |[fill on=<6-8>]| G \& 
                |[fill on=<7-8>]| C \& 
                |[fill on=<8>]| A \\
            };
        \end{tikzpicture}
    \end{figure}
\end{frame}
\end{document}

相关内容