Counting chapters and appendices
You can use both the LaTeX2e-kernel-macro \addtocounter
and the
eTeX-primitive \numexpr
for calculations.
\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}
Define three new counters, for the main chapters, for the appendix and for the total.
The trick here is to add code to \appendix
for saving the number of chapters so far in truechapters
and resetting the counter for the number of appendices.
\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}
A variant of egreg's solution, this time using the up-to-date xassoccnt
package (being the successor of assoccnt
), which has a partial support for total counters.
\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}