![重置每个部分的 TotalCount (totcount)](https://linux22.com/image/464143/%E9%87%8D%E7%BD%AE%E6%AF%8F%E4%B8%AA%E9%83%A8%E5%88%86%E7%9A%84%20TotalCount%20(totcount).png)
我正在尝试建立一个系统,其中我可以为每个部分计算总数并打印,然后重置每个部分。
梅威瑟:
\documentclass{article}
\usepackage{xparse}
\usepackage{ifthen}
\usepackage{totcount}
\newcounter{myCounter}[section]
\regtotcounter{myCounter}
\begin{document}
\section{sec1}
\label{sec:sec1}
\stepcounter{myCounter}\themyCounter/\total{myCounter}\\
\stepcounter{myCounter}\themyCounter/\total{myCounter}\\
\stepcounter{myCounter}\themyCounter/\total{myCounter}\\
\stepcounter{myCounter}\themyCounter/\total{myCounter}\\
\section{sec2}
\label{sec:sec2}
\stepcounter{myCounter}\themyCounter/\total{myCounter}\\
\stepcounter{myCounter}\themyCounter/\total{myCounter}\\
\stepcounter{myCounter}\themyCounter/\total{myCounter}\\
\stepcounter{myCounter}\themyCounter/\total{myCounter}\\
\stepcounter{myCounter}\themyCounter/\total{myCounter}\\
\stepcounter{myCounter}\themyCounter/\total{myCounter}\\
\end{document}
生成:
1 sec1
1/6
2/6
3/6
4/6
2 sec2
1/6
2/6
3/6
4/6
5/6
6/6
期望结果:
1 sec1
1/4
2/4
3/4
4/4
2 sec2
1/6
2/6
3/6
4/6
5/6
6/6
我曾尝试过,\regtotcounter{myCounter}
但没有成功。
答案1
您无法使用 来做到这一点totcount
。也许可以使用xassoccnt
,但它已无人维护。
这是一个独立的解决方案,其中每个部分的变化都会在文件中写出一些内容.aux
(并且\AtEndDocument
):对于每个注册的计数器,其最终值与当前部分编号相关联。
因此我们能够在下次运行时知道每个部分的总数。
\documentclass{article}
\AddToHook{cmd/section/before}{\savesectioncounters}
\AtEndDocument{\savesectioncounters}
\ExplSyntaxOn
\clist_new:N \g__panda_counters_clist
\prop_new:N \g__panda_counters_prop
\AtBeginDocument{\prop_show:N \g__panda_counters_prop}
\NewDocumentCommand{\declaresectioncounter}{m}
{
\clist_gput_right:Nn \g__panda_counters_clist { #1 }
}
\NewDocumentCommand{\savesectioncounters}{}
{
\clist_map_inline:Nn \g__panda_counters_clist
{
\iow_shipout:cx { @mainaux } { \sectioncountertotal{##1}{\use:c{the##1}}{\arabic{section}} }
}
}
\NewDocumentCommand{\sectioncountertotal}{mmm}
{
\prop_gput:Nnn \g__panda_counters_prop { #1@#3 } { #2 }
}
\NewExpandableDocumentCommand{\getsectiontotal}{m}
{
\prop_item:Ne \g__panda_counters_prop { #1@\arabic{section} }
}
\cs_generate_variant:Nn \prop_item:Nn { Ne }
\ExplSyntaxOff
\newcounter{myCounter}[section]
\declaresectioncounter{myCounter}
\begin{document}
\section{sec1}
\label{sec:sec1}
\stepcounter{myCounter}\themyCounter/\getsectiontotal{myCounter}\\
\stepcounter{myCounter}\themyCounter/\getsectiontotal{myCounter}\\
\stepcounter{myCounter}\themyCounter/\getsectiontotal{myCounter}\\
\stepcounter{myCounter}\themyCounter/\getsectiontotal{myCounter}
\section{sec2}
\label{sec:sec2}
\stepcounter{myCounter}\themyCounter/\getsectiontotal{myCounter}\\
\stepcounter{myCounter}\themyCounter/\getsectiontotal{myCounter}\\
\stepcounter{myCounter}\themyCounter/\getsectiontotal{myCounter}\\
\stepcounter{myCounter}\themyCounter/\getsectiontotal{myCounter}\\
\stepcounter{myCounter}\themyCounter/\getsectiontotal{myCounter}\\
\stepcounter{myCounter}\themyCounter/\getsectiontotal{myCounter}
\end{document}