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.Edit:
Here is my attempt to generalize the solution. More testing is required.