我想在报告的每一页添加一个“进度条”。我有 25 张图片,分别命名为 pbar1.png 到 pbar25.png,如下所示,将放在幻灯片上:
pbar1.png:
pbar12.png:
pbar25.png:
因为我正好有 25 张图片,但我的报告可能有更多页(或更少),所以我使用简单的线性映射将页码映射到图像编号:
我可以使用 进行计算calc
。我的问题是确定有多少页。我可以使用lastpage
包或只是简单地在页面末尾添加一个标签并调用\pageref{lastpagenum}
,但是当我第一次在文件上运行 LaTeX 时,引用未定义,并且出现错误Arithmatic overflow
(因为它出现在分数的分母中)。我尝试使用\getrefbykeydefault{lastpagenum}{page}{1400}
from 来refcount
避免得到零,但这没有帮助。我也尝试了\ifstrequal
,但这也没有帮助。
这是 MWE。代码中提供了一些注释。
\documentclass{article}
\newcounter{imagenum}
\newcounter{dummy}
\usepackage{calc}
\usepackage{graphicx}
\usepackage{refcount}
\usepackage{fancyhdr}
\usepackage{lipsum}
\pagestyle{fancy}
\lhead{
% 1400 below can be replaced with any number number big enough
\setcounter{dummy}{\getrefbykeydefault{lastpagenum}{page}{1400}-1}
% To run without error, replace \thedummy below with 3 (which is 4-1)
\setcounter{imagenum}{(\thepage-1)*\ratio{24 cm}{\thedummy cm} +1}
%\includegraphics{../../../_Common/ProgressBar/pbar\theimagenum.png}
% includegraphics above clearly won't work. So simply just print the scaled image number
\theimagenum
}
\begin{document}
\lipsum[1]\newpage
\lipsum[1]\newpage
\lipsum[1]\newpage
\lipsum[1]
\label{lastpagenum}
\end{document}
答案1
您可以在执行计算之前简单地检查lastpage
引用(即\r@lastpagenum
)是否已定义(使用宏):\ifdefined
\makeatletter% required as some of the commands we will use contain "@" in their name
\lhead{
% only perform the calculations if the "lastpagenum" reference is defined
\ifdefined\r@lastpagenum%
% 1400 below can be replaced with any number number big enough
\setcounter{dummy}{\getrefbykeydefault{lastpagenum}{page}{1400}-1}%
\setcounter{imagenum}{(\thepage-1)*\ratio{24 cm}{\thedummy cm} +1}%
%\includegraphics{../../../_Common/ProgressBar/pbar\theimagenum.png}
\theimagenum%
\fi%
}
\makeatother
下一步是删除虚拟计数器的使用,通过获取/将最后的页面引用转换为可以在计算中使用的数字,refcount
使用\getpagerefnumber
:
\makeatletter% required as some of the commands we will use contain "@" in their name
\lhead{
% only perform the calculations if the "lastpagenum" reference is defined
\ifdefined\r@lastpagenum%
\setcounter{imagenum}{(\thepage-1)*24/(\getpagerefnumber{lastpagenum}-1) +1}%
%\includegraphics{../../../_Common/ProgressBar/pbar\theimagenum.png}
\theimagenum%
\fi%
}
\makeatother