
Während meinerletzte Erfahrungen mit Querverweisen auf Gesamtzahlen zwischen verschiedenen Dateientritt ein Problem beim Zählen der Kapitel und Anhänge auf.
Als MWE können wir ein MWE verwenden, das gegeben istHier mit einigen Modifikationen.
Es kommt vor, dass ein einfacher Befehl wie \setcounter{truechapters}{\value{totalchapters}-\value{chapter}}
nicht funktioniert.
Ich vermute, dass der Grund in einer falschen Verwendung dieser Werte liegt.
Die Frage ist, wie man auf einfache und sinnvolle Weise drei Zählungen (Gesamtzahl der Kapitel, Anhänge und eigentliche Kapitel) in einer PDF-Datei unterbringen kann.
Bitte beachten Sie auch, dass ich es nicht \zlabel
so verwenden kann, wie es von Heiko beschrieben wirdHier
Hier ist mein 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}
Unten sehen Sie einen Screenshot von MyBook.pdf:
Antwort1
Sie können für Berechnungen sowohl das LaTeX2e-Kernel-Makro \addtocounter
als auch das eTeX-Primitiv verwenden.\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}
Antwort2
Definieren Sie drei neue Zähler, für die Hauptkapitel, für den Anhang und für das Gesamtergebnis.
Der Trick besteht hier darin, Code hinzuzufügen, der \appendix
die bisherige Anzahl der Kapitel speichert truechapters
und den Zähler für die Anzahl der Anhänge zurücksetzt.
\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}
Antwort3
Eine Variante der Lösung von egreg, diesmal unter Verwendung des aktuellen xassoccnt
Pakets (Nachfolger von assoccnt
), das teilweise Unterstützung für Gesamtzähler bietet.
\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}