![如何在自定义进度条中放置代表进度百分比的数字?](https://linux22.com/image/392244/%E5%A6%82%E4%BD%95%E5%9C%A8%E8%87%AA%E5%AE%9A%E4%B9%89%E8%BF%9B%E5%BA%A6%E6%9D%A1%E4%B8%AD%E6%94%BE%E7%BD%AE%E4%BB%A3%E8%A1%A8%E8%BF%9B%E5%BA%A6%E7%99%BE%E5%88%86%E6%AF%94%E7%9A%84%E6%95%B0%E5%AD%97%EF%BC%9F.png)
在进度条中,我想放置一个百分比数字来表示进度量。我获得的进度条原始代码来自latex-beamer 的进度条。下图显示了我想要得到的东西,但这里的 85% 是手动放置的。
在尝试自己解决这个问题并查看了之前的问题后,我了解到用于计算进度百分比的命令是
\newcommand{\progressframepercent}{
{\textnormal{\pgfmathparse{\insertframenumber*100/\inserttotalframenumber}%
\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}\,\%}}
}
但是,百分比指示器仍是垂直居中的。要解决这个问题,框必须具有与进度条相同的高度,并且内容垂直居中。
\documentclass[aspectratio=169, xcolor={x11names}]{beamer}
\usecolortheme{rose}
\setbeamercolor{itemize item}{fg=black}
\useoutertheme{miniframes}
\useinnertheme{inmargin}
\setbeamersize{text margin left=2mm, text margin right=2mm}
\newlength{\sidebarWidth}
\setlength{\sidebarWidth}{0.2\paperwidth}
\setbeamersize{sidebar width left=\sidebarWidth, sidebar width right=0cm}
\usefonttheme{structurebold}
\usepackage{tikz}
\usetikzlibrary{calc}
% Custom progress bar
% BEGIN_FOLD
\setbeamercolor{progress bar progress}{use=progress bar,bg=progress bar.fg}
\newlength{\heightNavigationSymbol}
\setlength{\heightNavigationSymbol}{2.5mm} % around 2.5mm or 7.1pt
\newlength{\widthProgressBarFull}
\setlength{\widthProgressBarFull}{\sidebarWidth}
\newcommand{\totalslideinframe}{0}
\defbeamertemplate{footline}{progress bar}{
% Calculate bars widths
\dimen0=\widthProgressBarFull
\multiply\dimen0 by \insertframenumber
\divide\dimen0 by \inserttotalframenumber
\edef\widthProgressBar{\the\dimen0}
\leavevmode%
%
% The bar itself
\begin{beamercolorbox}[wd=\widthProgressBarFull, ht=\heightNavigationSymbol, dp=1ex]{progress bar}
\begin{beamercolorbox}[wd=\widthProgressBar, ht=\heightNavigationSymbol, dp=1ex]{progress bar progress}
\end{beamercolorbox}%
\end{beamercolorbox}%
{\hspace{-\widthProgressBarFull}\color{white} \adjustbox{minipage={\sidebarWidth}, frame}{\hspace*{\fill} $\progressframepercent$ \hspace*{\fill}}}%
}
\setbeamertemplate{footline}[progress bar]
\setbeamercolor{progress bar}{fg=DodgerBlue3,bg=PeachPuff3}
% END_FOLD
\usepackage{adjustbox}
\newcommand{\progressframepercent}{
{\textnormal{\pgfmathparse{\insertframenumber*100/\inserttotalframenumber}%
\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}\,\%}}
}
\begin{document}
\begin{frame}
Some contents
\end{frame}
\begin{frame}
Some contents
\end{frame}
\begin{frame}
Some contents
\end{frame}
\begin{frame}
Some contents
\end{frame}
\begin{frame}
Some contents
\end{frame}
\end{document}
答案1
要使百分比指示器垂直居中,您可以将文本放在 中\raisebox
。下面使用的 值0.02cm
只是一个快速猜测,如有必要,您可以进一步微调它。
\documentclass[aspectratio=169, xcolor={x11names}]{beamer}
\usecolortheme{rose}
\setbeamercolor{itemize item}{fg=black}
\useoutertheme{miniframes}
\useinnertheme{inmargin}
\setbeamersize{text margin left=2mm, text margin right=2mm}
\newlength{\sidebarWidth}
\setlength{\sidebarWidth}{0.2\paperwidth}
\setbeamersize{sidebar width left=\sidebarWidth, sidebar width right=0cm}
\usefonttheme{structurebold}
\usepackage{tikz}
\usetikzlibrary{calc}
% Custom progress bar
% BEGIN_FOLD
\setbeamercolor{progress bar progress}{use=progress bar,bg=progress bar.fg}
\newlength{\heightNavigationSymbol}
\setlength{\heightNavigationSymbol}{2.5mm} % around 2.5mm or 7.1pt
\newlength{\widthProgressBarFull}
\setlength{\widthProgressBarFull}{\sidebarWidth}
\newcommand{\totalslideinframe}{0}
\defbeamertemplate{footline}{progress bar}{
% Calculate bars widths
\dimen0=\widthProgressBarFull
\multiply\dimen0 by \insertframenumber
\divide\dimen0 by \inserttotalframenumber
\edef\widthProgressBar{\the\dimen0}
\leavevmode%
%
% The bar itself
\begin{beamercolorbox}[wd=\widthProgressBarFull, ht=\heightNavigationSymbol, dp=1ex]{progress bar}
\begin{beamercolorbox}[wd=\widthProgressBar, ht=\heightNavigationSymbol, dp=1ex]{progress bar progress}
\end{beamercolorbox}%
\end{beamercolorbox}%
\raisebox{0.02cm}{\hspace{-\widthProgressBarFull}\color{white} \adjustbox{minipage={\sidebarWidth}, frame}{\hspace*{\fill} $\progressframepercent$ \hspace*{\fill}}}%
}
\setbeamertemplate{footline}[progress bar]
\setbeamercolor{progress bar}{fg=DodgerBlue3,bg=PeachPuff3}
% END_FOLD
\usepackage{adjustbox}
\newcommand{\progressframepercent}{
{\textnormal{\pgfmathparse{\insertframenumber*100/\inserttotalframenumber}%
\pgfmathprintnumber[fixed,precision=2]{\pgfmathresult}\,\%}}
}
\begin{document}
\begin{frame}
Some contents
\end{frame}
\begin{frame}
Some contents
\end{frame}
\begin{frame}
Some contents
\end{frame}
\begin{frame}
Some contents
\end{frame}
\begin{frame}
Some contents
\end{frame}
\end{document}