As I get further into my PhD thesis I expect more of these pearls to be about LaTeX. This one came about while I was trying to satisfy a common writing style requirement (one of those rigidly enforced by my advisor): abbreviations and acronyms must be defined once and only once, namely when they are first used.
It's a little hard to remember to do this, and when the editing process involves wholesale reordering of paragraphs and sections, that can require moving the definition to the new first use of an acronym, which is annoying. I'm writing this thesis in separate files per chapter, which are then combined when compiling the whole thesis. So the first use might be in a different file altogether. Furthermore, the chapter files can be compiled individually, so I might want an acronym explained at first use in-chapter in that situation, even if it's omitted when compiling the final thesis.
To implement this, I ended up writing a self-modifying command. That bent m