Beamer:将 tikzpicture 置于背景中心(\usebackgroundtemplate)

Beamer:将 tikzpicture 置于背景中心(\usebackgroundtemplate)

我真的以为这不会太难,但我很挣扎定心(垂直和水平)tikzpicture\usebackgroundtemplate

我使用的代码取自我的一个先前的问题- 最后,我希望在投影仪演示的背景中有一个进度指示器。

当我使用一个简单的\begin{center}...\end{center}(仅水平)然后我收到一个错误说

!LaTeX 错误:出现错误 — — 可能缺少 \item。

\begin{center}...\end{center}在正常框架内有效(参见 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}

在此处输入图片描述

答案1

使用调整tikzpicture纸张大小\useasboundingbox,并将圆圈置于中心。有几种机制,我使用了坐标(.5\paperwidth,.5\paperheight)。您也可以将其放置在对角线的一半或其他位置。

注意:您确实希望每帧上有两个这样的圆圈吗?

\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}

在此处输入图片描述

答案2

您可以将其放置在页面的中心

\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}

相关内容