tikzpictures 在 beamer 中的位置问题

tikzpictures 在 beamer 中的位置问题

考虑以下 MWE:

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{tikz,alphalph,amsmath}
\setbeamertemplate{navigation symbols}{}
\usetheme{AnnArbor}
\usecolortheme{dolphin}
\setbeamercolor{frametitle}{fg=structure,bg=white}
\setbeamerfont{frametitle}{shape=\rm\bfseries}
\newcommand{\caesar}[1]{
    \centering
    \begin{tikzpicture}[every node/.style={draw,minimum width=1cm,minimum height=1cm}]
        \foreach \k in {1,...,26}
        {
            \pgfmathsetmacro\secure{int(\k+#1)}
            \ifnum\k=26
                \pgfmathsetmacro\mainx{mod(\k,26)}
            \else
                \pgfmathsetmacro\mainx{mod(\k,26)-1}
            \fi
            \pgfmathsetmacro\xpos{mod(\mainx,5)}
            \pgfmathsetmacro\testnumber{mod(\k,5)}

            \ifcase\testnumber=0
                \pgfmathsetmacro\ypos{-floor(\k/5)+1}
            \else
                \pgfmathsetmacro\ypos{-floor(\k/5)}
            \fi


            \pgfmathsetmacro\letter{int(mod(\secure,26))}
            \ifnum\letter=0
                \pgfmathsetmacro\letter{26}
            \else\fi

            \node at (\xpos,\ypos) {\strut\alphalph{\letter}};
        }

        \node[draw=none] at (7,0) {$(x+\textcolor{red}{e})\mod 26$ mit $\textcolor{red}{e} = #1$};
    \end{tikzpicture}
}
\begin{document}
    \title{\bf Title}
    \author{Name}
    \institute{Institute}
    \maketitle

    \begin{frame}{Cäsar-Verschlüsselung}
        \only<1>{
            \caesar{0}
        }
        \only<2>{
            \caesar{1}
        }
    \end{frame}
\end{document}

截屏

如您所见,第三帧中的 tikzpicture 的位置与第二帧中的位置不同(稍微向右偏移了一点)。我猜它与 alphalph 包用于存放字母的某种框有关。类似这样。

我的问题是:我该如何修复这个“错误”(意思是第三帧出现时没有任何变化)?

答案1

实际原因要简单得多。您在使用方法时包含了虚假空格\only,这导致了偏移。您必须确保使用以下方法注释掉行尾%

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{tikz,alphalph,amsmath}
\setbeamertemplate{navigation symbols}{}
\usetheme{AnnArbor}
\usecolortheme{dolphin}
\setbeamercolor{frametitle}{fg=structure,bg=white}
\setbeamerfont{frametitle}{shape=\rm\bfseries}
\newcommand{\caesar}[1]{
    \centering
    \begin{tikzpicture}[every node/.style={draw,minimum width=1cm,minimum height=1cm}]
        \foreach \k in {1,...,26}
        {
            \pgfmathsetmacro\secure{int(\k+#1)}
            \ifnum\k=26
                \pgfmathsetmacro\mainx{mod(\k,26)}
            \else
                \pgfmathsetmacro\mainx{mod(\k,26)-1}
            \fi
            \pgfmathsetmacro\xpos{mod(\mainx,5)}
            \pgfmathsetmacro\testnumber{mod(\k,5)}

            \ifcase\testnumber=0
                \pgfmathsetmacro\ypos{-floor(\k/5)+1}
            \else
                \pgfmathsetmacro\ypos{-floor(\k/5)}
            \fi


            \pgfmathsetmacro\letter{int(mod(\secure,26))}
            \ifnum\letter=0
                \pgfmathsetmacro\letter{26}
            \else\fi

            \node at (\xpos,\ypos) {\strut\makebox[1em]{\alphalph{\letter}}};
        }

        \node[draw=none] at (7,0) {$(x+\textcolor{red}{e})\mod 26$ mit $\textcolor{red}{e} = #1$};
    \end{tikzpicture}
}
\begin{document}
    \title{\bf Title}
    \author{Name}
    \institute{Institute}
    \maketitle

    \begin{frame}{Cäsar-Verschlüsselung}
        \only<1>{%
            \caesar{0}%
        }%
        \only<2>{%
            \caesar{1}%
        }%
    \end{frame}
\end{document}

在此处输入图片描述

相关内容