# LaTeX problem: \addtocounter doesn't work with babel option

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}
\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}
\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!

• Welcome, you are doing something completely wrong. What exactly are you trying to achieve? – Johannes_B Jan 7 '18 at 9:53
• I have to make a document with two part, each part starting with page number one. – Garulfo Jan 7 '18 at 10:03
• Package lastpage? \setcounter{page}{1}? I have no clue what you are trying to do. – Johannes_B Jan 7 '18 at 10:06
• Christian Hupfer will be here soon and post an answer. – Johannes_B Jan 7 '18 at 10:07
• @ChristianHupfer I know you for a while now ;-) – Johannes_B Jan 7 '18 at 10:24

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}

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}


• Thanks a lot for your answer! If I understand right, \makeatletter \pretocmd{\part}{\page-numbering{arabic}\clearpage}{}{} sets the page counter as a number. Should there not be a \makeatother afterwards? Anyway, I tried the xassoccnt package, which looks out to be the best solution, since zref could leads to confusions with lastpage package, which I also use. I copied and pasted your code, but I unfortunately get the error message Undefined control sequence. \DeclareTotalAssociatedCounters. Did I make something wrong? – Garulfo Jan 7 '18 at 16:43
• @Garulfo: I forgot the \makeatother, sorry. \DeclareTotalAssociatedCounters is an addition in v1.2. (current version is 1.6), from 2017/03/03. If this does not work, \DeclareTotalDocumentCounter{totalpages}\DeclareAssociatedCounters{page}{totalpages} works for older versions. In my point of view, zref and lastpage do not interfere – user31729 Jan 7 '18 at 16:47
• I tried, it seems to works, but then other problems appears. I'll try tomorrow to analyze that and post then an new message. – Garulfo Jan 7 '18 at 17:08
• So I tried again, but when I only add \usepackage{xassoccnt}, without any other commands, a new error appears with labels: Undefined control sequence. \label{LastPagePart01}. Does it mean that there is a conflict between xassoccnt and labels? – Garulfo Jan 8 '18 at 16:58
• No, there shold be no conflict as long as you load xassoccnt very late – user31729 Jan 8 '18 at 17:21