# Total pages number warning and miscalculated

The question is a follow-up to this answer.

I think that the document is suppsed to display 9 instead of 8 ? also the current position of \AddAssociatedCounters{chapter}{appendixchapters} (instead of the preamble whic will give wrong number by the way) generate a warning:

Warning: An accidental(?) addition of associated counter(s) without * DeclareAssociatedCounters was detected for * the driver counter "chapter" and following driven counters: * --- appendixchapters ---

\documentclass{book}
\usepackage{xassoccnt}
\NewTotalDocumentCounter{appendixchapters}
\NewTotalDocumentCounter{totalpages}
\DeclareAssociatedCounters{page}{totalpages}

\begin{document}
There are \TotalValue{appendixchapters} appendix chapters in this document

There are \TotalValue{totalpages} pages in this document
\chapter{First}
\chapter{Second}
\appendix

\chapter{Foo Appendix}
\chapter{FooBar Appendix}
\end{document}

• It is a warning, nothing more or less. It's better to use \DeclareAssociatedCounters right from the start for all relevant counters! The page counter is an unreliable friend, actually. (And I am the author of xassoccnt) – user31729 Jun 30 '16 at 20:13
• @ChristianHupfer but in this case we cant right ? otherwise appendixchapters final value would be 4 instead of 2. talking about the wrong total number of pages what did i do wrong ? – Cfun Jun 30 '16 at 20:14

The page counter is very tricky, also for xassoccnt (yet).

What is working for basically any other counter causes troubles with page, i.e. the counter value is off by one usually.

Another issue is \NewTotalDocumentCounter starts at -1 -- I will change this in a new version of xassoccnt in order to make it freely adjustable with some option key value.

For the moment, the best fix is to use \addtocounter{xassoccnt@total@totalpages}{1}. (xassoccnt@total@totalpages is the internal name of this counter)

\documentclass{book}
\usepackage{xassoccnt}
\NewTotalDocumentCounter{appendixchapters}
\NewTotalDocumentCounter{totalpages}

\DeclareAssociatedCounters{page}{totalpages}
\makeatletter
\AtBeginDocument{%
}
\makeatother

\begin{document}
There are \TotalValue{appendixchapters} appendix chapters in this document

There are \TotalValue{totalpages} pages in this document
\chapter{First}
\chapter{Second}
\appendix

\chapter{Foo Appendix}
\chapter{FooBar Appendix}
\end{document}


Also use AddAssociatedCounters also as a remedy only, it's possible to define the counters in the beginning already, suspend them and resume them accordingly:

\documentclass{book}
\usepackage{xassoccnt}
\NewTotalDocumentCounter{appendixchapters}
\NewTotalDocumentCounter{totalpages}

\DeclareAssociatedCounters{page}{totalpages}
\DeclareAssociatedCounters{chapter}{appendixchapters}

\SuspendCounters{appendixchapters}

\makeatletter
\AtBeginDocument{%
}
\makeatother

\begin{document}
There are \TotalValue{appendixchapters} appendix chapters in this document

There are \TotalValue{totalpages} pages in this document
\chapter{First}
\chapter{Second}
\appendix

\ResumeSuspendedCounters{appendixchapters}

\chapter{Foo Appendix}
\chapter{FooBar Appendix}
\end{document}


Update

\documentclass{book}
%compile twice

\usepackage{xassoccnt}
\NewTotalDocumentCounter{totalfigures}
\NewTotalDocumentCounter{totaltables}
\NewTotalDocumentCounter{appendixchapters}
\DeclareAssociatedCounters{figure}{totalfigures}
\DeclareAssociatedCounters{table}{totaltables}
\NewDocumentCounter{realpages}
\NewTotalDocumentCounter{totalpages}
\usepackage{everyshi}

\makeatletter
\AtBeginDocument{%
\stepcounter{realpages}%
}
\EveryShipout{%
\stepcounter{realpages}%
}
\AtEndDocument{%
\setcounter{xassoccnt@total@totalpages}{\value{realpages}}%
\write\@auxout{%
\string\setcounter{xassoccnt@total@totalpages}{\number\value{realpages}}%
}
}
\makeatother

\usepackage{blindtext}

\begin{document}

There are \TotalValue{totalfigures} figures in this document

There are \TotalValue{totaltables} tables in this document

There are \TotalValue{appendixchapters} appendix chapters in this document

There are \TotalValue{totalpages} pages in this document
\chapter{First}

\begin{figure}
\caption{Figure foo \thefigure}
\end{figure}

\begin{table}
\caption{Table foo \thetable}
\end{table}

\begin{table}
\caption{Table foo \thetable}
\end{table}

\begin{table}
\caption{Table foo \thetable}
\end{table}

\chapter{Second}

\begin{figure}
\caption{Figure foo \thefigure}
\end{figure}

\begin{figure}
\caption{Figure foo \thefigure}
\end{figure}

\begin{table}
\caption{Table foo \thetable}
\end{table}

\begin{table}
\caption{Table foo \thetable}
\end{table}

\begin{table}
\caption{Table foo \thetable}
\end{table}

\appendix

\chapter{Foo Appendix}
\chapter{FooBar Appendix}

\begin{table}
\caption{Table foo \thetable}
\end{table}

\begin{table}
\caption{Table foo \thetable}
\end{table}

\begin{table}
\caption{Table foo \thetable}
\end{table}
\end{document}

• Thanks, and nice to know the package will be updated/improved in the future :) – Cfun Jun 30 '16 at 20:25
• @SAM: Let's say updated ;-) – user31729 Jun 30 '16 at 20:27
• it seems the solution only fix the issue in that specific case, please look at pastebin.com/VETc1eiW instead of 10 we got 9, the value added to the counter must be 2...we are in a loop? – Cfun Jun 30 '16 at 20:31
• @SAM: As I said: page is a tricky counter. – user31729 Jun 30 '16 at 20:34
• @SAM: This is nothing that can be solved without severe hacking or very manipulative methods – user31729 Jun 30 '16 at 20:46