3

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

5

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

  • Thanks - the circles are just for debugging purpuses. I will use a complete different design later (hopefully). I really like the elegancy of the solution! I tried remember picture and overlay and friends but without success. – Dr. Manuel Kuehner Jan 12 '17 at 17:07
  • Just out of curiosity (and because I didn't figure it out myself :( ) - what would be an alternative approach? – Dr. Manuel Kuehner Jan 12 '17 at 17:11
  • @Dr.ManuelKuehner Alternative to what? – Henri Menke Jan 12 '17 at 17:31
  • Maybe I misunderstood you, you wrote [...]There are several mechanisms[...]. – Dr. Manuel Kuehner Jan 12 '17 at 17:59
  • 1
    @Dr.ManuelKuehner Ah, I see. You could use the coordinate (current page.center) or construct \path (current page.north west) -- coordinate (mid) (current page.south east) and use (mid). – Henri Menke Jan 12 '17 at 18:05
3

You can place it at the centre of the page with

\usebackgroundtemplate{%
   \begin{tikzpicture}[remember picture, overlay]
      \draw[myOwnStyle=\n,color=blue] (current page.center) circle [radius = 10mm] node {\n\,\%};  
   \end{tikzpicture} 
}

\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{center}
\begin{tikzpicture}[remember picture, overlay]
    \draw[myOwnStyle=\n,color=blue] (current page.center) 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}
  • Thanks! I used the current page.center apparently wrong in my own attempts. – Dr. Manuel Kuehner Jan 12 '17 at 18:00
  • You need to typeset twice for it to show up. – Henri Menke Jan 12 '17 at 18:09
  • @HenriMenke s/typeset/compile/? – Paul Gaborit Jan 13 '17 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.