
私の異なるファイル間の合計カウントの相互参照に関する最新の経験、章と付録の数え方の問題が発生します。
MWEとして、与えられたMWEを使用することができますここ 若干の変更を加えました。
のような単純なコマンドが \setcounter{truechapters}{\value{totalchapters}-\value{chapter}}
機能しないことがあります。
理由は、これらの値の誤った使用にあると思われます。
問題は、3 つのカウント (総章数、付録、実際の章数) を 1 つの PDF に簡単かつ合理的な方法で取得する方法です。
\zlabel
また、 Heikoが説明したように使用することはできませんのでご了承ください。ここ
これが私の 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 カーネル マクロ\addtocounter
と eTeX プリミティブの両方を使用できます。\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
メインの章、付録、合計の 3 つの新しいカウンターを定義します。
ここでの秘訣は、\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}