4

I am trying to set-up a system where I can total a counter for each section and print this, then reset each section.

MWE:

\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}

Produces:

1 sec1
1/6
2/6
3/6
4/6

2 sec2
1/6
2/6
3/6
4/6
5/6
6/6

Desired result:

1 sec1
1/4
2/4
3/4
4/4

2 sec2
1/6
2/6
3/6
4/6
5/6
6/6

I have attempted to do \regtotcounter{myCounter} but no luck.

2
  • +1 for a very good MWE. It would appear that the totcount package assumes that the counter variables of interest are global rather than section-specific. Put differently, the package doesn't appear to be set up to deal with counters (e.g., myCounter) that get reset each time some other counter (e.g., section) is stepped. Asking to modify the totcount package to handle subordinated counters would amount to a feature request, and feature requests are off-topic on this site. Have you contacted the maintainer of the totcount package directly?
    – Mico
    Apr 28 at 6:14
  • Thanks, but not wishing for a feature request. Solution below is good May 2 at 23:16

1 Answer 1

6

You can't do it with totcount. Possibly with xassoccnt, but it has become unmaintained.

Here's a standalone solution, where at each section change something is written out in the .aux file (and also \AtEndDocument): for each registered counter, its final value is associated to the current section number.

Therefore we're able to know the total for each section at the next run.

\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}

enter image description here

1
  • Brilliant - great solution May 2 at 23:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .