
Durante miúltimas experiencias con cruces de recuentos totales entre diferentes expedientes, aparece un problema de recuento de capítulos y apéndices.
Como MWE podemos usar un MWE dadoaquí con algunas modificaciones.
Sucede que un comando simple como \setcounter{truechapters}{\value{totalchapters}-\value{chapter}}
no funciona.
Supongo que la razón está en un uso incorrecto de estos valores.
La pregunta es ¿cómo obtener tres recuentos (capítulos totales, apéndices y capítulos verdaderos) en un pdf de una manera simple y razonable?
Tenga en cuenta, por favor, también que no puedo usar\zlabel
como lo describe Heiko.aquí
Aquí está mi 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 continuación se muestra una captura de pantalla de MyBook.pdf:
Respuesta1
Puede utilizar tanto la macro del núcleo LaTeX2e \addtocounter
como la primitiva eTeX \numexpr
para los 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}
Respuesta2
Definir tres nuevos contadores, para los capítulos principales, para el apéndice y para el total.
El truco aquí consiste en agregar código para \appendix
guardar la cantidad de capítulos hasta el momento truechapters
y restablecer el contador de la cantidad 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}
Respuesta3
Una variante de la solución de egreg, esta vez usando el paquete actualizado xassoccnt
(siendo el sucesor de assoccnt
), que tiene soporte parcial para contadores totales.
\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}