[Tex/LaTex] Displaying a later value of a counter

counters

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?

Best Answer

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{\addfig}[1]{\g@addto@macro\sos@figures{\vbox{\centering#1}\vfill}}
\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
      \AtBeginShipoutDiscard
      \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

\addtocounter{mycounter}{1}

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

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

\lipsumnopar{10}

\addfig{\rule{1cm}{3cm}\captionof{figure}{Another figure}}
\addfig{\rule{8cm}{3cm}\captionof{figure}{A figure}}
\addfig{\rule{1cm}{3cm}\captionof{figure}{Another figure}}

\addtocounter{mycounter}{1}
\addtocounter{mycounter}{1}

\newchapter{Bye}

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

\addtocounter{mycounter}{1}

\addfig{\rule{8cm}{3cm}\captionof{figure}{That should be figure 5.}}
\addfig{\rule{1cm}{3cm}\captionof{figure}{Perhaps the sixth}}

\lipsumnopar{10}

\addfig{\rule{8cm}{3cm}\captionof{figure}{Yet another one}}
\addfig{\rule{1cm}{3cm}\captionof{figure}{One last figure for now.}}

\SOSfalse

\addtocounter{mycounter}{1}

\newchapter{Back to normal}

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

\addtocounter{mycounter}{1}
\addtocounter{mycounter}{1}

\addfig{\rule{8cm}{3cm}\captionof{figure}{That figure won't be lost.}}

\lipsumnopar[11]{15}

\addfig{\rule{4cm}{5cm}\captionof{figure}{Nor will that one.}}

\lipsumnopar[16]{20}

\lipsumnopar[21]{30}

See Table~\ref{tab:atable}.

\SOSshipout
\SOStrue

\addtocounter{mycounter}{1}

\newchapter{Figures, again}

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

\addtocounter{mycounter}{1}

\addfig{\rule{5cm}{2cm}\captionof{table}{Let's pretend it's a table}}

\lipsumnopar[21]{25}

\addfig{\rule{5cm}{2cm}\captionof{table}{Let's pretend it's a table}}

\lipsumnopar[26]{30}

\addfig{\rule{4cm}{1cm}\captionof{table}{Last table}}

\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}}
\def\headingnr#1{%
  \ifstrsame{section}{#1}{%
    \expandafter\getheadingnr\romannumeral-`\q\@nameuse{the#1}..\relax
  }{%
    \number\@nameuse{the#1}%
  }%
}
\def\getheadingnr#1.#2.#3\relax{#2}
\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
            {\appchap{#1}#2@\headingnr{#1}}{\number\@nameuse{c@#2}}%
        }%
      }%
    }{%
      \newchapter@hook
    }%
    \@nameuse{#1}{##1}%
    \ifnum\headingnr{#1}<\tw@\else
      \immediate\write\@auxout{%
        \global\noexpand\@namedef
          {\appchap{#1}#2@\the\numexpr\headingnr{#1}-1}{\number\@nameuse{c@#2}}%
      }%
    \fi
    \setcounter{#2}{0}%
    \@ifundefined{\appchap{#1}#2@\headingnr{#1}}{}{%
      \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
        {\appchap{#1}#2@\headingnr{#1}}{\number\@nameuse{c@#2}}%
    }%
  }%
}
\DeclareCallsCounter{chapter}{chapcounter}
\DeclareCallsCounter{section}{seccounter}
\makeatother
Related Question