# Displaying a later value of a counter

I have a counter which is used throughout my document. At the beginning of each chapter (right after the \chapter command), I want to print the value of the counter which is reached immediately before the next \chapter command. For e.g. At the first paragraph occurring within chapter 1, I want to display the last value the counter reached within chapter 1. At the first paragraph occurring within chapter 2, I want to display the last value the counter reached within chapter 2.

I have put together a partial solution, but I do not know if it is ideal:

I have the counter thecounter, which is used for the entire length of my document. I use a custom macro for \chapter called \newchapter. This macro resets the counter each time back to "0" whenever I start a new chapter:

\newmacro{\newchapter}[1]{
\setcounter{thecounter}{0}
\chapter{#1}
}


I can further add to this macro by creating a variable which knows the value of the counter immediately before the reset:

\newmacro{\newchapter}[1]{
\def\valuebeforethechapterends{\value{thecounter}}
\setcounter{thecounter}{0}
\chapter{#1}
%the last value of the counter before it gets reset for the next chapter should be printed here
}


Within the first paragraph of each chapter, I'd like to display the value that the counter will reach before the next reset. Unfortunately, I do not know how to tell the code which \valuebeforethechapterends to use, as it will be defined many times in the document.

How can I print the last value of the counter before it gets reset right after the \chapter command in my macro?

-

The solution by egreg is useful because it calls \refstepcounter, which might be needed for things other than printing a counter's state/number. But here is a solution that needs no refcount package and no \refstepcounter, thereby being cheaper. It requires a command, but not a label, per each chapter. I have used Bruno Le Floch's example text from here.

\documentclass{book}
\makeatletter
\newcounter{mycounter}
\newcommand{\newchapter}[1]{%
\chapter{#1}%
\ifnum\thechapter<\tw@\else
\immediate\write\@auxout{%
\global\noexpand\@namedef
{mycounter\the\numexpr\thechapter-1}{\number\c@mycounter}%
}%
\fi
\setcounter{mycounter}{0}%
\@ifundefined{mycounter\thechapter}{}{%
\textcolor{red}{The value of \texttt{mycounter} at
the end of this chapter is \@nameuse{mycounter\thechapter}.}\medskip
}%
}
\AtEndDocument{%
\immediate\write\@auxout{%
\global\noexpand\@namedef{mycounter\thechapter}{\number\c@mycounter}%
}%
}
% All the following is just an example text.
\newlength{\sos@top}
\newlength{\sos@right}
\newcounter{sos@pages}
\newif\ifSOS
\renewcommand{\thepage}{\the\numexpr\value{page}-\value{sos@pages}\relax}
\newcommand{\sos@reset@figures}{\gdef\sos@figures{\sos@reset@figures\vfill}}
\sos@reset@figures

\newcommand{\sos@shipout@figures}{%
\begingroup
\stepcounter{page}%
\stepcounter{sos@pages}%
\let\protect\relax
\setbox\z@\vbox to\vsize{\sos@figures}%
\let\protect\string
\AtBeginShipoutOriginalShipout\vbox
{\vbox to\sos@top{}\moveright\sos@right\box\z@}%
\endgroup
}
\AtBeginShipout{%
\ifSOS
\ifodd\c@page
\begingroup
\let\protect\string
\AtBeginShipoutOriginalShipout\box\AtBeginShipoutBox
\sos@shipout@figures
\endgroup
\fi
\fi
}
\newcommand{\SOSshipout}{\clearpage\sos@shipout@figures}
\renewcommand{\SOStrue}{\clearpage\global\let\ifSOS\iftrue}
\renewcommand{\SOSfalse}{\clearpage\global\let\ifSOS\iffalse}
\setlength{\sos@top}{2cm}
\setlength{\sos@right}{2cm}
\newcommand*\lipsumnopar[2][1]{{\def\lips@par{ }\lipsum[#1-#2]}}
\makeatother

\usepackage{booktabs,caption}
\usepackage{atbegshi,xcolor}
\usepackage{lipsum}
\usepackage{hyperref}
\begin{document}

\title{Hello world}
\author{Bruno Le Floch}
\maketitle

\tableofcontents
\listoffigures
\listoftables

\part{Abc}

\SOStrue
\newchapter{Hello}

\noindent{\bf\texttt{mycounter} is called 3 times in this chapter.}\medskip

\toprule
Abc def & ghijk lmno pq \\
\midrule
\lipsum[1] & \lipsum[2] \\
\bottomrule
\end{tabular}
\captionof{table}{\label{tab:atable}A table}
}

\rule{8cm}{3cm}%
\captionof{figure}{A figure}
}

\lipsumnopar{10}

\newchapter{Bye}

\noindent{\bf\texttt{mycounter} is called 2 times in this chapter.}\medskip

\lipsumnopar{10}

\SOSfalse

\newchapter{Back to normal}

\noindent{\bf\texttt{mycounter} is called 3 times in this chapter.}\medskip

\lipsumnopar[11]{15}

\lipsumnopar[16]{20}

\lipsumnopar[21]{30}

See Table~\ref{tab:atable}.

\SOSshipout
\SOStrue

\newchapter{Figures, again}

\noindent{\bf\texttt{mycounter} is called 1 time in this chapter.}\medskip

\lipsumnopar[21]{25}

\lipsumnopar[26]{30}

\end{document}


Edit:

Here is my attempt to generalize the solution. More testing is required.

\makeatletter
\newcommand\ifstrsame[2]{%
\@nameuse{@\ifnum\pdfstrcmp{\detokenize{#1}}%
{\detokenize{#2}}=0first\else second\fi oftwo}%
}
\def\appchap#1{\ifstrsame{chapter}{#1}{}{\thechapter}}
\ifstrsame{section}{#1}{%
\expandafter\getheadingnr\romannumeral-\q\@nameuse{the#1}..\relax
}{%
\number\@nameuse{the#1}%
}%
}
\def\newchapter@hook{}
\def\DeclareCallsCounter#1#2{%
\newcounter{#2}%
\expandafter\newcommand\csname new#1\endcsname[1]{%
\ifstrsame{section}{#1}{%
\gdef\newchapter@hook{%
\immediate\write\@auxout{%
\global\noexpand\@namedef
}%
}%
}{%
\newchapter@hook
}%
\@nameuse{#1}{##1}%
\immediate\write\@auxout{%
\global\noexpand\@namedef
}%
\fi
\setcounter{#2}{0}%
\textcolor{red}{The value of \texttt{#2} at
the end of this #1 is \@nameuse{\appchap{#1}#2@\headingnr{#1}}.}\medskip
}%
}%
\AtEndDocument{%
\immediate\write\@auxout{%
\global\noexpand\@namedef
}%
}%
}
\DeclareCallsCounter{chapter}{chapcounter}
\DeclareCallsCounter{section}{seccounter}
\makeatother

-

You need to use the \label-\ref system.

First of all you don't need to reset manually your counter, it suffices to bind it to chapter:

\newcounter{mycounter}[chapter]


Next I'd use the refcount package and issue a \label just before doing \chapter together with \refstepcounter{mycounter} (this will give an "off by one" which is easily corrected):

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


Of course, the first compilation won't give the actual number as usual for the \label-\ref` system.

-