2

I'd like to display/get the maximum value of a counter that it reaches per section.

I have found a way to get the total/maximum value of a counter in the entire document so far, but I fail to get it working per section.

Is this even possible, perhaps with another package or would I need a custom command?

Picture of problem

screenshot of problem

MWE

\documentclass[
paper=a4,
DIV=15,
parskip=half
]{scrartcl}

\usepackage[T1]{fontenc} \usepackage[utf8]{inputenc} \usepackage{ lmodern, totcount, comment, }

%\setcounter{secnumdepth}{0}

\newcounter{counterPERsection}[section] \regtotcounter{counterPERsection} \newcounter{anothercounter} \regtotcounter{anothercounter}

\begin{comment} \newcommand{\callingcounters}{% \stepcounter{counterPERsection}\arabic{counterPERsection}\ \stepcounter{anothercounter}\arabic{anothercounter}.\total{anothercounter} } \end{comment}

%%%%%%%%%%%%%%%%%%%%%% %%% NOT %%%%%%% %%% WORKING %%%%%%% %%%%%%%%%%%%%%%%%%%%%% %\begin{comment} \newcommand{\callingcounters}{% \stepcounter{counterPERsection}\arabic{counterPERsection}.\total{counterPERsection}\ \stepcounter{anothercounter}\arabic{anothercounter}.\total{anothercounter} } %\end{comment}

\begin{document}

\section{First section}

\callingcounters

\callingcounters

\callingcounters

\callingcounters

\section{Second section}

\callingcounters

\callingcounters

\end{document}

henry
  • 6,594

2 Answers2

1

You can use the xcntperchap package that was written with this purpose.

\documentclass{article}
\usepackage{xcntperchap}

\newcounter{dummy} % for “total” counters

\newcounter{counterPERsection}[section]
\RegisterTrackCounter{section}{counterPERsection}

\newcounter{anothercounter}
\RegisterTrackCounter{dummy}{anothercounter}

% initialize the total counters
\stepcounter{dummy}

\newcommand{\callingcounters}{%
  \stepcounter{counterPERsection}%
  \arabic{counterPERsection}.%
  \ObtainTrackedValueExp{section}{counterPERsection}%
  \\*
  \stepcounter{anothercounter}\arabic{anothercounter}.%
  \ObtainTrackedValueExp{dummy}{anothercounter}%
}

\setlength{\parindent}{0pt} % just for this example

\begin{document}

\section{First section}

\callingcounters

\callingcounters

\callingcounters

\callingcounters

\section{Second section}

\callingcounters

\callingcounters

\end{document}

enter image description here

egreg
  • 1,121,712
  • Well, thank you. :) But I have a problem. While your MWE works/compiles fine on my machine, the final version in my document does not. First it threw an error because of calc being loaded, but with Mico's trick from here that went away. Finally, the value for both instances of \ObtainTrackedValueExp{.. are a 0/zero. Would you happen to have any clue as to what I could do to zero in on the cause for that? – henry Jun 06 '20 at 11:32
  • I discovered a .cpc-file in the folder, which has this content: https://pastebin.com/FwbFLb2w (your "dummy" being "totcounter" here) If I set the zeros to different values and save the file, the document displays these values after the next latex run. Then with the next run, they go back to 0. – henry Jun 06 '20 at 11:46
  • @henry Sorry, I can't debug what I can't see. I added \usepackage{calc} before xcntperchap and have no problem. It's useless to edit the .cpc file, because it gets rewritten at each run. – egreg Jun 06 '20 at 11:53
  • This is so frustrating. I have been to hell and back for this. Can I send you a version of my document? – henry Jun 06 '20 at 14:15
  • @henry You should rather ask a new question with an example that fails, with details on the TeX distribution you're running. – egreg Jun 06 '20 at 14:33
  • I know. On it. :) – henry Jun 06 '20 at 14:35
  • I found the issue lying with amsmath, amssymb and siunitx. If I comment out amsmath and amssymb (both with zero options) and later on situnitx (loaded with a few options), it works. However I am unable to reproduce it so I am actually quite fed up with this and done for today. :( If I somehow happen to to make it work, I will post again about this. – henry Jun 06 '20 at 14:53
  • @henry I added amsmath, amssymb and siunitx. No issue. – egreg Jun 06 '20 at 15:12
0

You can do it using a version of \label and \ref, but you have to manually save the counter values where you want them saved. \AtEndDocument won't cut it.

You can use \currentlabel to save almost anything.

\documentclass[
paper=a4,
DIV=15,
parskip=half
]{scrartcl}

%\usepackage[T1]{fontenc}% easier to remove than use
%\usepackage[utf8]{inputenc}
%\usepackage{lmodern, totcount, comment,}

%\setcounter{secnumdepth}{0}

\newcounter{counterPERsection}[section]
\newcounter{anothercounter}

\makeatletter
\newcommand{\currentlabel}[2]{% #1 = text, #2 = label name
  \edef\@currentlabel{#1}%
  \label{#2}%
}
\makeatother

\newcommand{\callingcounters}[2]{% #1 = total counterPERsection, #2 = total anothercounter
    \stepcounter{counterPERsection}\arabic{counterPERsection}.#1\\
    \stepcounter{anothercounter}\arabic{anothercounter}.#2
}
%\end{comment}



\begin{document}

\section{First section}

\callingcounters{\ref{CPS1}}{\ref{AC}}

\callingcounters{\ref{CPS1}}{\ref{AC}}

\callingcounters{\ref{CPS1}}{\ref{AC}}

\callingcounters{\ref{CPS1}}{\ref{AC}}

\currentlabel{\thecounterPERsection}{CPS1}
\section{Second section}

\callingcounters{\ref{CPS2}}{\ref{AC}}

\callingcounters{\ref{CPS2}}{\ref{AC}}

\currentlabel{\thecounterPERsection}{CPS2}
\currentlabel{\theanothercounter}{AC}
\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120