# Resetting glossaries' abbreviations each chapter

I'm using glossaries-extra to generate a list of abbreviations, and I'd like to reset the abbreviations every chapter so that:

• If there is a single occurrence of the entry in the chapter, use only the "long" form (and don't add it to the list).
• If there are more than one occurrence of the entry in the chapter:
• use the "long (short)" form on the first occurrence of the chapter
• use the "short" form on subsequent occurrence of the chapter
• add the entry to the list

To achieve the distinction between "first" occurrence and subsequent ones per chapter, we can:

\AddToHook{cmd/chapter/before}{\glsresetall}


To achieve the distinction between "single" use and more uses, we can enable reference counting with:

\GlsXtrEnableEntryUnitCounting{abbreviation}{1}{chapter}


However, while using each of those individually, works for part of the problem, using both together no longer works as intended. Consider the following document:

\documentclass{book}

\usepackage{hyperref}

\usepackage[
abbreviations,
shortcuts=abbr,
]{glossaries-extra}

\makeglossaries

\GlsXtrEnableEntryUnitCounting{abbreviation}{1}{chapter}
% This is what I'd like to do instead of manually resetting after each chapter

\newabbreviation{ABC}{ABC}{Aaaa Bbbb Cccc}
\newabbreviation{DEF}{DEF}{Dddd Eeee Ffff}
\newabbreviation{GHI}{GHI}{Gggg Hhhh Iiii}

\begin{document}

\chapter{Chapter 1}

\ab{ABC}, \ab{ABC}, \ab{ABC}

\ab{DEF}

\ab{GHI}

\chapter{Chapter 2}
% \glsresetall % resetting after \chapter works

\ab{ABC}

\ab{DEF}, \ab{DEF}, \ab{DEF}

\ab{GHI}

\chapter{Chapter 3}
% \glsresetall % ditto

\ab{ABC}

\ab{DEF}

\ab{GHI}, \ab{GHI}, \ab{GHI}

\printabbreviations

\end{document}


This results in all abbreviations being written in "long" form, and only GHI ending up in the list of abbreviations. (Compiled with pdflatex -> pdflatex -> makeglossaries -> pdflatex -> pdflatex -> makeglossaries -> pdflatex -> pdflatex).

But manually resetting after each chapter gives us intended results, as in:

\documentclass{book}

\usepackage{hyperref}

\usepackage[
abbreviations,
shortcuts=abbr,
]{glossaries-extra}

\makeglossaries

\GlsXtrEnableEntryUnitCounting{abbreviation}{1}{chapter}
% This is what I'd like to do instead of manually resetting after each chapter

\newabbreviation{ABC}{ABC}{Aaaa Bbbb Cccc}
\newabbreviation{DEF}{DEF}{Dddd Eeee Ffff}
\newabbreviation{GHI}{GHI}{Gggg Hhhh Iiii}

\begin{document}

\chapter{Chapter 1}

\ab{ABC}, \ab{ABC}, \ab{ABC}

\ab{DEF}

\ab{GHI}

\chapter{Chapter 2}
\glsresetall % resetting after \chapter works

\ab{ABC}

\ab{DEF}, \ab{DEF}, \ab{DEF}

\ab{GHI}

\chapter{Chapter 3}
\glsresetall % ditto

\ab{ABC}

\ab{DEF}

\ab{GHI}, \ab{GHI}, \ab{GHI}

\printabbreviations

\end{document}


The problem here is that we can't use \AddToHook{cmd/chapter/after}{\glsresetall}, because \chapter...

I've been digging the code and, as far as I can tell, the tracking done by glossaries/glossaries-extra is not tied to the resetting of the counter, its reference counting is triggered when the reference occurs, and uses the current value of the "unitcounter". So this difference really baffles me.

The question is hence twofold:

• Why the difference?
• Is there a convenient way to get both things at the same time which does not rely on manually resetting the glossary entries after each chapter?
• Why not \AddToHook{cmd/chapter/before}{\glsresetall}? Jun 3 at 21:00
• @egreg It doesn't work, and that's the point of the question. The first (non-working) document uses precisely that... Jun 3 at 21:06
• As you see from my answer, you also need to remove \GlsXtrEnableEntryUnitCounting{abbreviation}{1}{chapter}. Jun 3 at 21:08
• @egreg No I can't, that does not produce the intended results, as I commented on your answer. Jun 3 at 21:10
• Why shouldn't the abbreviation be shown? Quite likely you use it later; if not, there is no need to define it. Jun 3 at 21:12

Redefine \chapter so adding after it works

\documentclass[oneside]{book}
\usepackage[a6paper]{geometry}
\usepackage{hyperref}

\usepackage[
abbreviations,
shortcuts=abbr,
]{glossaries-extra}

\makeglossaries
\NewCommandCopy{\latexchapter}{\chapter}
\RenewDocumentCommand{\chapter}{sO{#3}m}{%
\IfBooleanTF{#1}{\latexchapter*{#3}}{\latexchapter[#2]{#3}}%
}
\GlsXtrEnableEntryUnitCounting{abbreviation}{1}{chapter}

\newabbreviation{ABC}{ABC}{Aaaa Bbbb Cccc}
\newabbreviation{DEF}{DEF}{Dddd Eeee Ffff}
\newabbreviation{GHI}{GHI}{Gggg Hhhh Iiii}

\begin{document}

\chapter{Chapter 1}

\ab{ABC}, \ab{ABC}, \ab{ABC}

\ab{DEF}

\ab{GHI}

\chapter{Chapter 2}

\ab{ABC}

\ab{DEF}, \ab{DEF}, \ab{DEF}

\ab{GHI}

\chapter{Chapter 3}

\ab{ABC}

\ab{DEF}

\ab{GHI}, \ab{GHI}, \ab{GHI}

\printabbreviations

\end{document}


Note: a6paper and oneside are just to make a smaller picture.

• @gusbrs Reworked. Anyway, I don't see the purpose. Jun 3 at 21:24
• egreg, Ah, that's a good idea. Thank you! And, do you have a clue why the difference? I spent quite some time trying to track this in glossaries.sty and glossaries-extra.sty, and couldn't find a hint of a connection between \chapter (or the resetting of the chapter counter) and the entry counting machinery of glossaries... Jun 3 at 21:28
• @gusbrs Sorry, but I'm not going to investigate: the code is really very complex. Jun 3 at 21:28
• egreg, no problem, of course, I have seen it. ;-) I will wait a little though, in case someone happens to know, otherwise I'll accept you answer. Jun 3 at 21:31

Finally! (for why, see edit history)

Using \glsresetall every chapter for this purpose, alongside with \GlsXtrEnableEntryUnitCounting, creates two problems:

• First, issuing any reset command, resets also the entrycount, tampering with the counting.
• Second, and most importantly, \@gls@write@entryunitcounts is run at \AtEndDocument and loops over all gls entries, but only actually writes the data to the .aux file for those which are "set" or "used" at that point. So, if we reset all entries at every chapter, only those used in the last chapter will get written and will thus be available for the next run. Which is an arbitrary set, from the point of view of the whole document.

So, mixing the two procedures does not work well. An alternative form to "reset" the first (but not single) use of an entry for each chapter must be found. The best I could think of is the following:

\newcommand*{\restoreusedflag}{}
\ifnumcomp{\glsentrycurrcount{\glslabel}}{=}{0}
{%
\ifglsused{\glslabel}
{\renewcommand*\restoreusedflag{\csuse{@@glsunset}{\glslabel}}}{}%
\glsreset{\glslabel}%
}{}%
}
\ifglsused{\glslabel}{}{\restoreusedflag}%
\renewcommand*{\restoreusedflag}{}%
}


