30 如何获取当前页码与总页码之比并令其参与tikz计算参数?

发布于 2025-03-21 11:03:40

我有下面的代码:

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

我想要获得入里面注释所示的一个比值,即当前页码与总页码之比,如此用这个比值作为填充矩形长度代入,从而得到一个页码指示器。但是很遗憾,我通过多方探查,发现找不到实现的办法,因此求教大佬,希望解决下这个问题,无比感激!!

查看更多

关注者
1
被浏览
760
鱼香肉丝没有鱼先生
脾气不好,别来惹我!!!
\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}

image.png

2 个回答
Sagittarius Rover
Sagittarius Rover 2025-03-22
我要成为Typst糕手/(ㄒoㄒ)/~~

昨天修习了一下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。

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览