Displaying a later value of a counter

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.


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

Tags:

Counters