[Tex/LaTex] LaTeX problem: \addtocounter doesn’t work with babel option

babelcounters

I am new on this forum, so I hope to write my question in the right way.

I have a document in two parts (which is a model that I need many times), each part starting with page number 1. I must then write the total number of pages of the whole document. For this purpose, I want to use the command \addtocounter. The following code works:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\begin{document}
\newcounter{TotPages}
\addtocounter{TotPages}{\pageref{02}}
\addtocounter{TotPages}{\pageref{03}}
\pageref{02}+\pageref{03}=
\arabic{TotPages}
\section{title 01}
\newpage
\section{title 02}
\label{02}
\newpage
\section{title 03}
\label{03}
\end{document}

But with either [french], [francais] or [british] option to babel package, it writes the error message

Missing number, treated as zero. \addtocounter{TotPages} \pageref{02}}.

For example, the following code doesn't work:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[francais]{babel} %only change
\begin{document}
\newcounter{TotPages}
\addtocounter{TotPages}{\pageref{02}}
\addtocounter{TotPages}{\pageref{03}}
\pageref{02}+\pageref{03}=
\arabic{TotPages}
\section{title 01}
\newpage
\section{title 02}
\label{02}
\newpage
\section{title 03}
\label{03}
\end{document}

I know that I could use the package calc, but I don't want if possible, since I need only this command in the whole document.

Does anyone know how to fix this problem?
Thanks a lot!

Best Answer

Short answer why \addtocounter fails with \pageref:

\pageref is not expandable, it can't be 'fed' to \addtocounter -- this macro expects a number value.

Apart from this issue, the addition of reference values to page is not giving what might be expected, since a \pageref appearing on page 5 and another one appearing on page 17 does not mean that the document has only 22 pages then; even if this would be the case, never rely on the output of a ref value, it could be a roman number or a letter as well.

Possible solutions:

  • With the lastpage package it is possible to get the last page reference, but this does not work, if the page number is reset in the meantime → a wrong value is reported.
  • With the xassoccnt package and its \DeclareTotalAssociatedCounters{page}{totalpages} the number of pages can be output with \TotalValue{totalpages} -- this is not influenced by a resetting of the page counter.
  • Using \usepackage[perpage,user,lastpage]{zref} it is possible to get the total number of pages with \zref[abspage]{LastPage}. (Please do not confuse the LastPage label from zref with the LastPage label from the lastpage package -- they belong to different namespaces)

Here's a 'tiny' document, showing the problems, the pageref can be extracted expandable with \getpagerefnumber from refcount, for example -- it is only added in order to show that the counting with \pageref is logically wrong.

The document has 6 pages, only \TotalValue or \zref[abspage]{LastPage} report the correct value.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{refcount}
\usepackage{lastpage}
\usepackage{blindtext}
\usepackage[francais]{babel} %only change


\usepackage[perpage,user,lastpage]{zref}
\usepackage{xassoccnt}

\DeclareTotalAssociatedCounters{page}{totalpages}

\makeatletter
\pretocmd{\part}{\pagenumbering{arabic}\clearpage}{}{}
\makeatother
\newcounter{TotPages}

\begin{document}

\addtocounter{TotPages}{\getpagerefnumber{02}}
\addtocounter{TotPages}{\getpagerefnumber{03}}


The number of pages in this beautiful document: \TotalValue{totalpages} pages! 

whereas \verb!\pageref{Lastpage}! reports \pageref{LastPage} pages!. 

With \texttt{zref} you get \zref[abspage]{LastPage} pages!


Values reported with sum up of references: \pageref{02}+\pageref{03}= \arabic{TotPages} pages!

\part{First} 

\section{title 01}
\clearpage
\section{title 02}
\label{02}
\clearpage

\blindtext

\part{Second} 
\section{title 03} \label{03}
\blindtext[5]

\end{document}

enter image description here

Related Question