1

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?

4 Answers 4

1

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.

1
  • 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. :-)
    – Philippp
    Commented Apr 14, 2015 at 10:34
3

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}

progressbar.sty exx

5
  • 2
    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 ;) Commented Mar 5, 2015 at 14:40
  • Absolutely. I think I knew that. Making the change… Thanks.
    – sgmoye
    Commented Mar 5, 2015 at 14:42
  • 1
    I read up on progress bar.sty (thanks for the pointer) and altered my answer accordingly.
    – sgmoye
    Commented Mar 5, 2015 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.)
    – Philippp
    Commented Mar 5, 2015 at 15:11
  • @sgmoye: Well done! Commented Mar 5, 2015 at 15:16
1

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}

enter image description here

1

For rating purpose, my answer based on @sgmoye's and @Philippp's answer would look like,

\documentclass{article}
\usepackage{pgfmath}
\usepackage{progressbar}

\def\totalvalp{5}
\newcommand{\pbar}[1]{%
    \pgfmathsetmacro{\ratio}{#1/\totalvalp}
    \progressbar{\ratio}

}

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

\begin{document}

\noindent\pbar{1} % for rating 1/5
\noindent\pbar{2} % for rating 2/5
\noindent\pbar{3} % for rating 3/5 
\noindent\pbar{4} % for rating 4/5 
\noindent\pbar{5} % for rating 5/5

\end{document}

Output

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .