![计算章节和附录](https://linux22.com/image/344396/%E8%AE%A1%E7%AE%97%E7%AB%A0%E8%8A%82%E5%92%8C%E9%99%84%E5%BD%95.png)
在我的上次在不同文件之间交叉引用总数的经验,出现章节和附录计数的问题。
作为 MWE,我们可以使用给定的 MWE这里 做了一些修改。
有时像这样的简单命令 \setcounter{truechapters}{\value{totalchapters}-\value{chapter}}
不起作用。
我猜测,原因在于错误地使用这些价值观。
问题是如何简单合理的方式在一个pdf中获取三个计数(总章节,附录和真实章节)?
请注意,我不能\zlabel
像 Heiko 描述的那样使用这里
这是我的 MWE:
MyBook.tex
:
\documentclass{memoir}
\usepackage{totcount}
\usepackage{assoccnt}
\newtotcounter{totalchapters}
\DeclareAssociatedCounters{chapter}{totalchapters} % Associate the driven counter `totalchapters` to the master counter `chapter`
\AtBeginDocument{
%% register a counter on the basis of the last chapter in totcounter
\regtotcounter{chapter}
}
\newcounter{truechapters}
\begin{document}
\chapter{A}
\chapter{B}
\appendix
\chapter{C}
\chapter{D}
\chapter{E}
\chapter{F}
\chapter{G}
\chapter{H}
\chapter{I}
There are \total{totalchapters} total chapters.
There are \total{chapter} appendix chapters.
%here we need to count the difference of two previous counters
%\setcounter{truechapters}{\value{totalchapters}-\value{chapter}}
There are \thetruechapters \: true chapters.
\end{document}
MyBook.pdf 的截图如下:
答案1
您可以同时使用 LaTeX2e-kernel-macro\addtocounter
和 eTeX-primitive\numexpr
进行计算。
\documentclass{memoir}
\usepackage{totcount}
\usepackage{assoccnt}
\newtotcounter{totalchapters}
\DeclareAssociatedCounters{chapter}{totalchapters} % Associate the driven counter `totalchapters` to the master counter `chapter`
\AtBeginDocument{
%% register a counter on the basis of the last chapter in totcounter
\regtotcounter{chapter}
}
\newcounter{truechapters}
\begin{document}
\chapter{A}
\chapter{B}
\appendix
\chapter{C}
\chapter{D}
\chapter{E}
\chapter{F}
\chapter{G}
\chapter{H}
\chapter{I}
There are \total{totalchapters} total chapters.
There are \total{chapter} appendix chapters.
%here we need to count the difference of two previous counters
\setcounter{truechapters}{\value{totalchapters}}%
\addtocounter{truechapters}{-\value{chapter}}%
There are \thetruechapters \: true chapters.
\setcounter{truechapters}{\numexpr\value{totalchapters}-\value{chapter}\relax}%
There are \thetruechapters \: true chapters.
\end{document}
答案2
定义三个新的计数器,分别用于主要章节、附录和总数。
这里的技巧是添加代码来\appendix
保存迄今为止的章节数truechapters
并重置附录数量的计数器。
\documentclass{memoir}
\usepackage{etoolbox}
\usepackage{totcount,assoccnt}
\newcounter{truechapters}
\regtotcounter{truechapters}
\newcounter{totalchapters}
\newcounter{appendixchapters}
\DeclareAssociatedCounters{chapter}{totalchapters,appendixchapters}
\regtotcounter{totalchapters}
\regtotcounter{appendixchapters}
\preto\appendix{%
% save the number of true chapters
\setcounter{truechapters}{\value{chapter}}%
% reset the number of chapters
\setcounter{appendixchapters}{0}%
}
\begin{document}
\frontmatter
There are \total{totalchapters} total chapters.
There are \total{appendixchapters} appendix chapters.
There are \total{truechapters} true chapters.
\mainmatter
\chapter{A}
\chapter{B}
\appendix
\chapter{C}
\chapter{D}
\chapter{E}
\chapter{F}
\chapter{G}
\chapter{H}
\chapter{I}
\end{document}
答案3
egreg 解决方案的一个变体,这次使用最新的xassoccnt
包(的后继者assoccnt
),它对总计数器有部分支持。
\documentclass{memoir}
\usepackage{xassoccnt}% Version 1.2 or higher, current version is 1.7
\NewTotalDocumentCounter{appendixchapters}
\DeclareTotalAssociatedCounters{chapter}{truechapters,totalchapters}
\makeatletter
\g@addto@macro\appendix{%
\SuspendCounters{truechapters}
\AddAssociatedCounters{chapter}{appendixchapters}
}
\makeatother
\begin{document}
\frontmatter
There are \TotalValue{totalchapters} total chapters.
There are \TotalValue{appendixchapters} appendix chapters.
There are \TotalValue{truechapters} true chapters.
\mainmatter
\chapter{A}
\chapter{B}
\appendix
\chapter{C}
\chapter{D}
\chapter{E}
\chapter{F}
\chapter{G}
\chapter{H}
\chapter{I}
\end{document}