TransWikia.com

General hooks to step a counter on chapter and section regardless of whether they are numbered or unnumbered

TeX - LaTeX Asked by gusbrs on July 31, 2021

I’m trying to step a pair of counters at each chapter and section of a document regardless of whether they are numbered or not. I don’t intend to use the absolute value of these counters, I’d like to use them to find if a zlabel is in the previous, or next section, or chapter.

My attempt so far, which works for the standard classes, is to use the new hook system, and add a hook to chapter and section (MWE of this below).

However, I’d like this to work for other document classes too. The more the better, but at least the most common ones, such as Koma-script, memoir, etc. Koma I’m a little more acquainted with, and happen to know that it uses chapter and section internally, even for addchap and addsec, so that using those would work there too. But I don’t know if that is standard. So my question is: are chapter and section the right places to hook for this purpose? Or is there a more general place I could (or should) hook into to get more robust results?

The MWE for the book class is the following:

documentclass{book}

ExplSyntaxOn
int_new:N g__zrefcheck_abschap_int
int_new:N g__zrefcheck_abssec_int

AddToHook { cmd / chapter / before }
  {
    int_gincr:N g__zrefcheck_abschap_int
    int_zero:N g__zrefcheck_abssec_int
  }

AddToHook { cmd / section / before }
  { int_gincr:N g__zrefcheck_abssec_int }

NewDocumentCommand myshowchapint {} { int_use:N g__zrefcheck_abschap_int }
NewDocumentCommand myshowsecint {} { int_use:N g__zrefcheck_abssec_int }
ExplSyntaxOff

begin{document}

chapter{Chapter 1}
myshowchapint{} par
myshowsecint{}

section{Section 1.1}
myshowchapint{} par
myshowsecint{}

section{Section 1.2}
myshowchapint{} par
myshowsecint{}

section{Section 1.3}
myshowchapint{} par
myshowsecint{}

chapter*{Chapter X}
myshowchapint{} par
myshowsecint{}

section*{Section X.1}
myshowchapint{} par
myshowsecint{}

section*{Section X.2}
myshowchapint{} par
myshowsecint{}

section*{Section X.3}
myshowchapint{} par
myshowsecint{}

end{document}

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP