10

I am new to LaTeX. I'm working on some sample LaTeX examples. The sample I want to create a progress bar in my document. My sample code is shown below. Any help would be appreciated.

\documentclass{article}
\usepackage{amsmath}
\usepackage{lastpage}
\usepackage[draft,subsubsection]{progress}
\renewcommand{\ProgressDocOutput}[1]{%
\vskip-0.6cm\ProgressDrawBar{#1}\vskip 0.4cm}
\ProgressGfxXSize = 1725
\ProgressGfxYSize = 120
\begin{document}
Page1
\progress{\thepage*100/\pageref{LastPage}}  % it should rturn 50
\pagebreak
Page2
\progress{\thepage*100/\pageref{LastPage}}  % it should rturn 100
\end{document}
PSK
  • 1,032

1 Answers1

5

You can use refcount. Note that the label is LastPage and not Lastpage.

\documentclass{article}
\usepackage{lastpage,refcount}
\usepackage[draft,subsubsection]{progress}

\renewcommand{\ProgressDocOutput}[1]{%
  \vskip-0.6cm\ProgressDrawBar{#1}\vskip 0.4cm}
\ProgressGfxXSize = 1725
\ProgressGfxYSize = 120

\newcommand{\calcprogress}{%
  \progress{%
    \numexpr
      \ifnum\getpagerefnumber{LastPage}=0
        0
      \else
        \thepage*100/\getpagerefnumber{LastPage}
      \fi
    \relax
  }%
}

\begin{document}
Page1
\calcprogress  % it should return 50

\pagebreak

Page2
\calcprogress  % it should return 100

\end{document}

Explanation: \progress wants a number that's stored in a count register, so \numexpr is good. We have to deal with the case LastPage has not yet been set, when \getpagerefnumber{LastPage} returns 0; in this case we return 0, otherwise we do the computation.

egreg
  • 1,121,712