# Is there any way to hook onto the advancement of a counter?

Question title really says it all - can you hook onto

\stepcounter{mycounter}


which probably boils down to (I may very well be wrong)

\advance \mycounter by 1


Is this a hookable thing?

### Edit:

I'm not sure if this is helpful, but answers in LaTeX3 are okay, too :)

• @Werner (who had just commented \setcounter -> \stepcounter ;) Yes in fact - that would be more useful. (In the end, I'm trying to hook onto page breaks, but atbegshi and everypage aren't doing the trick. So, by looking into hooking counters, I can hook onto the page counter (or whatever, more extensible) and do it that way.) – Sean Allred Jul 11 '13 at 16:35
• I'm not entirely sure whether this question stems from a What does “XY Problem” mean? type of situation. Perhaps it would be useful to actually focus on the problem at hand, rather than fixing a possible work-around. – Werner Jul 11 '13 at 16:40
• Note that unless you are planning to use the code of your hook inside the output routine it will execute at strange places, typically half way down the following page, as TeX will have expanded macros and typeset boxes ahead of the material that finally lands on any given page. – David Carlisle Jul 11 '13 at 16:42
• @DavidCarlisle For my purposes, as long as it updates its counter only when certain sectioning elements have been set, I'm golden. I'm just updating a l3seq stack, I'm not necessarily outputting anything. It may not work, but I had the idea. Werner - specific to me, this is a good example of the XY problem, but that's why I made it more general -- perhaps the functionality is also useful elsewhere. – Sean Allred Jul 11 '13 at 16:47
• Yes but if you look at the l3seq stack from a macro expanding in the main page rather than the output routine it will have an essentially arbitrary value, perhaps reflecting the setting from several pages back as one or more pages might by typeset before the page counter is incremented. (which is why you need \pageref to refer to a page number the value of \thepage (and the value of your stack) are not useful within the document. – David Carlisle Jul 11 '13 at 16:57

In this specific case, yes, but you need to identify the base component of this advancement. The macros involved include \refstepcounter, \stepcounter and \addtocounter. The following are their definitions, taken from latex.ltx:

\def\refstepcounter#1{\stepcounter{#1}%
\protected@edef\@currentlabel
{\csname p@#1\endcsname\csname the#1\endcsname}%
}

\def\stepcounter#1{%
\begingroup
\let\@elt\@stpelt
\csname cl@#1\endcsname
\endgroup}


\refstepcounter uses \stepcounter for advancement, and \stepcounter uses \addtocounter for advancement. So, tapping into \addtocounter is the best. You can update it to hook into arguments #1 (the counter) or #2 (the amount/number).
If you're attempting to hook into page counters, then a \label-\pageref system should be considered instead, due to the asynchronous nature of the output routine. For this, the use of refcount which provides \setcounterpageref and \getpagerefnumber, both of which might be of help.