# Show page number as a “percent complete”

I want to show in the page foot the current progress of the document. For example, if the document contains 200 pages, then I want the foot to show 11.5% in the page 23.

Edit: I'm also interested the foot to show percent complete of current chapter. Is it possible?

Tank you for help

-
Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Christian Hupfer Jul 8 '14 at 21:44

The package zref-totpages can deliver the total number of pages and with expl3 we have a powerful number crunching system.

\documentclass[12pt,paper=a4]{scrbook}
\usepackage{scrpage2}
\usepackage{blindtext}
\usepackage{zref-totpages}
\usepackage{xparse}
\usepackage{xcolor}

\ExplSyntaxOn
\NewDocumentCommand{\percentage}{}
{
\int_compare:nTF { \ztotpages > 0 }
{
\fp_to_decimal:n { round( 100 * \value{page} / \ztotpages , 2 ) }
}
{ -- }
}
\ExplSyntaxOff

\cfoot{\LARGE\textcolor{blue}{\percentage\% -- Page \thepage\ of \ztotpages}}
\ifoot{}
\ofoot{}

\begin{document}
\blinddocument
\blinddocument
\blinddocument

\end{document}


-
I knew of lastpage until today. After the comment of Werner, I searched a bit, and it seems that lastpage sets the label with \AtEndDocument, so it “solves” nothing. The manual says that the one that tries harder to get to the very very end is pagelts. Opinion? What are the possible problems? –  Manuel Jul 8 '14 at 23:01
@Manuel It may have problems in tough situations. Never encountered one. A possibly safer package is zref; I'll update this tomorrow. –  egreg Jul 8 '14 at 23:02
Thanks for the help. I'm also interested the foot to show percent complete of current section, also percent complete of total document. Is it possible? –  Albert Jul 9 '14 at 23:36
@Albert That's rather vaguely defined. –  egreg Jul 9 '14 at 23:41
\documentclass{scrartcl}
\usepackage{xparse}
\usepackage{scrlayer-scrpage}

\cfoot{\percentpagemark}

\AtEndDocument{\label{last-page}}

\ExplSyntaxOn
\NewDocumentCommand \percentpagemark { }
{
\int_set:NV \l_tmpa_int { \value{page} }
\int_set:NV \l_tmpb_int { \pageref{last-page} }
\fp_eval:n { round( 100*\l_tmpa_int/\l_tmpb_int , 2 ) } \, \%
}
\cs_generate_variant:Nn \int_set:Nn { NV }
\ExplSyntaxOff

\usepackage{blindtext} % For this example

\begin{document}
\Blinddocument
\end{document}


As noted by @Werner, this \AtEndDocument solution may not be perfect in some cases (something with the last shipout or may be some contents that are added after the reference included in my code). Of course, there are already packages for that; a better result may be achieved using the references made by pagelts (which uses atveryend commands).

-
Better to use atveryend. Also, this creates an error at first compile (since the reference is non-existent). –  Werner Jul 8 '14 at 22:16
@Werner I added a little comment about it. However, since I've never used them and I don't know how shipouts and family works, I'm not going to add that solution to my answer. You can add another one :) –  Manuel Jul 8 '14 at 22:54
As soon as TeX reaches \end{document} it issues whatever has been accumulated in the \AtEndDocument hook. As such, if you have pending floats that need to be flushed, your \label{last-page} may be too early. Try adding (say) 10 \begin{figure}[p] \centering\rule{.5\linewidth}{.5\textheight} \end{figure} before the end of the document just to get an idea of what's happening. –  Werner Jul 8 '14 at 23:11
Thanks for the help. I'm also interested the foot to show percent complete of current section, also percent complete of total document. Is it possible? –  Albert Jul 9 '14 at 23:36
@Albert What is percent complete of current section? (please detail a little bit in the question.) And, percent complete of total document… that's what it already does, or do you mean something else? –  Manuel Jul 10 '14 at 8:10

Another approach, with page counters and usage of calculator package. The percentage is calculated via \CalculatePercentage[]{}{}, where the first optional argument contains the number of digits for round off, the 2nd argument is the designed for the current page number and the 3rd argument holds the total number of pages.

I used a different approach, deviating from totcount package.

