# Problems with position of tikzpictures in beamer

Consider the following MWE:

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{tikz,alphalph,amsmath}
\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}


As you see, the position of the tikzpicture in the third frame is different from the second one (it's a little bit shifted to the right). I guess it's related a kind of the boxes, which the package alphalph uses for the letters of sth. like that.

My questions is: How can I fix this "bug" (in meaning of that there is no shifting when the third frame appears)?

• Set both tikzpictures to use the same size for their bounding box (use as bounding box option). – Skillmon Feb 23 at 11:35

The actual reason is way simpler. You included spurious spaces with the way you used \only which lead to the shift. You have to make sure to comment out line endings using %:

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{tikz,alphalph,amsmath}
\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}