# How to calculate a percentage and use result in progressbar?

With the progressbar package, I want to show bars on pages depending on a counter.

Progressbar works like:

\progressbar{0.7}


I use the calc package anyway, so I thought it should be easy to calculate the ratio for the bar for each page, like this:

\newcounter{y}
\setcounter{y}{5 / 1}  % check that calc is working

\newcounter{total}
\setcounter{total}{30}


But, none of the below seem to work:

\progressbar{\value{y}/\value{total}}\\
\progressbar{\value{y} / \value{total}}\\
\progressbar{\they{}/\thetotal{}}\\


I get the error: Illegal unit of measure (pt inserted).

What am I doing wrong?

Package progessbar uses packages calc's \real which accepts only a decimal number, not a ratio. Package calc provides \ratio but unfortunately it seems \real{\ratio{\lenA}{\lenB}} is not legal syntax.

Hence, in despair, you may use:

\makeatletter
\expandafter\progressbar\expandafter {\strip@pt \dimexpr \value{y}pt/\value{total}\relax}
\makeatother


This should be wrapped in a macro, naturally.

• Special thanks for this answer - I finally switched to this solution since it turned out that pgfmath conflicted with some other package on Linux and Windows (on OS X it had worked). Its obviously better to avoid using additional packages. :-) Apr 14 '15 at 10:34

Counters (and the math that used to manipulate them) use only integers, so you could never end up with \progressbar{.7}.

progressbar.sty uses TikZ, so you can do something like this:

\documentclass{article}

\usepackage{progressbar}

\def\total{30}
\newlength{\basicwidth}
\setlength{\basicwidth}{5in}

\newcommand{\pbar}[1]{%
\pgfmathsetmacro{\barpc}{#1/\total}%
}

\begin{document}

\noindent\pbar{15}%
\progressbar{\barpc}\\
\pbar{20}%
\progressbar{\barpc}\\
\pbar{10}%
\progressbar{\barpc}\\
\pbar{5}%
\progressbar{\barpc}\\

\end{document}


• Loading the beast TikZ only for a simple operation is not convenient. Replacing that call with \usepackage{pgfmath} makes the code still working, just more efficiently as you don't make use of TikZ elsewhere. BTW: I think the OP is looking for some solutions to package progressbar where the argument is not a counter ;) Mar 5 '15 at 14:40
• Absolutely. I think I knew that. Making the change… Thanks. Mar 5 '15 at 14:42
• I read up on progress bar.sty (thanks for the pointer) and altered my answer accordingly. Mar 5 '15 at 15:04
• Thanks a lot!! The pgfmathsetmacro was the missing link! So here is my MWE, now bysed on your help. (Cannot past it here, will put it in answer.) Mar 5 '15 at 15:11
• @sgmoye: Well done! Mar 5 '15 at 15:16

My solution based on the first answer looks like:

\documentclass{article}

\usepackage{pgfmath}
\usepackage{progressbar}
\usepackage{totcount}

\newcommand{\tutprogress}{%
\stepcounter{tutpageCounter}
\pgfmathsetmacro{\ratio}{\value{tutpageCounter}/\totvalue{tutpageCounter}}
\progressbar{\ratio}

}

\begin{document}

\progressbarchange {ticksheight=1,borderwidth=0.8pt,tickswidth=1.0pt,filledcolor=gray!50,emptycolor=gray!10}
\progressbarchange {subdivisions=\totvalue{tutpageCounter}}
\newtotcounter{tutpageCounter}

\tutprogress{}
\tutprogress{}
\tutprogress{}
\tutprogress{}
\tutprogress{}
\tutprogress{}
\tutprogress{}
\tutprogress{}

\end{document}