1

I am trying to access the final value of a counter in each section at the beginning of the section. The reason for knowing the value ahead of time is to use it in a bar plot that summarizes information about the section. Realistically, I need to do this with multiple counters, so solutions that can eloquently accomplish that would be preferred! So far, I have tried two different approaches suggested on Tex exchange without success.

Approach 1: totcount seems like a nice out-of-the-box solution, but I can't get the counter to auto reset each section. My understanding is that \newtotcounter{mycount}[section] should accomplish that. I also tried defining a new command \mysection that would manually \setcounter{mycount}{0}, but had the same issue

\documentclass[a4paper,12pt]{book}
\usepackage{totcount}

\begin{document}
\newtotcounter{mycount}[section]
\chapter{The First Chapter}
    \section{Part 1}
        There are \total{mycount} counts in this section
        \stepcounter{mycount}
        \stepcounter{mycount}
    \section{Part 2}
        There are \total{mycount} counts in this section
\end{document}

totcount rendering

Approach 2: More complicated, but according to the answer, this solves the same problem. Being newer to Tex, I have trouble understanding the internals of how \newsection accomplishes the counter logic. Perhaps my refactoring of the original answer from resetting each chapter to section was incorrect?

\documentclass[a4paper,12pt]{book}
\usepackage{refcount}

\newcounter{mycounter}[section]
\newcommand{\newsection}[1]{%
    \refstepcounter{mycounter}\label{mycounterref@\thesection}%
    \section{#1}
    (The value of \texttt{mycounter} at the end of this section will be \number\numexpr\getrefnumber{mycounterref@\number\value{section}}-1\relax)\par}
\AtEndDocument{\refstepcounter{mycounter}\label{mycounterref@\thesection}}

\begin{document}
\chapter{The First Chapter}
    \newsection{Part 1} 
        \stepcounter{mycounter}
        \stepcounter{mycounter} 
    \newsection{Part 2}
\end{document}

enter image description here