9

During my last experiences with cross-referencing on total counts between different files, a problem of counting of chapters and appendices appears.

As a MWE we can use a MWE given here with some modifications.

It happens that a simple command like \setcounter{truechapters}{\value{totalchapters}-\value{chapter}} does not work.

I guess, that the reason is in a wrong using of these values.

The question is how to get three counts (total chapters, appendices and true chapters) in one pdf in a simple and reasonable way?

Note, please, also that I cannot use \zlabel like it is described by Heiko here

Here is my 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}

A screenshot of MyBook.pdf is below:

enter image description here

  • Please change for future purposes to xassoccnt. I won't update assoccnt –  Apr 15 '17 at 16:46

3 Answers3

8

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}
Ulrich Diez
  • 28,770
  • @ Ulrich Diez, thank you for the explanations! I have just updated the question and deleted all bugs in counter's abbreviations. – Vladimir Parkhomenko Nov 17 '16 at 12:56
  • 1
    @UlrichDiez, the edit you are talking about was mainly edited by Vladimir and I am not sure, what I have done except approving the edit. Sorry, if the edit of Vladimir that I approved was not in your sense. – Stefan Pinnow Nov 17 '16 at 16:16
6

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}

enter image description here

egreg
  • 1,121,712
  • @ egreg, thank you so much! This way of problem solving is very interesting too! It uses more user-friendly names of counters. I mean especially appendixchapters instead of chapters. – Vladimir Parkhomenko Nov 24 '16 at 17:10
  • This solution was the only one which didn't cause an error for a figure numbered/labeled "0.1", for me at least – L. Nielsen Nov 28 '19 at 09:19
6

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}

enter image description here