Подсчет глав и приложений

Подсчет глав и приложений

Во время моегопоследний опыт использования перекрестных ссылок на общее количество между различными файламивозникает проблема подсчета глав и приложений.

В качестве MWE мы можем использовать MWE, заданныйздесь с некоторыми изменениями.

Бывает, что простая команда типа \setcounter{truechapters}{\value{totalchapters}-\value{chapter}}не работает.

Я полагаю, что причина в неправильном использовании этих значений.

Вопрос в том, как получить три показателя (общее количество глав, приложений и истинное количество глав) в одном PDF-файле простым и разумным способом?

Обратите внимание, пожалуйста, также, что я не могу использовать \zlabelтак, как это описал Хайко.здесь

Вот мой 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}

введите описание изображения здесь

Связанный контент