我有下面的代码:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[scale=3]
\def\len{1cm}
\def\hlen{1.25cm}
\def\headradius{10pt}
\def\hwidth{2cm}
\def\hfillwidth{0.567*\hwidth}
% 计算当前页码与总页码的比例
% \pgfmathsetmacro{\ratio}{\arabic{\thepage} / \pageref{LastPage}}
%\def\hfillwidth{\ratio*\hwidth}
\coordinate (NW) at (0,0);
\fill[red!90] ([shift={(\hlen,0)}]NW) arc(-90:90:\headradius)--++(-\hwidth,0) arc(90:270:\headradius) --cycle; % 双圆角矩形
\fill[black!30] ([shift={(\hlen,0)}]NW) arc(-90:90:\headradius)--++(-\hfillwidth,0) arc(90:270:\headradius)--cycle; % 双圆角填充矩形
\end{tikzpicture}
\end{document}
我想要获得入里面注释所示的一个比值,即当前页码与总页码之比,如此用这个比值作为填充矩形长度代入,从而得到一个页码指示器。但是很遗憾,我通过多方探查,发现找不到实现的办法,因此求教大佬,希望解决下这个问题,无比感激!!
\documentclass{article}
\usepackage{tikz}
\usepackage{geometry}
\geometry{
showframe,
paperheight = 2cm,
}
\pagestyle{empty}
\parindent=0pt
\makeatletter
% #1,
\def\pageProgressBar{
\ifnum\@abspage@last=\number\maxdimen
\pgfmathsetmacro\radio{\value{page} / 100}
\else
\pgfmathsetmacro\radio{\value{page} / \@abspage@last}
\fi
\begin{tikzpicture}
\draw[rounded corners = .25cm, fill = black!30] (0, 0) rectangle ++ (\textwidth, .5cm);
\fill[rounded corners = .25cm, fill = red!90] (0, 0) rectangle ++ (\radio*\textwidth, .5cm);
\end{tikzpicture}
}
\makeatother
\begin{document}
\foreach \p in {1, ..., 20} {
\pageProgressBar
\newpage
}
\end{document}
昨天修习了一下zref-lastpage
,如果不想用圈圈的话,可以试试下面的代码:
P.S.在calc
宏包中提供了名为\ratio
的用于计算两个<dim>
之比,最好是不要使用\ratio
作为宏的名称
\documentclass[tikz,border=5pt]{standalone}
\usepackage[totpages,lastpage]{zref}
\usetikzlibrary{calc}
\def\hlen{1.25cm}
\def\headradius{10pt}
\def\hwidth{2cm}
\def\hfillwidth{.567*\hwidth}
\newcommand*\pageProgressBar{%
\ifnum\ztotpages=0\relax
\def\myratio{1}
\else%
\pgfmathsetmacro{\myratio}{\value{page}/\ztotpages}
\fi
\def\hfillwidth{\myratio*\hwidth}
\begin{tikzpicture}[scale=3]
\draw[rounded corners = \headradius, fill = black!30] (0,0) rectangle ++ (\textwidth, .5cm);
\fill[rounded corners = \headradius, fill = violet!90] (0,0) rectangle ++ (\myratio*\textwidth, .5cm);
\end{tikzpicture}
}
\begin{document}
\foreach \p in {1, ..., 20}{%
\pageProgressBar
\newpage
}
\end{document}
(上述甚至抄了一些鱼老师的代码和思路)
LastPaage
索引的数值问题可以通过refcount
提供的\getrefbykeydefault
命令获取,或者直接通过zref-lastpage
获得;这里有一个较为详细的总结~LastPage
的值的取得需要等第一轮编译到shipout
阶段之后把页码信息(应该恰好是\@abspage@last
)写入.aux
内,而使用\ztotpages
等方法在第一轮的初始值是0(使用\@abspage@last
的初始值为\maxdimen
),因为这里需要在不能获取到LastPage
的时候先编译tikz
的计算,因此对第一轮需要进行特殊判断以避免除零错误。这一过程写入.aux
是由latex自动完成的,而如果需要写入其他信息,可以参考雾月老师的这个解答,或者使用zref
的机制写入新的property。
非常感谢,是个很好的思路