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:
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.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.\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 fromzref
with theLastPage
label from thelastpage
package -- they belong to different namespaces)Here's a 'tiny' document, showing the problems, the pageref can be extracted expandable with
\getpagerefnumber
fromrefcount
, 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.