1

I would like to write page numbers like this '7/9' in all minted environments of my document. The first number ('7') is the current page and the second number ('9') is the maximum number of page breaks the source file has been split into.

I was planning to use the package totcount but I realized that it was only considering the last value of the counter at the end of the document and it couldn't manage counters locally to an environment.

Here is a totally stupid example, but it illustrate the problem:

\documentclass{report}

\usepackage{totcount}
\newtotcounter{sectnum}

\let\oldsection\section
\renewcommand{\section}{%
  \setcounter{sectnum}{0}%
  \regtotcounter{sectnum}%
  \oldsection%
}

\let\oldsubsection\subsection
\renewcommand{\subsection}{%
  \stepcounter{sectnum}%
  \oldsubsection%
}

\begin{document}

\section*{One}

\subsection*{SectionOne}
\thesectnum / \total{sectnum}

\subsection*{SectionTwo}
\thesectnum / \total{sectnum}

\subsection*{SectionThree}
\thesectnum / \total{sectnum}

\subsection*{SectionFour}
\thesectnum / \total{sectnum}


\section*{Two}

\subsection*{SectionOne}
\thesectnum / \total{sectnum}

\subsection*{SectionTwo}
\thesectnum / \total{sectnum}

\end{document}

Gives:

Failed attempt to define local counters

Where we would have liked to have: '1/4', '2/4', '3/4', '4/4' and '1/2', '2/2'.

So, is there a way to get this kind of feature ?

1 Answer 1

2

totcount, like anything that uses information that is only available later in the document, uses auxiliary files to get the final value of counters. That is, at the end of the document, the values of all registered counters are written to an auxiliary file and read in at the beginning of the following TeX run.

One consequence of this is that you cannot save more than one value for the same counter, just the final one. To solve your problem, you have to either write the current value of the counter to the .aux file yourself at the end of every section and read those in at the beginning of the document (instead of using totcount) or you have to use a different counter for every section, like this:

\documentclass{report}

\usepackage{etoolbox}
\usepackage{totcount}

\makeatletter
  \newcounter{every@section}
  \pretocmd\section{%
    \stepcounter{every@section}%
    \begingroup
      \edef\@tmpa{\endgroup\noexpand\newtotcounter{my@sectnum-\arabic{every@section}}}%
    \@tmpa
  }{}{}
  \pretocmd\subsection{%
    \stepcounter{my@sectnum-\arabic{every@section}}%
  }{}{}
  \def\thesectnum{\arabic{my@sectnum-\arabic{every@section}}}
  \def\totsectnum{\total{my@sectnum-\arabic{every@section}}}
\makeatother

\begin{document}

\section*{One}

\subsection*{SectionOne}
\thesectnum / \totsectnum

\subsection*{SectionTwo}
\thesectnum / \totsectnum

\subsection*{SectionThree}
\thesectnum / \totsectnum

\subsection*{SectionFour}
\thesectnum / \totsectnum

\section*{Two}

\subsection*{SectionOne}
\thesectnum / \totsectnum

\subsection*{SectionTwo}
\thesectnum / \totsectnum

\end{document}

Some notes:

  • I used etoolbox' \pretocmd to patch the sectioning commands instead of copying and replacing them.
  • I use the counter every@section to count all sections, even the unnumbered ones, in order to create separate counters for all of them.
1
  • Thanks a lot! I would never have find this out by myself!
    – perror
    Jul 23, 2019 at 7:09

You must log in to answer this question.

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