![总页数警告和计算错误](https://linux22.com/image/336830/%E6%80%BB%E9%A1%B5%E6%95%B0%E8%AD%A6%E5%91%8A%E5%92%8C%E8%AE%A1%E7%AE%97%E9%94%99%E8%AF%AF.png)
这个问题是这个答案。
我认为文档应该显示 9 而不是 8 ?而且当前位置\AddAssociatedCounters{chapter}{appendixchapters}
(而不是序言,顺便说一下,序言会给出错误的数字)会生成警告:
警告:检测到在驱动计数器“章节”和以下驱动计数器中意外(?)添加了未使用 * DeclareAssociatedCounters 的关联计数器:* --- 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
\AddAssociatedCounters{chapter}{appendixchapters}
\chapter{Foo Appendix}
\chapter{FooBar Appendix}
\end{document}
答案1
这个page
计数器非常棘手,对于xassoccnt
(目前)也是如此。
基本上,任何其他计数器的工作原理都会导致问题page
,即计数器值通常会偏离 1。
另一个问题\NewTotalDocumentCounter
始于-1
——我将在的新版本中改变它,xassoccnt
以便可以通过一些选项键值自由调整。
目前,最好的解决方法是使用\addtocounter{xassoccnt@total@totalpages}{1}
。(xassoccnt@total@totalpages
是该计数器的内部名称)
\documentclass{book}
\usepackage{xassoccnt}
\NewTotalDocumentCounter{appendixchapters}
\NewTotalDocumentCounter{totalpages}
\DeclareAssociatedCounters{page}{totalpages}
\makeatletter
\AtBeginDocument{%
\addtocounter{xassoccnt@total@totalpages}{1}%
}
\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
\AddAssociatedCounters{chapter}{appendixchapters}
\chapter{Foo Appendix}
\chapter{FooBar Appendix}
\end{document}
也可以AddAssociatedCounters
仅用作补救措施,可以在开始时定义计数器,然后暂停它们并相应地恢复它们:
\documentclass{book}
\usepackage{xassoccnt}
\NewTotalDocumentCounter{appendixchapters}
\NewTotalDocumentCounter{totalpages}
\DeclareAssociatedCounters{page}{totalpages}
\DeclareAssociatedCounters{chapter}{appendixchapters}
\SuspendCounters{appendixchapters}
\makeatletter
\AtBeginDocument{%
\addtocounter{xassoccnt@total@totalpages}{1}
}
\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}
更新
\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
\AddAssociatedCounters{chapter}{appendixchapters}
\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}