This essentially does two things. For the first occurrence of a given entry in each chapter (tested with the entrycounting data \ifnumcomp{\glsentrycurrcount{\glslabel}}{=}{0}): sets a macro \restoreusedflag which should do what the name says, when needed, and then resets the entry. When is it the restore needed?: when the used flag was set at the beginning of the command (at \glslinkpresetkeys), but was not at the end (at \glspostlinkhook). This means that we have used a \glstext-like command which does not set the used flag, but since we reset the entry, we must restore the previous state of things. One care is to use \@@glsunset instead of \glsunset not to affect the entry counter values (for \glsreset it makes no difference, since we know the entrycount is zero when it is run).

Since \glslinkpostsetkeys is only called for entries which trip the counting trigger, this won't affect the case of a "single" entry in the chapter.

Putting it all together:

% arara: pdflatex
% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex

\documentclass[oneside]{book}
\usepackage[a6paper]{geometry}

\usepackage{hyperref}

\usepackage[
abbreviations,
shortcuts=abbr,
]{glossaries-extra}

\makeglossaries

\newcounter{myuniquechapter}
\GlsXtrEnableEntryUnitCounting{abbreviation}{1}{myuniquechapter}

\newcommand*{\restoreusedflag}{}
\ifnumcomp{\glsentrycurrcount{\glslabel}}{=}{0}
{%
\ifglsused{\glslabel}
{\renewcommand*\restoreusedflag{\csuse{@@glsunset}{\glslabel}}}{}%
\glsreset{\glslabel}%
}{}%
}
\ifglsused{\glslabel}{}{\restoreusedflag}%
\renewcommand*{\restoreusedflag}{}%
}

\newabbreviation{ABC}{ABC}{Aaaa Bbbb Cccc}
\newabbreviation{DEF}{DEF}{Dddd Eeee Ffff}
\newabbreviation{GHI}{GHI}{Gggg Hhhh Iiii}

\begin{document}

\chapter{Chapter 1}

\ab{ABC}, \ab{ABC}, \ab{ABC}

\ab{DEF}

\ab{GHI}

\chapter{Chapter 2}

\ab{ABC}

\ab{DEF}, \ab{DEF}, \ab{DEF}

\ab{GHI}

\chapter{Chapter 3}

\ab{ABC}

\ab{DEF}

\ab{GHI}, \ab{GHI}, \ab{GHI}

\printabbreviations

\end{document}


I'm using myuniquechapter instead of chapter so that unnumbered chapters can also be handled properly. But if that's not needed, just go with \GlsXtrEnableEntryUnitCounting{abbreviation}{1}{chapter} and let go of the hook too.