\documentclass[12pt,paper=a4]{scrbook}
\usepackage{scrpage2}%
\usepackage{calculator}%
\usepackage{etoolbox}%
\usepackage{forloop}
\usepackage{blindtext}
\usepackage{everypage}%
\usepackage{xcolor}%

\newcommand{\CalculatePercentage}[3][1]{%
\def\tempone{1.0}%
\def\temptwo{1.0}%
\DIVIDE{#2}{#3}{\temptwo}%
\MULTIPLY{\temptwo}{100}{\tempone}
\ROUND[#1]{\tempone}{\temptwo}%
\temptwo%
}%

\newcounter{lastpagecounter}%
\newcounter{totalpagecounter}%

\makeatletter

\refstepcounter{totalpagecounter}%
}%

\AtEndDocument{%
\immediate\write\@auxout{%   Store the total number of pages to aux -file
\string\setcounter{lastpagecounter}{\number\value{totalpagecounter}}%
}%
}%

\cfoot{\LARGE \textcolor{blue}{\CalculatePercentage{\number\value{page}}{\number\value{lastpagecounter}}\% -- Page \thepage~of \number\value{lastpagecounter}}}%
\ifoot{}%
\ofoot{}%

\newcounter{loopcounter}%

\newcounter{maxpages}
\setcounter{maxpages}{201}

\begin{document}
\forloop{loopcounter}{1}{\number\value{loopcounter} < \number\value{maxpages}}{%
\blindtext
\newpage
}%
\end{document}


-
Why not using lastpage and refcount for being able to use the value as a number? –  egreg Jul 8 '14 at 22:33
@egreg: I know of them, but sometimes I want to do some things the hard way ;-) I will post another version late on, with lastpage and refcount –  Christian Hupfer Jul 8 '14 at 22:37
Thanks for the help. I'm also interested the foot to show percent complete of current section, also percent complete of total document. Is it possible? –  Albert Jul 9 '14 at 23:37
@Albert: Why did you not write that in your OP before? How do you define % of section or of total document. As portions of pages belonging to the current section? If so, this would be difficult, since sections do not start on new pages in general. –  Christian Hupfer Jul 9 '14 at 23:40
Then i can do it with chapters in book mode instead of sections? –  Albert Jul 10 '14 at 0:17

Because the question doesn't include the word LaTeX, I can show the non-LaTeX solution. The first task (percent of total page) is simple with opmac's \lastpage:

\input opmac

\openref
\footline={\rm\thefontsize[10]\hfil\the\pageno\ / \dopercent\pageno of\lastpage\hfil}

\def\dopercent #1of#2{\ifnum#2=0 ??\else
\tmpdim=#1 pt
\multiply\tmpdim by10 \divide\tmpdim by#2\multiply\tmpdim by10
\edef\tmp{\expandafter \ignorept \the\tmpdim;}%
\expandafter\onlyoneten \tmp
\fi ~\%%
}
\def\onlyoneten #1.#2#3;{#1.#2}

... document

\bye


The second task with percents of pages for chapters is a bit more complicated. I save the list of totalpages for each chapter to the REF file. This is done by \chaphook which is executed at the beginning of the chapter: the \totchappage from previous chapter is saved. The \chappagelist includes in second TeX run:

pre_chapter_pages,first_chapter_pages,second_chapter_pages,etc.


The \readttotchappage reads from this list first item and removes this item from the list. First, we need to run \readtotchappage alone in order to remove pre_chapter_pages. Then the \readtotchappage is used in \chaphook.

\def\chappagelist{}
\input opmac

... the same code as above ...

\newcount\chappage \newcount\totchappage
\def\chaphook{\vfil\break
\savechappage
\footline={\rm\thefontsize[10]\hfil\the\pageno\ / \dopercent\pageno of\lastpage\hfil
chapter: \dopercent\chappage of\totchappage \hfil}
}
}
\def\savechappage{%
}

This is non-LaTeX solution, as I annonced. Use tex or pdftex. Or you can only get an inspiration from this. –  wipet Jul 10 '14 at 13:52
The command \chapter isn't defined in plainTeX. OPmac defines \chap command but with another syntax. My solution is intended for OPmac users, no for LaTeX users, sorry. The TeX macro engine (latex/plain) wasn't specified in the question. If you are using LaTeX then you can only be inspired by my code but you cannot use it directly. –  wipet Jul 10 '14 at 15:30