
Durante meuúltimas experiências com referência cruzada em contagens totais entre diferentes arquivos, surge um problema de contagem de capítulos e apêndices.
Como um MWE podemos usar um MWE dadoaqui com algumas modificações.
Acontece que um comando simples como \setcounter{truechapters}{\value{totalchapters}-\value{chapter}}
não funciona.
Eu acho que o motivo está no uso errado desses valores.
A questão é como obter três contagens (total de capítulos, apêndices e capítulos verdadeiros) em um pdf de forma simples e razoável?
Observe, por favor, também que não posso usar \zlabel
como descrito por Heikoaqui
Aqui está meu 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}
Uma captura de tela do MyBook.pdf está abaixo:
Responder1
Você pode usar o LaTeX2e-kernel-macro \addtocounter
e o eTeX-primitive \numexpr
para cálculos.
\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}
Responder2
Defina três novos contadores, para os capítulos principais, para o apêndice e para o total.
O truque aqui é adicionar código para \appendix
salvar o número de capítulos até o momento truechapters
e zerar o contador do número de apêndices.
\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}
Responder3
Uma variante da solução do egreg, desta vez utilizando o pacote atualizado xassoccnt
(sendo o sucessor do assoccnt
), que possui suporte parcial para contadores totais.
\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}