在每个章节的开始处将计数器的值写入 aux 文件中,
\usepackage{atveryend}
\makeatletter
\newcounter{problem} % 问题总个数
\protected\def\write@problem@counter #1{% 计数器写入辅助文件, #1 为第几个
\immediate\write\@auxout{\string\global\string\@namedef
{label/problem/atchap\number #1}{\number\value{problem}}}}
\AtBeginDocument{%
\providecommand\totalproblemcount{0}% 总个数:\totalproblemcount
\write@problem@counter{0}} % 第 0 个
\AfterLastShipout{\immediate\write\@auxout{%
\string\gdef\totalproblemcount{\value{problem}}}% 总个数:\totalproblemcount
\stepcounter{problem}%
\write@problem@counter{\numexpr\value{chapter}+1}} % 第 n+1 个
% 得到保存的第 #1 个 problem 的值
\def\getproblemcount #1{\ifcsname label/problem/atchap\number\numexpr #1\relax\endcsname
\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\@nameuse{label/problem/atchap\number\numexpr #1\relax}}{-1}}
% #1+1 个与 #1 个的差
\def\problemcounterdiff #1{%
\ifnum\getproblemcount{#1+1}>\z@
\the\numexpr\getproblemcount{#1+1}-\getproblemcount{#1}\relax
\else 0???\fi}
\makeatother
然后在 \chapter
开始(或结尾)时,将第 \value{chapter}
个写入 aux 文件:
\renewcommand\chapter{
...
\refstepcounter{chapter}
...
\write@problem@counter{\value{chapter}}
...
}
还需在每个问题开始时都递增 problem
计数器。
问 怎么获得特定计数器在每个章节的总量?