[Tex/LaTex] Beamer: Centering a tikzpicture in the background (\usebackgroundtemplate)

beamerpositioningtikz-pgf

I really thought that this can't be too hard but I struggle centering (vertically and horizontally) a tikzpicture in the \usebackgroundtemplate.

I use the code taken from one of my previous questions – in the end I want to have a progress indicator in the background of the beamer presentation.

When I use a simple \begin{center}...\end{center} (only horizontally) then I get an error saying

! LaTeX Error: Something's wrong–perhaps a
missing \item.

But \begin{center}...\end{center} works in the normal frame (see MWE).

\documentclass[]{beamer}

\usepackage{tikz}
\usetikzlibrary{decorations}

% taken from https://tex.stackexchange.com/questions/247742
% user https://tex.stackexchange.com/users/23215/mark-wibrow
\makeatletter
\tikzset{%
  get path length/.code={%
    \tikz@addoption{%
      \pgfgetpath\tikz@tmppath%
      \pgfprocessround\tikz@tmppath\tikz@tmppath%
      \pgf@decorate@parsesoftpath\tikz@tmppath\tikz@discard%
      \global\let#1=\pgf@decorate@totalpathlength%
    }%
  }
}
\makeatother

% taken from https://tex.stackexchange.com/questions/347336
% user https://tex.stackexchange.com/users/23215/mark-wibrow
\tikzset{myOwnStyle/.style={
  get path length=\pathlength,
  draw=black,
  line width=1mm,
  postaction={
    draw=red,
    line width=2mm,
    dash pattern= on \pathlength/100*#1 off \pathlength/100*(100-#1),
  }
}}

% I want to vertically and horizontally center this tikzpicture
\usebackgroundtemplate%
{
%\begin{center}
\begin{tikzpicture}
    \draw[myOwnStyle=\n,color=blue] (0,0) circle [radius = 10mm] node {\n\,\%};  
\end{tikzpicture} 
%\end{center}
}

\begin{document}

\foreach \n in {0,10,...,100}{
\begin{frame}
\frametitle{Frame Number: \n}
This is the frame with the number \n.

\begin{center}
\begin{tikzpicture}
    \draw[myOwnStyle=\n] (0,0) circle [radius = 10mm] node {\n\,\%};  
\end{tikzpicture} 
\end{center}

\end{frame}
}

\end{document}

enter image description here

Best Answer

Resize the tikzpicture to the paper using \useasboundingbox and position the circle in the center. There are several mechanisms, I used the coordinate (.5\paperwidth,.5\paperheight). You could also place it at half the diagonal or something else.

N.B.: Do you really want two of these circles on each frame?

\documentclass[]{beamer}

\usepackage{tikz}
\usetikzlibrary{decorations}

% taken from http://tex.stackexchange.com/questions/247742
% user http://tex.stackexchange.com/users/23215/mark-wibrow
\makeatletter
\tikzset{%
  get path length/.code={%
    \tikz@addoption{%
      \pgfgetpath\tikz@tmppath%
      \pgfprocessround\tikz@tmppath\tikz@tmppath%
      \pgf@decorate@parsesoftpath\tikz@tmppath\tikz@discard%
      \global\let#1=\pgf@decorate@totalpathlength%
    }%
  }
}
\makeatother

% taken from http://tex.stackexchange.com/questions/347336
% user http://tex.stackexchange.com/users/23215/mark-wibrow
\tikzset{myOwnStyle/.style={
  get path length=\pathlength,
  draw=black,
  line width=1mm,
  postaction={
    draw=red,
    line width=2mm,
    dash pattern= on \pathlength/100*#1 off \pathlength/100*(100-#1),
  }
}}

% I want to vertically and horizontally center this tikzpicture
\usebackgroundtemplate%
{
\begin{tikzpicture}
  \useasboundingbox (current page.north west) rectangle (current page.south east);
  \draw[myOwnStyle=\n,color=blue] (.5\paperwidth,.5\paperheight) circle [radius = 10mm] node {\n\,\%};  
\end{tikzpicture}
}

\begin{document}

\foreach \n in {0,10,...,100}{
\begin{frame}
\frametitle{Frame Number: \n}
This is the frame with the number \n.

\begin{center}
\begin{tikzpicture}
    \draw[myOwnStyle=\n] (0,0) circle [radius = 10mm] node {\n\,\%};  
\end{tikzpicture} 
\end{center}

\end{frame}
}

\end{document}

enter image description here