投影機中 tikzpicture 的位置問題

投影機中 tikzpicture 的位置問題

考慮以下 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}

在此輸入影像描述

相關內容