% \iffalse meta-comment
%
% Copyright (C) 2026 Evan Berkowitz
%
% This work may be distributed and/or modified under the conditions of the
% LaTeX Project Public License, either version 1.3c of this license or (at
% your option) any later version.  The latest version of this license is in
%   https://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008 or later.
%
% This work has the LPPL maintenance status `maintained'.
% The Current Maintainer of this work is Evan Berkowitz.
% This work consists of the files mermin-equations.dtx and
% mermin-equations.ins and the derived file mermin-equations.sty.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{mermin-equations.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[2021/11/15]
%<package>\ProvidesPackage{mermin-equations}
%<*package>
    [2026/11/18 v0.1.1 Mermin-style equation tools]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\PassOptionsToPackage{hidelinks}{hyperref}
 % doc before v3 (LaTeX 2022-06) does not load hyperref for \href and
 % \url; hypdoc does, without breaking doc's index entries as a bare
 % \usepackage{hyperref} would.
\IfFormatAtLeastTF{2022-06-01}{}{\usepackage{hypdoc}}
\usepackage{amsmath}
\usepackage{mermin-equations}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{mermin-equations.dtx}
  \PrintChanges
  \clearpage
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \changes{v0.1}{2026/11/17}{Initial version, providing \cs{aka} (\cs{alsolabel}) and \cs{eq} (\cs{eqcite}).}
% \changes{v0.1.1}{2026/11/18}{Renamed from \textsf{mermin} to \textsf{mermin-equations} at CTAN's request; the commands are unchanged.}
%
% \GetFileInfo{mermin-equations.sty}
%
% \DoNotIndex{\begingroup,\endgroup,\def,\gdef,\let,\global,\else,\fi}
% \DoNotIndex{\ifx,\ifdefined,\expandafter,\noexpand,\unexpanded,\xdef}
% \DoNotIndex{\newcommand,\newif,\label,\do,\the,\toks@,\@xp,\@empty}
% \DoNotIndex{\AddToHook,\NeedsTeXFormat,\ProvidesPackage,\RequirePackage}
% \DoNotIndex{\@ifnextchar,\endinput}
%
% \title{The \textsf{mermin-equations} package\thanks{This document corresponds to
%   \textsf{mermin-equations}~\fileversion, dated \filedate.}}
% \author{Evan Berkowitz}
% \date{\filedate}
%
% \maketitle
%
% \begin{abstract}
% The \textsf{mermin-equations} package helps physicists write equations the way
% N.~David Mermin asks in ``What's Wrong with These Equations?''\
% \cite{mermin}.
% An equation is labeled by the noun phrases that describe it, as many as
% the prose needs, with \cs{aka}, and cited by one of those phrases
% with \cs{eq}, which prints the phrase and the number together.
% Citing an equation by a bare number becomes the harder thing to write.
% \end{abstract}
%
% \section{Introduction}
%
% In his 1989 column~\cite{mermin} Mermin proposed three rules for
% equations in physics writing.
% Fisher's rule asks that every displayed equation be numbered, whether or
% not the text refers to it yet, because readers, referees, and later papers
% will.
% The Good Samaritan rule asks that an equation be cited by a phrase as well
% as a number, so that the reader need not page back to learn what
% ``(11)'' says: one writes ``the anomaly condition~(11)'', never a bare
% ``(11)'' and never ``Eq.~(11) shows''.
% An equation number locates; the noun phrase says what is there.
% The third rule, that mathematics is prose, asks that a display be
% punctuated as part of its sentence, so that the paragraph reads straight
% through the equation.
%
% \LaTeX\ makes the Good Samaritan rule harder than it need be.
% A label is an arbitrary key, so nothing ties a citation to the phrase
% that should accompany it, and \textsf{amsmath} allows a numbered line
% exactly one \cs{label}, although the right phrase depends on why the
% equation is being cited.
% The same line may be ``the anomaly condition'' in one paragraph and
% ``the Pythagorean condition'' in another.
% This package makes the phrase the key.
% A line is labeled by each phrase that describes it, and \cs{eq} cites it
% by one of them, printing the phrase and the number.
%
% \section{Usage}
%
% \DescribeMacro{\aka}
% \DescribeMacro{\eq}
% Load the package with |\usepackage{mermin-equations}|.
% Then |\aka{|\meta{phrase}|}|, for ``also known as'', attaches
% \meta{phrase} as a key to the
% current equation, or to the current line of a multi-line display, in
% addition to its \cs{label}.
% It may be used any number of times on a line, before or after the
% \cs{label}, or on a line that has no \cs{label} at all.
% Every key refers to the same number and, with \textsf{hyperref}, to the
% same anchor.
% |\eq{|\meta{phrase}|}| prints \meta{phrase}, a tie, and the equation
% number in parentheses, linked by \textsf{hyperref} to the equation.
%
% Consider two-flavor chiral gauge theories in two dimensions, whose gauge
% anomaly cancels when the left- and right-handed charges obey
% \begin{equation}
%   L_1^2+L_2^2=R_1^2+R_2^2.
%   \label{anomaly condition}
%   \aka{Pythagorean condition}
% \end{equation}
% The source of this display, and of a paragraph that cites it, reads
% \begin{verbatim}
% \begin{equation}
%   L_1^2+L_2^2=R_1^2+R_2^2.
%   \label{anomaly condition}
%   \aka{Pythagorean condition}
% \end{equation}
%
% The 3450 model, with charges $(3,4\,\vert\,5,0)$, satisfies
% the \eq{anomaly condition}.
% Read as a statement about integers, the same line is
% the \eq{Pythagorean condition}, and $3^2+4^2=5^2$ is its
% smallest nontrivial solution.
% \end{verbatim}
% and typesets as
% \begin{quote}
% The 3450 model, with charges $(3,4\,\vert\,5,0)$, satisfies
% the \eq{anomaly condition}.
% Read as a statement about integers, the same line is
% the \eq{Pythagorean condition}, and $3^2+4^2=5^2$ is its
% smallest nontrivial solution.
% \end{quote}
% We advise leaving the article out of the phrase and in the prose.
% Capitalization is the reason.
% A sentence that opens with a citation needs ``The'', one that cites in
% the middle needs ``the'', and |The \eq{anomaly condition}| handles both,
% because the capital falls on the article, which the sentence supplies.
% With the article inside the key, the capitalized and lowercase forms
% would be different labels.
% Keeping it out also leaves the sentence free to say ``this'', ``that'',
% or nothing at all.
% This is only advice: a label such as |the anomaly condition| works like
% any other key.
%
% Lines of an \textsf{align} are labeled the same way, one set of phrases
% per line, and a line may be labeled by \cs{aka} alone:
% \begin{verbatim}
% \begin{align}
%   S &= \int d^2x\,\mathcal{L} \label{action} \aka{fermion action} \\
%   P_{L,R} &= \tfrac12(1\pm\gamma_5) \aka{chiral projectors}
% \end{align}
% \end{verbatim}
%
% \subsection{Phrases and keys}
%
% Labels written with an |eq:| prefix, as many papers already have, need
% no change: when no label \meta{phrase} exists, \cs{eq} uses
% |eq:|\meta{phrase}.
% For example, |\eq{shift relation}| cites the line labeled
% |\label{eq:shift relation}|; so does |\eq{eq:shift relation}|, and both
% print ``shift relation'', without the prefix.
% If |shift relation| labels a section, say, and |eq:shift relation|
% an equation, \cs{eq} cites the equation; if both label equations,
% different ones, \cs{eq} cites the first and warns.
%
% \cs{eq} prints its argument as the phrase.
% A phrase that cannot be a key, because it contains mathematics or a
% command, is given where the label is defined, as the optional argument of
% \cs{aka}, and every citation then uses the plain key.
% For example, a compact boson on a lattice may be given the Villain action
% \begin{equation}
%   S_\Gamma=\frac{\beta}{2}\sum_{\ell}\left(d\phi-2\pi n\right)_\ell^2.
%   \aka[$\Gamma$ action]{Gamma action}
%   \aka{Villain model}
% \end{equation}
% The source of this display, and of a paragraph that cites it by both
% phrases, reads
% \begin{verbatim}
% \begin{equation}
%   S_\Gamma=\frac{\beta}{2}\sum_{\ell}\left(d\phi-2\pi n\right)_\ell^2.
%   \aka[$\Gamma$ action]{Gamma action}
%   \aka{Villain model}
% \end{equation}
%
% The integer field $n$ makes the \eq{Gamma action} invariant
% under $\phi\to\phi+2\pi k$ with $n\to n+dk$.
% Summed over $n$, the \eq{Villain model} is a periodic function
% of each $d\phi_\ell$.
% \end{verbatim}
% and typesets as
% \begin{quote}
% The integer field $n$ makes the \eq{Gamma action} invariant
% under $\phi\to\phi+2\pi k$ with $n\to n+dk$.
% Summed over $n$, the \eq{Villain model} is a periodic function
% of each $d\phi_\ell$.
% \end{quote}
%
% A key that is not printable text, such as |E_field|, needs a phrase in
% the same way.
% Without one \cs{eq} prints the key verbatim and, once the label exists,
% warns.
% To give a phrase to a line that already has a \cs{label}, add \cs{aka}
% with the same key, |\label{E_field}\aka[$E$ field]{E_field}|, or replace
% the \cs{label} with it.
%
% \subsection{How \cs{aka} behaves}
%
% Because \cs{label} is left alone, two \cs{label}s on one line remain the
% error \textsf{amsmath} has always reported; only \cs{aka} adds keys.
% Citing a key with \cs{ref}, \cs{eqref}, \cs{pageref}, or the
% \textsf{cleveref} command \cs{cref} works exactly as for a key given to
% \cs{label}.
% Outside a display \cs{aka} is simply \cs{label}, so it can also give a
% section, a figure, or a theorem a second name, and it may stand inside a
% heading or caption just as \cs{label} may.
% \cs{eq}, however, is for equations only, since it prints a parenthesized
% number, and it warns when its key names anything else.
%
% On a line without a number, such as one with \cs{notag} or in
% |equation*|, \cs{aka} does what \cs{label} would do there, which is
% seldom what is wanted, and \cs{eq} cannot tell: on an \textsf{align}
% line it labels the next numbered line, even one with its own
% \cs{label}, or nothing on the last line; in \textsf{gather} its number
% is empty; and in |equation*| it takes the section's number.
% Put it on a numbered line.
%
% \subsection{Mermin's rules as warnings}
%
% Two of Mermin's rules become warnings, which never stop compilation.
% \emph{Fisher's rule}, ``number all displayed equations'', warns at
% every \textsf{amsmath} display, \textsf{empheq}, or \textsf{eqnarray}
% that typesets no number: |equation*|, |\[|\ldots|\]|, or an
% \textsf{align} whose every line is \cs{notag}.
% (Other packages' unnumbered displays, such as \textsf{breqn}'s |dmath*|,
% and plain \TeX's |$$|\ldots|$$|, are not checked.)
% A derivation that is numbered once, on its last line, is one equation
% and is numbered:
% \begin{verbatim}
% \begin{align}
%   f &= g \nonumber\\
%     &= h \label{f=h}
% \end{align}
% \end{verbatim}
% The \textsf{mathtools} option |showonlyrefs|, which numbers only the
% equations that are cited, is the practice Mermin calls Occam's rule, a
% heresy; with it, one warning says so.
% The \emph{Good Samaritan rule}, ``when referring to an equation identify
% it by a phrase as well as a number'', warns at every \cs{eqref},
% \cs{cref}, \cs{Cref}, or \cs{labelcref} of an equation, even one that
% words precede, as in |the Dirac equation~\eqref{Dirac}|: \cs{eq} gives
% the phrase and the number together, from one place.
% Page references such as \cs{cpageref} are not checked, and neither is
% plain \cs{ref}, which other commands use internally; so
% |Eq.~(\ref{x})|, a common REVTeX style, passes unremarked.
% The options |nofisher| and |nosamaritan| turn the warnings off.
%
% The warnings appear in the log file, and in the summaries of tools such
% as \texttt{latexmk}, like this:
% \begingroup
% \def\MacroFont{\footnotesize\ttfamily}
% \begin{verbatim}
% Package mermin-equations Warning: `equation*' display without a number.
% (mermin-equations)                Fisher's rule: number every display
% (mermin-equations)                (option nofisher) on input line 4.
%
% Package mermin-equations Warning: `chain' is cited by number alone.
% (mermin-equations)                Good Samaritan rule: name it, as \eq does
% (mermin-equations)                (option nosamaritan) on input line 12.
% \end{verbatim}
% \endgroup
% The first names the environment (|\[|\ldots|\]| is |equation*|) and
% the line where the display ends; the second names the key and the line
% of the citation.
% With |showonlyrefs| a single warning appears at |\begin{document}|:
% \begingroup
% \def\MacroFont{\footnotesize\ttfamily}
% \begin{verbatim}
% Package mermin-equations Warning: mathtools' showonlyrefs numbers only the
% (mermin-equations)                equations that are cited, which Mermin calls
% (mermin-equations)                Occam's rule; Fisher's rule numbers them all
% (mermin-equations)                (option nofisher silences this).
% \end{verbatim}
% \endgroup
%
% \subsection{Long names}
%
% \DescribeMacro{\alsolabel}
% \DescribeMacro{\eqcite}
% Short names are easily taken.
% \cs{alsolabel} and \cs{eqcite} are the same commands as \cs{aka} and
% \cs{eq}, under names no other package uses.
% Several packages, among them \textsf{nccmath}, define their own \cs{eq},
% and a document may define its own \cs{aka}.
% \textsf{mermin-equations} then leaves the existing command alone, says so in a
% warning, and the document uses the long name instead.
%
% \section{Compatibility}
%
% \subsection{Requirements and loading}
%
% The package requires a \LaTeX\ format from 2021-11-15 or later, for the
% hooks it uses, and loads \textsf{amsmath}.
% Load \textsf{amsmath} with options, such as |fleqn|, before
% \textsf{mermin-equations}, as before any package that loads it.
% \textsf{hyperref} and \textsf{cleveref} may be loaded before or after
% \textsf{mermin-equations}; \textsf{cleveref}, as always, after \textsf{amsmath}.
% arXiv compiles papers with its own fixed \TeX\ Live installations, which
% do not receive new packages, so a paper submitted there should include
% \texttt{mermin-equations.sty} among its files.
%
% \subsection{Displays}
%
% The package works in the \textsf{amsmath} environments \textsf{equation},
% \textsf{align}, \textsf{gather}, \textsf{multline}, \textsf{split} (with
% the keys given in the enclosing \textsf{equation}), and
% \textsf{subequations}, and in \textsf{empheq}; in \textsf{breqn}'s
% |dmath|, |dseries|, and |dgroup|; in the \textsf{cases} package's
% |numcases| and |subnumcases|; in \textsf{IEEEtrantools}' |IEEEeqnarray|;
% in \textsf{subeqnarray}; and in the kernel's \textsf{eqnarray}.
% With the \textsf{mathtools} option |showonlyrefs|, a line is numbered
% when any of its keys is cited.
%
% \subsection{Other documents}
%
% With \textsf{xr} or \textsf{xr-hyper}, |\eq{S-|\meta{key}|}| cites an
% equation of a document loaded with |\externaldocument[S-]|, and prints
% its phrase, or its key without the prefix; the |eq:| fallback reaches
% it too, so |\eq{S-sum rule}| cites |\label{eq:sum rule}| there.
% A document that does not use \textsf{mermin-equations} has no record of which
% labels are equations, so \cs{eq} treats all of its labels as equations.
% Load \textsf{mermin-equations} before calling \cs{externaldocument}: a document
% read earlier is imported without its phrases or its record of which
% labels are equations, so its keys print with the prefix, the |eq:|
% fallback does not reach it, and \cs{eq} warns about every one.
%
% A paper that adopts \textsf{mermin-equations} while \cs{includeonly} leaves out
% chapters may see such warnings for equations in those chapters, until
% each is compiled once with the package.
%
% \subsection{Headings, bookmarks, and running heads}
%
% \cs{aka}, like \cs{label}, vanishes from tables of contents, running
% heads, and PDF bookmarks.
% In a bookmark \cs{eq} gives its phrase and number as plain text.
% A phrase with mathematics is dropped from bookmarks, as mathematics is
% from any bookmark; \textsf{hyperref}'s \cs{texorpdfstring} gives a
% plain-text form, as in |\aka[\texorpdfstring{$E$}{E} field]{E_field}|.
%
% In a heading or running head that a class sets in capitals with
% \cs{MakeUppercase}, the phrase is left as written, from the \LaTeX\
% release of 2022-06 on, since it is printed
% from its key; for a running head, give the heading a short form without
% \cs{eq}, as in |\section[|\ldots|]{|\ldots|}|.
% REVTeX sets the section headings themselves in capitals, and the phrase
% stays lowercase there (``THE anomaly condition (1)''); \cs{eqref} in
% the same place fails altogether, printing ``(??)''.
% Classes that use the primitive \cs{uppercase}, such as \textsf{mnras}
% and \textsf{aastex631}, capitalize the key itself, which breaks
% \cs{eq} there as it breaks \cs{eqref}; wrap such a citation in a
% command defined in the preamble.
%
% \subsection{Classes and tools}
%
% In \textsf{beamer}, |\aka<|\meta{overlay}|>| passes the overlay to
% \cs{label}, as |\label<|\meta{overlay}|>| does.
% In a display the overlay of \cs{aka} or \cs{label} is dropped, and the
% label's anchor is on the frame's first slide.
%
% The \textsf{iopart} class conflicts with \textsf{amsmath}, which this
% package loads; when \textsf{iopart} is the class, the package applies
% IOP's usual remedy, undefining |equation*| first.
%
% \textsf{latexdiff} shows a changed \cs{eq} only when told that it is
% safe to keep in deleted text, with |--append-safecmd=eq,eqcite|; do not
% add |aka|, which would bring deleted labels back.
% An \cs{aka} in text \textsf{latexdiff} marks as deleted is ignored.
%
% \textsf{showkeys} and \textsf{showlabels} show the first key of a display
% line, not the keys \cs{aka} adds to it.
%
% \subsection{Not supported}
%
% \textsf{autonum} copies \cs{label} before this package can attach its
% extra keys, and numbers a line by its first key only.
% \textsf{witharrows}' |DispWithArrows| keeps its labels where \cs{aka}
% cannot reach them.
%
% \section{Testing}
%
% The package's repository,
% \url{https://github.com/evanberkowitz/mermin}, also holds a test suite
% and the issue tracker.
% Running |make test| there typesets every environment above, with the
% keys given before, after, and instead of \cs{label}, in four package
% configurations, and checks that each key in the resulting \texttt{.aux}
% file carries its line's number and anchor.
% It also checks the text \cs{eq} prints, its warnings, and each of the
% interactions above.
% The suite runs with pdf\LaTeX, Lua\LaTeX, and Xe\LaTeX\ on the current
% \TeX\ Live, and with pdf\LaTeX\ on \TeX\ Live 2021, 2023, and 2025.
%
% \begin{thebibliography}{1}
% \bibitem{mermin}
%   N.~D. Mermin, ``What's Wrong with These Equations?'',
%   \emph{Physics Today} \textbf{42}(10), 9 (1989),
%   \href{https://doi.org/10.1063/1.2811173}{doi:10.1063/1.2811173}.
% \end{thebibliography}
%
% \StopEventually{}
%
% \section{Implementation}
%
% The implementation has eleven parts.
% Parts~\ref{impl:load}--\ref{impl:aux} set up the package and the records
% it keeps in the \texttt{.aux} file.
% Parts~\ref{impl:aka}--\ref{impl:queue} define \cs{alsolabel} and the
% machinery that gives a display line several keys.
% Part~\ref{impl:xr} imports the records of another document through
% \textsf{xr}.
% Parts~\ref{impl:cite}--\ref{impl:short} define \cs{eqcite} and the
% short names, and make both behave in headings and bookmarks.
% Part~\ref{impl:rules} turns two of Mermin's rules into warnings.
%
% The narrative is here; the code carries short comments wherever a
% construction looks odd or exists for one particular package, kernel
% version, or engine, so that a reader of the bare \texttt{mermin-equations.sty} is
% not lost.
% Two words are kept apart: a key is \emph{marked} when it is recorded as
% an equation, and an \texttt{.aux} file is \emph{signed} when it was
% written with this package.
%
% \subsection{Loading}\label{impl:load}
%
% The package needs \textsf{amsmath}, whose display machinery it extends.
% IOP's \textsf{iopart} class defines |equation*| itself, which
% \textsf{amsmath} then refuses to redefine; IOP's documented remedy is to
% undefine it first, and the package does so when \textsf{iopart} is the
% class and \textsf{amsmath} is not yet loaded.
%    \begin{macrocode}
%<*package>
 % iopart.cls defines equation*, and amsmath would stop with "Command
 % \equation* already defined"; this is IOP's own workaround.  amsmath's
 % \@xp is not defined yet, hence \expandafter.
\@ifclassloaded{iopart}{%
  \@ifpackageloaded{amsmath}{}{%
    \expandafter\let\csname equation*\endcsname\relax
    \expandafter\let\csname endequation*\endcsname\relax}}{}
\RequirePackage{amsmath}
%    \end{macrocode}
%
% Two options turn off the warnings of part~\ref{impl:rules}.
%    \begin{macrocode}
\newif\ifmermin@fisher \mermin@fishertrue
\newif\ifmermin@samaritan \mermin@samaritantrue
\DeclareOption{nofisher}{\mermin@fisherfalse}
\DeclareOption{nosamaritan}{\mermin@samaritanfalse}
\ProcessOptions\relax
%    \end{macrocode}
%
% \subsection{Utilities}\label{impl:util}
%
% \begin{macro}{\mermin@warn}
% \begin{macro}{\mermin@warn@noline}
% Every warning goes through these, so the package's name is written once.
% A warning's lines are kept under 45 characters: the prefix
% |(mermin-equations)| and its padding take 34 of the 79 that \TeX\
% prints before it breaks a line of the log.
%    \begin{macrocode}
\def\mermin@warn{\PackageWarning{mermin-equations}}
\def\mermin@warn@noline{\PackageWarningNoLine{mermin-equations}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mermin@firstchar}
% \begin{macro}{\mermin@ifeqprefix}
% Two expandable tests on a key look at its characters rather than its
% tokens.
% |\mermin@firstchar{|\meta{text}|}| expands to the first character of
% \meta{text}, as a character of category 12.
% |\mermin@ifeqprefix{|\meta{key}|}{|\meta{true}|}{|\meta{false}|}| tests
% whether \meta{key} begins with |eq:|.
% Both detokenize first, for two reasons.
% The raw key is never scanned as a delimited argument, which inside an
% alignment would end the cell at an |&| in the key; and the test does not
% depend on categories, which \textsf{babel} languages change: French makes
% the colon active in the document body.
% |\if| compares character codes only, so the category-12 characters of
% the detokenized key match the letters typed here.
%    \begin{macrocode}
 % Detokenized, so that an & in a key cannot end an alignment cell and
 % babel's active characters still compare as characters.
\def\mermin@firstchar#1{%
  \@xp\mermin@firstchar@\detokenize{#1}\relax\mermin@nil}
 % Without the \relax an empty key would make #1 \mermin@nil, and #2
 % would run away looking for it.
\def\mermin@firstchar@#1#2\mermin@nil{#1}
 % The three \relax pad keys shorter than three characters.
\def\mermin@ifeqprefix#1{%
  \@xp\mermin@ifeqprefix@\detokenize{#1}\relax\relax\relax\mermin@nil}
\def\mermin@ifeqprefix@#1#2#3#4\mermin@nil{%
  \ifnum\if e#1\if q#2\if :#3 1\else 0\fi\else 0\fi\else 0\fi=1
    \@xp\@firstoftwo
  \else
    \@xp\@secondoftwo
  \fi}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mermin@ifunprintable}
% \begin{macro}{\mermin@verbatim}
% A key that is printed as text, because it has no typeset phrase, must
% not contain characters that typesetting would misread.
% |\mermin@ifunprintable| tests for them; |\mermin@verbatim| prints a key
% character by character in typewriter type, whose fonts have every
% \textsc{ascii} character.
% Braces are not in the list: \cs{in@} cannot find a brace character, and
% a key with braces prints, only without them.
%    \begin{macrocode}
\edef\mermin@specials{% < > | print as other glyphs in OT1 fonts
  \detokenize{_^$&<>|}\@percentchar\@backslashchar}
\def\mermin@ifunprintable#1{%
  \edef\mermin@tmp{\detokenize{#1}}%
  \let\mermin@bad\@empty
  \@xp\@tfor\@xp\mermin@c\@xp:\@xp=\mermin@specials\do{%
    \edef\reserved@a{\noexpand\in@{\mermin@c}{\mermin@tmp}}%
    \reserved@a
    \ifin@\def\mermin@bad{x}\fi}%
  \ifx\mermin@bad\@empty\@xp\@secondoftwo\else\@xp\@firstoftwo\fi}
\def\mermin@verbatim#1{{\normalfont\ttfamily\detokenize{#1}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Records in the \texttt{.aux} file}\label{impl:aux}
%
% Three kinds of record travel through the \texttt{.aux} file, like
% labels, so that a citation may come before its equation:
% \begin{itemize}
% \item the typeset phrase given to \cs{alsolabel}, as |\mermin@newphrase|
%   with the arguments \meta{key} and \meta{phrase}, which defines
%   |\mermin@ph@|\meta{key};
% \item |\mermin@equation{|\meta{key}|}|, which marks \meta{key} as an
%   equation by defining |\mermin@eq@|\meta{key}, so that \cs{eqcite} can
%   warn about anything else;
% \item |\mermin@signature|, once per file, which signs the file as
%   written with this package and sets |\mermin@signed|.
% \end{itemize}
% Until a signed file has been read, \cs{eqcite} does not warn about
% non-equations: a paper that has just adopted the package has an
% \texttt{.aux} file full of equation labels and no marks for them.
%
% \begin{macro}{\mermin@record}
% \begin{macro}{\mermin@writephrase}
% \begin{macro}{\mermin@markeq}
% Records do not depend on the page, so they are written immediately.
% The argument is expanded as \cs{label} expands its key, robust commands
% excepted, so that a key such as |sec\thesection:mass| is recorded as the
% same key that \cs{label} writes.
%    \begin{macrocode}
\def\mermin@record#1{%
  \if@filesw
     % Expand the key, robust commands excepted, as \label's write does
     % (which expands once more at shipout), but write now.
    \begingroup
      \let\protect\@unexpandable@protect
      \immediate\write\@auxout{#1}%
    \endgroup
  \fi}
\def\mermin@writephrase#1#2{%
  \mermin@record{\string\mermin@newphrase{#1}{\unexpanded{#2}}}}
\def\mermin@markeq#1{\mermin@record{\string\mermin@equation{#1}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mermin@auxheader}
% \begin{macro}{\mermin@equation}
% \begin{macro}{\mermin@signature}
% At the beginning of the document the package writes, to each
% \texttt{.aux} file, |\providecommand|s for the three records, so that a
% document that stops loading the package still reads its old files, and
% then the signature.
% Files of chapters read with \cs{include} get the same lines: under
% \cs{includeonly} their old files are read without being rewritten.
%    \begin{macrocode}
\def\mermin@auxheader#1{%
  \immediate\write#1{%
    \string\providecommand\string\mermin@newphrase[2]{}%
    \string\providecommand\string\mermin@equation[1]{}%
    \string\providecommand\string\mermin@signature{}}%
   % The signature gets a line of its own: xr looks only at the first
   % token of each line.
  \immediate\write#1{\string\mermin@signature}}
\AtBeginDocument{\if@filesw\mermin@auxheader\@auxout\fi}
 % Under \includeonly, the old .aux files of excluded chapters are read
 % but not rewritten, so each needs its own \providecommand lines.
\AddToHook{include/before}{\if@filesw\mermin@auxheader\@partaux\fi}
\def\mermin@signature{\global\let\mermin@signed\@empty}
\def\mermin@equation#1{\@xp\gdef\csname mermin@eq@#1\endcsname{}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mermin@newphrase}
% \begin{macro}{\mermin@checkphrases}
% When \LaTeX\ rereads the \texttt{.aux} file at the end of the document
% to look for changed labels, a changed phrase also asks for another run.
% So does a removed one: the keys that had phrases when the run began are
% listed in |\mermin@phkeys|, and after the reread any of them that was
% not seen again sets the same flag.
% Phrases are compared by their characters only.
%    \begin{macrocode}
\let\mermin@phkeys\@empty
\def\mermin@newphrase#1#2{%
   % The end-of-document reread is recognized by \@setckpt, which the
   % kernel (and REVTeX) makes \@gobbletwo only then.  \@newl@bel is the
   % obvious test, but acronym changes it during the reread.
  \ifx\@setckpt\@gobbletwo
    \global\@namedef{mermin@seen@#1}{}%
     % Detokenized: unicode-math changes the category of letters such as
     % a literal Gamma after the .aux file is first read, and comparing
     % tokens would ask for a rerun forever.
    \edef\reserved@a{\detokenize{#2}}%
    \edef\reserved@b{%
      \detokenize\@xp\@xp\@xp{\csname mermin@ph@#1\endcsname}}%
    \ifx\reserved@a\reserved@b\else\@tempswatrue\fi
  \else
    \@xp\gdef\csname mermin@ph@#1\endcsname{#2}%
    \g@addto@macro\mermin@phkeys{\do{#1}}%
  \fi}
\def\mermin@checkphrases{%
   % Runs inside \enddocument's group, so \do is redefined only there.
  \def\do##1{\@ifundefined{mermin@seen@##1}{\@tempswatrue}{}}%
  \mermin@phkeys}
 % afteraux runs after the reread and before the kernel's rerun warning.
\AddToHook{enddocument/afteraux}{\if@filesw\mermin@checkphrases\fi}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{The command \cs{alsolabel}}\label{impl:aka}
%
% \begin{macro}{\alsolabel}
% Where \LaTeX\ disables \cs{label}, as it does in the text written to a
% table of contents or a running head, \cs{alsolabel} must disappear with
% it, together with its arguments.
% \cs{alsolabel} is therefore expandable: it looks at what \cs{label}
% means and expands either to a gobbler or to the real command,
% |\mermin@alsolabel|, which is robust.
% Releases before 2025-06-01 disable \cs{label} as |\@gobble| or
% |\relax|, as \textsf{hyperref}, \textsf{memoir}, and REVTeX do on any
% release; later releases as |\@gobble@om|, or |\@gobble@with@sphack@om|
% where the text is typeset, which \cs{alsolabel} then uses itself; and
% the \textsf{caption} package as |\caption@gobble|.
%
% \cs{alsolabel} also disappears in text that \textsf{latexdiff} marks as
% deleted.
% \textsf{latexdiff} keeps a deleted display, as |displaymath| between
% |\DIFdelbegin| and |\DIFdelend|, and comments out the \cs{label}s in
% it, but its list of label commands is fixed and cannot be extended; a
% deleted \cs{alsolabel} would define its key a second time, possibly with
% the wrong number.
% The package therefore sets |\ifmermin@deleted| between the two markers.
% \textsf{latexdiff} defines them in the preamble, after
% |\usepackage{mermin-equations}|, so they are wrapped only at
% |begindocument/end|.
%    \begin{macrocode}
\newif\ifmermin@deleted
\AddToHook{begindocument/end}{%
   % latexdiff defines its markers in the preamble, after
   % \usepackage{mermin-equations}.  They are made \protected so that,
   % in a moving argument such as a heading, they are written as
   % themselves and still set the flag when the heading is typeset
   % again.
  \ifdefined\DIFdelbegin
    \let\mermin@DIFdelbegin\DIFdelbegin
    \let\mermin@DIFdelend\DIFdelend
    \protected\def\DIFdelbegin{%
      \global\mermin@deletedtrue\mermin@DIFdelbegin}%
    \protected\def\DIFdelend{%
      \mermin@DIFdelend\global\mermin@deletedfalse}%
  \fi}
\def\alsolabel{%
  \ifmermin@deleted \mermin@aka@use\mermin@gobble@aka\fi
   % \@gobble and \relax: kernels before 2025-06, and hyperref,
   % memoir, and REVTeX on any kernel.
  \ifx\label\@gobble \mermin@aka@use\mermin@gobble@aka\fi
  \ifx\label\relax \mermin@aka@use\mermin@gobble@aka\fi
   % \@gobble@om and \@gobble@with@sphack@om: kernels from 2025-06.
  \ifx\label\@gobble@om \mermin@aka@use\mermin@gobble@aka\fi
  \ifx\label\@gobble@with@sphack@om
    \mermin@aka@use\@gobble@with@sphack@om\fi
   % caption disables \label this way, in its single-line check and in
   % subcaption's list of figures.
  \ifx\label\caption@gobble \mermin@aka@use\mermin@gobble@aka\fi
  \mermin@alsolabel}
 % \mermin@aka@use ends the test that matched, drops the remaining tests
 % up to \mermin@alsolabel, and leaves #1 in its place.
\def\mermin@aka@use#1#2\mermin@alsolabel{\fi#1}
\NewExpandableDocumentCommand\mermin@gobble@aka{d<> o m}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mermin@alsolabel}
% The real command records the optional phrase and then calls whatever
% \cs{label} means at that point, with the flag |\ifmermin@also| raised.
% In a display \cs{label} is |\label@in@display|, which
% part~\ref{impl:queue} wraps to read the flag; elsewhere \cs{label} acts as
% usual, and \cs{alsolabel} gives a section or a figure a second name.
% The measuring passes of \textsf{amsmath} gobble or defer \cs{label}, and
% \cs{alsolabel} inherits the same treatment.
% A \textsf{beamer} overlay, |<|\meta{overlay}|>|, goes to \textsf{beamer}'s
% \cs{label} outside displays, and is dropped inside them.
% |\mermin@doclabel| is the document's \cs{label}, saved at
% |begindocument/end| after \textsf{hyperref} and \textsf{beamer} have set
% it.
%    \begin{macrocode}
\newif\ifmermin@also
\AddToHook{begindocument/end}{% after hyperref and beamer set \label
  \let\mermin@doclabel\label}
\NewDocumentCommand\mermin@alsolabel{d<> o m}{%
   % \label ends with \@esphack, but inside the group below it cannot
   % remove the space after the command in running text; the command
   % therefore has its own \@bsphack and \@esphack.
  \@bsphack
  \IfValueT{#2}{\mermin@writephrase{#3}{#2}}%
  \begingroup
    \mermin@alsotrue
     % cleveref's multline trial pass calls this hook directly instead
     % of \label@in@display; see \mermin@cref@entry.
    \ifdefined\mermin@cref@old
      \let\cref@old@label@in@display\mermin@also@stored
    \fi
    \IfValueTF{#1}%
       % Only beamer's \label takes an overlay: the document's \label,
       % or \mermin@marklabel wrapping it.  amsmath's display \label
       % does not.
      {\ifx\label\mermin@doclabel
         \label<#1>{#3}%
       \else\ifx\label\mermin@marklabel
         \label<#1>{#3}%
       \else
         \label{#3}%
       \fi\fi}%
      {\label{#3}}%
  \endgroup
  \@esphack}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Which keys are equations}\label{impl:mark}
%
% \begin{macro}{\mermin@markargs}
% \begin{macro}{\mermin@keyof}
% A key is marked as an equation where a display registers it, and not
% where labels are written: |\ltx@label|, which writes them, is reached
% from outside displays too.
% A key arrives as |{|\meta{key}|}|, as |[|\meta{type}|]{|\meta{key}|}|
% with \textsf{cleveref}'s optional argument, or as \textsf{amsmath} stores
% it without \textsf{cleveref}, the bare \meta{key}.
% |\mermin@keyof| sets |\mermin@rawkey| to the key in any of these forms.
%    \begin{macrocode}
\def\mermin@markargs#1{%
   % The \iffalse braces raise TeX's alignment brace count without
   % opening a group, so that an & in a bare key, scanned as a delimited
   % argument inside align, does not end the cell.
  \iffalse{\fi
  \mermin@keyof#1\mermin@nil
  \@xp\mermin@markeq\@xp{\mermin@rawkey}%
  \iffalse}\fi}
 % [type] is cleveref's optional argument of \label.
\def\mermin@keyof{\@ifnextchar[\mermin@keyof@opt\mermin@keyof@plain}
\def\mermin@keyof@plain#1\mermin@nil{\def\mermin@rawkey{#1}}
\def\mermin@keyof@opt[#1]#2\mermin@nil{\def\mermin@rawkey{#2}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mermin@wraplabel}
% \begin{macro}{\mermin@marklabel}
% Some equations are labeled by an ordinary \cs{label}, not by
% |\label@in@display|: the label of a \textsf{subequations} group, numbered
% like ``(12)'', and the labels in \textsf{breqn}'s displays, in the
% \textsf{cases} package's |numcases|, in \textsf{IEEEtrantools}'
% |IEEEeqnarray|, in \textsf{subeqnarray}, and in the kernel's
% \textsf{eqnarray}.
% Inside these environments \cs{label} is |\mermin@marklabel|, which
% marks its key and calls the \cs{label} it replaced.
% The \textsf{amsmath} displays within them install their own \cs{label},
% and an environment nested in another, such as |dmath| in |dgroup|,
% finds \cs{label} already wrapped and leaves it.
%    \begin{macrocode}
 % For environments that label equations with the ordinary \label.
\def\mermin@wraplabel{%
  \ifx\label\mermin@marklabel\else
    \let\mermin@marklabel@orig\label
    \let\label\mermin@marklabel
  \fi}
 % beamer's \label takes an overlay, <...>, before the optional
 % argument.
\def\mermin@marklabel{%
  \@ifnextchar<\mermin@marklabel@ov
    {\@ifnextchar[\mermin@marklabel@opt\mermin@marklabel@plain}}
\def\mermin@marklabel@plain#1{%
  \mermin@markeq{#1}\mermin@marklabel@orig{#1}}
\def\mermin@marklabel@opt[#1]#2{%
  \mermin@markeq{#2}\mermin@marklabel@orig[#1]{#2}}
\def\mermin@marklabel@ov<#1>#2{%
  \mermin@markeq{#2}\mermin@marklabel@orig<#1>{#2}}
\def\mermin@markenv#1{\AddToHook{env/#1/begin}{\mermin@wraplabel}}
\@for\mermin@env:=%
    subequations,% amsmath
    dmath,dseries,dgroup,% breqn
    numcases,subnumcases,% cases
    IEEEeqnarray,IEEEeqnarray*,% IEEEtrantools
    eqnarray,% LaTeX kernel
    subeqnarray% subeqnarray
  \do{\@xp\mermin@markenv\@xp{\mermin@env}}
 % subeqnarray labels each row with \slabel, which writes \newlabel
 % itself.
\AddToHook{env/subeqnarray/begin}{%
  \let\mermin@slabel@orig\slabel
  \def\slabel#1{\mermin@markeq{#1}\mermin@slabel@orig{#1}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Extra keys on a display line}\label{impl:queue}
%
% \textsf{amsmath} does not write a display label when it meets \cs{label}.
% It stores the key in |\df@label| and writes it with
% |\ltx@label{|\meta{contents of \texttt{\char`\\df@label}}|}| once the
% line's tag, and so |\@currentlabel| and the \textsf{hyperref} anchor, are
% known.
% The package keeps the extra keys of a line in a queue and writes them
% immediately after that primary label, so they receive the same number and
% anchor.
%
% Three complications shape the code.
% First, \textsf{cleveref} stores keys in a different form, wrapping the
% key in braces or preceding it with an optional argument, so every queued
% key is stored in exactly the form |\label@in@display| would have stored
% it.
% Second, \textsf{amsmath} and \textsf{empheq} typeset some displays twice,
% once to measure them, and may discard a primary label without writing
% it.
% The queue is therefore tagged with the primary label it belongs to and
% written only when that label is, and a key is never queued twice.
% Third, \cs{alsolabel} may come before \cs{label} or without it.
% It then occupies |\df@label| itself, \emph{promoted}, and a later
% \cs{label} reclaims the slot and moves the earlier key into the queue.
%
% \begin{macro}{\mermin@queue}
% \begin{macro}{\mermin@tag}
% \begin{macro}{\mermin@promoted}
% The queue holds extra keys as |\do{|\meta{stored form}|}|.
% The tag is the |\df@label| the queue belongs to.
% |\mermin@promoted| is nonempty while \cs{alsolabel} occupies
% |\df@label|.
% All three are global, like |\df@label| itself, because the cells of an
% alignment are groups.
%    \begin{macrocode}
\gdef\mermin@queue{}    % the line's extra keys, as \do{<stored form>}
\gdef\mermin@tag{}      % the \df@label the queue belongs to
\gdef\mermin@promoted{} % an \aka key occupying \df@label, if any
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mermin@store}
% \begin{macro}{\mermin@markstore}
% \begin{macro}{\mermin@canonical}
% |\mermin@store| calls the |\label@in@display| that was in force before
% the package wrapped it, which stores the key in |\df@label|; |\mermin@markstore| also marks the key as an
% equation.
% Their argument is the complete argument list, braces included.
% |\mermin@canonical{|\meta{key}|}| sets |\mermin@form| to the form in which
% that |\label@in@display| would store \meta{key}, by letting it store the
% key into an emptied |\df@label| and restoring the old contents.
%    \begin{macrocode}
\def\mermin@store#1{%
  \begingroup
     % Undo, locally, the redirection of cleveref's inner hook.
    \ifdefined\mermin@cref@old
      \let\cref@old@label@in@display\mermin@cref@old
    \fi
    \mermin@orig@label@in@display#1%
  \endgroup}
\def\mermin@markstore#1{\mermin@markargs{#1}\mermin@store{#1}}
\def\mermin@canonical#1{%
   % Let the original store #1 into an empty \df@label, read the stored
   % form back, and restore \df@label: cleveref and amsmath store keys
   % differently, and queued keys must match the form exactly.
  \global\let\mermin@saved\df@label
  \global\let\df@label\@empty
  \mermin@store{{#1}}%
  \global\let\mermin@form\df@label
  \global\let\df@label\mermin@saved}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mermin@enqueue}
% This queues |\mermin@form| for the current |\df@label|.
% A queue tagged with a different label is stale and is discarded first.
% A form already in the queue is skipped: a display typeset twice runs its
% \cs{alsolabel}s twice, and \cs{text} typesets its argument four times.
% With the \textsf{mathtools} option |showonlyrefs| a line is numbered only
% if its primary label was cited, which \textsf{mathtools} records as
% |\MT_r_|\meta{label}; a citation of a queued key counts for the line too.
%    \begin{macrocode}
\def\mermin@enqueue{%
  \ifx\mermin@tag\df@label\else
    \gdef\mermin@queue{}%
    \global\let\mermin@tag\df@label
  \fi
  \iffalse{\fi % alignment guard, as in \mermin@markargs
   % mathtools' showonlyrefs numbers a line whose label is cited, which
   % it records as \MT_r_<label>: a cited queued key counts too.
  \@xp\mermin@keyof\mermin@form\mermin@nil
  \@ifundefined{MT_r_\mermin@rawkey}{}%
    {\global\@namedef{MT_r_\df@label}{}}%
  \iffalse}\fi
  \begingroup
    \let\mermin@found\@empty
    \def\do##1{\def\mermin@tmp{##1}%
      \ifx\mermin@tmp\mermin@form \def\mermin@found{x}\fi}%
    \mermin@queue
    \ifx\mermin@found\@empty
      \toks@\@xp{\mermin@form}%
      \xdef\mermin@queue{%
        \unexpanded\@xp{\mermin@queue}\noexpand\do{\the\toks@}}%
    \fi
  \endgroup}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mermin@also@in@display}
% \begin{macro}{\mermin@also@stored}
% \cs{alsolabel} in a display converts its key to stored form.
% A key the line's label already has adds nothing, so that
% |\label{K}\aka[|\meta{phrase}|]{K}| gives an existing label a phrase.
% If the line has no label yet, the key is promoted; otherwise it is
% queued.
%    \begin{macrocode}
\def\mermin@also@in@display#1{%
  \mermin@canonical{#1}%
  \@xp\mermin@also@stored\@xp{\mermin@form}}
\def\mermin@also@stored#1{%
  \mermin@markargs{#1}%
  \def\mermin@tmp{#1}%
  \ifx\mermin@tmp\df@label   % the line already has this key
  \else\ifx\df@label\@empty % no \label yet: promote this key
    \gdef\df@label{#1}%
    \global\let\mermin@promoted\df@label
    \global\let\mermin@tag\df@label
    \gdef\mermin@queue{}%
  \else
    \gdef\mermin@form{#1}%
    \mermin@enqueue
  \fi\fi}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mermin@real@in@display}
% A real \cs{label} normally passes straight through, and so still reports
% a second \cs{label} on one line as an error.
% If instead the line's label was promoted from \cs{alsolabel}, the real
% key takes the slot and the promoted key joins the queue, unless it is
% the same key.
% A promotion left over from a discarded measuring pass no longer matches
% |\df@label| and is cleared.
%    \begin{macrocode}
\def\mermin@real@in@display#1{%
  \ifx\mermin@promoted\@empty
    \mermin@markstore{#1}%
  \else\ifx\mermin@promoted\df@label
     % The line's label is a promoted \aka key: the real key takes the
     % slot, and the promoted one is queued unless it is the same key.
    \global\let\mermin@form\df@label
    \ifx\mermin@tag\df@label\else\gdef\mermin@queue{}\fi
    \global\let\df@label\@empty
    \mermin@markstore{#1}%
    \global\let\mermin@tag\df@label
    \ifx\mermin@form\df@label\else\mermin@enqueue\fi
    \gdef\mermin@promoted{}%
  \else
     % A promotion left over from a discarded measuring pass.
    \gdef\mermin@promoted{}%
    \mermin@markstore{#1}%
  \fi\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mermin@label@in@display}
% \begin{macro}{\mermin@cref@entry}
% A real \cs{label} in a display arrives at |\mermin@label@in@display|,
% with \textsf{cleveref}'s optional argument passed through.
% \textsf{beamer}'s display \cs{label} takes an overlay, which is dropped.
%
% \textsf{cleveref}'s \textsf{multline} trial pass bypasses
% |\label@in@display| and hands its inner hook,
% |\cref@old@label@in@display|, the stored key: braced, |{|\meta{key}|}|,
% with \textsf{amsmath} before~2.17z, but bare with \textsf{amsmath}~2.17z
% (\TeX\ Live 2025 and later).
% |\mermin@cref@entry| accepts either, or |[|\meta{type}|]{|\meta{key}|}|.
%    \begin{macrocode}
\def\mermin@label@in@display{%
  \@ifnextchar<\mermin@label@in@display@ov
    {\@ifnextchar[\mermin@label@in@display@opt
                 \mermin@label@in@display@plain}}
\def\mermin@label@in@display@ov<#1>{% beamer: drop the overlay
  \mermin@label@in@display}
\def\mermin@label@in@display@opt[#1]#2{%
  \mermin@real@in@display{[#1]{#2}}}
\def\mermin@label@in@display@plain#1{\mermin@real@in@display{{#1}}}
 % The forms are told apart by first character, never by scanning a
 % bare key, which may hold an &.  \@charlb is a brace character of
 % category 12.
\def\mermin@cref@entry#1{%
  \if\mermin@firstchar{#1}[\@xp\@firstoftwo\else\@xp\@secondoftwo\fi
    {\mermin@cref@entry@opt#1\mermin@nil}%
    {\if\mermin@firstchar{#1}\@charlb
       \@xp\@firstoftwo\else\@xp\@secondoftwo\fi
       {\mermin@real@in@display{#1}}%
       {\mermin@real@in@display{{#1}}}}}
\def\mermin@cref@entry@opt[#1]#2\mermin@nil{%
  \mermin@real@in@display{[#1]{#2}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\ltx@label}
% \begin{macro}{\label@in@display}
% \textsf{hyperref} and \textsf{cleveref} install their own |\ltx@label|
% and |\label@in@display| in |\AtBeginDocument| code.
% The package wraps them in the |begindocument/end| hook, which runs after
% all such code, so that load order does not matter.
% The new |\ltx@label| writes the primary label as before and then, if the
% queue belongs to it, every queued key except the primary one, which an
% |\aka{A}\aka{K}\label{K}| line would otherwise write twice.
% The new |\label@in@display| sends \cs{alsolabel} and \cs{label} their
% separate ways.
%    \begin{macrocode}
 % begindocument/end runs after hyperref's and cleveref's
 % \AtBeginDocument code, which define their own versions of both.
\AddToHook{begindocument/end}{%
  \let\mermin@ltx@label\ltx@label
  \def\ltx@label#1{%
    \mermin@ltx@label{#1}%
    \def\mermin@tmp{#1}%
    \ifx\mermin@tmp\mermin@tag
      \begingroup
         % Skip a queued key equal to the label just written, as on an
         % \aka{A}\aka{K}\label{K} line.
        \def\do##1{\def\mermin@tmp{##1}%
          \ifx\mermin@tmp\mermin@tag\else\mermin@ltx@label{##1}\fi}%
        \mermin@queue
      \endgroup
      \gdef\mermin@queue{}\gdef\mermin@tag{}\gdef\mermin@promoted{}%
    \fi}%
  \let\mermin@orig@label@in@display\label@in@display
   % cleveref's multline trial pass calls its inner hook directly, not
   % \label@in@display; give it the same treatment, which \mermin@store
   % undoes locally.
  \ifdefined\cref@old@label@in@display
    \let\mermin@cref@old\cref@old@label@in@display
    \def\cref@old@label@in@display#1{\mermin@cref@entry{#1}}%
  \fi
  \def\label@in@display{%
    \ifmermin@also
      \expandafter\mermin@also@in@display
    \else
      \expandafter\mermin@label@in@display
    \fi}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Documents read through \textsf{xr}}\label{impl:xr}
%
% \begin{macro}{\mermin@xr@test}
% \begin{macro}{\mermin@xr@import}
% \textsf{xr} and \textsf{xr-hyper} import another document's labels, as
% for a paper and its supplement, by reading that document's \texttt{.aux}
% file a line at a time with |\XR@test|, which keeps only \cs{newlabel}
% and \cs{bibcite} lines.
% A wrapper also imports the records, with the external document's prefix
% added to each key.
% For every equation key it imports, it stores the text that \cs{eqcite}
% prints when the key has no phrase, the key without the prefix (and
% without |eq:|), in |\mermin@xrdef@|\meta{prefixed key}; and for a key
% |eq:|\meta{rest} it defines the |eq:| fallback
% |\mermin@xreq@|\meta{prefix}\meta{rest}, which \cs{eqcite}'s resolver
% calls.
% A document that does not use the package records no equations; its file
% is not signed, and all its labels are imported as equations rather than
% warned about.
% The wrapper is installed whether \textsf{xr} or \textsf{xr-hyper} is
% loaded before or after this package, and again if both are; an \cs{externaldocument} called before the package
% is loaded is beyond its reach.
%    \begin{macrocode}
\long\def\mermin@xr@test#1#2#3#4\XR@{%
   % xr-hyper's \protected@xdef expands the titles stored in the other
   % file here, in the preamble, before the short names exist; a title
   % may hold \eq.  (\aka never reaches the file: nameref removes it.)
  \ifdefined\eq\else\let\eq\eqcite\fi
  \ifx#1\mermin@signature
    \def\mermin@xr@signed{}%
  \else\ifx#1\mermin@equation
    \mermin@xr@import{#2}%
  \else\ifx#1\mermin@newphrase
    \@xp\gdef\csname mermin@ph@\XR@prefix#2\endcsname{#3}%
  \else\ifx#1\newlabel
     % An unsigned file, from a document without this package, has no
     % marks: treat all its labels as equations rather than warn about
     % each.
    \ifdefined\mermin@xr@signed\else\mermin@xr@import{#2}\fi
  \fi\fi\fi\fi
  \mermin@xr@orig#1{#2}{#3}#4\XR@}
\def\mermin@xr@import#1{%
  \@xp\gdef\csname mermin@eq@\XR@prefix#1\endcsname{}%
  \mermin@ifeqprefix{#1}%
    {\@xp\mermin@xr@eqkey\@xp{\@gobblethree#1}}%
    {\@xp\xdef\csname mermin@xrdef@\XR@prefix#1\endcsname
       {\unexpanded{#1}}}}
 % For a key eq:<rest>: the text is <rest>, and
 % \mermin@xreq@<prefix><rest> takes the resolver's continuation and
 % passes it <prefix>eq:<rest>.
\def\mermin@xr@eqkey#1{%
  \@xp\xdef\csname mermin@xrdef@\XR@prefix eq:#1\endcsname
    {\unexpanded{#1}}%
  \@xp\xdef\csname mermin@xreq@\XR@prefix#1\endcsname##1{%
    ##1{\XR@prefix\unexpanded{eq:#1}}}}
\def\mermin@xr@patch{%
   % Wrap whatever \XR@test is now, unless it is already wrapped: xr
   % and then xr-hyper each define their own.
  \ifx\XR@test\mermin@xr@test\else
    \let\mermin@xr@orig\XR@test
    \let\XR@test\mermin@xr@test
  \fi}
\AddToHook{package/xr/after}{\mermin@xr@patch}
\AddToHook{package/xr-hyper/after}{\mermin@xr@patch}
\@ifpackageloaded{xr}{\mermin@xr@patch}{}
\@ifpackageloaded{xr-hyper}{\mermin@xr@patch}{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Citing with \cs{eqcite}}\label{impl:cite}
%
% \begin{macro}{\mermin@resolve}
% |\mermin@resolve{|\meta{argument}|}{|\meta{continuation}|}| finds the key
% \meta{argument} cites: the argument itself if it is a label, else
% |eq:|\meta{argument}, else the |eq:| key of another document that
% \textsf{xr} imported, else the argument, which is then undefined.
% It expands to \meta{continuation}|{|\meta{key}|}|.
% It is expandable, so that the same code serves bookmarks, and passes the
% key as an argument rather than storing it, so that nothing depends on a
% macro that a group might end: \textsf{ulem} typesets each word in a group
% of its own.
% |\@ifundefined| treats a label made |\relax| by an earlier failed
% \cs{ref} as undefined.
%
% One case comes first: when the argument labels something that is not an
% equation, a section say, and |eq:|\meta{argument} labels an equation,
% \cs{eqcite} cites the equation.
% |\mermin@ifpreferprefix| tests for it, and only in a document whose
% \texttt{.aux} file is signed, since only then are the equations known.
%    \begin{macrocode}
\def\mermin@ifpreferprefix#1{%
   % True if #1 labels a non-equation and an eq: equation exists: here
   % (both its label and its mark), or in a document read through xr.
   % Only in a signed document, where non-equations are known.
  \ifdefined\mermin@signed\@xp\@firstoftwo\else\@xp\@secondoftwo\fi
    {\@ifundefined{r@#1}{\@secondoftwo}%
       {\@ifundefined{mermin@eq@#1}%
          {\@ifundefined{mermin@xreq@#1}%
             {\@ifundefined{r@eq:#1}{\@secondoftwo}%
                {\@ifundefined{mermin@eq@eq:#1}%
                   {\@secondoftwo}{\@firstoftwo}}}%
             {\@firstoftwo}}%
          {\@secondoftwo}}}%
    {\@secondoftwo}}
\def\mermin@resolve#1#2{%
  \mermin@ifpreferprefix{#1}%
    {\@ifundefined{r@eq:#1}%
       {\csname mermin@xreq@#1\endcsname{#2}}% through xr
       {#2{eq:#1}}}%
    {\mermin@resolve@{#1}{#2}}}
\def\mermin@resolve@#1#2{%
  \@ifundefined{r@#1}%
    {\@ifundefined{r@eq:#1}%
       {\@ifundefined{mermin@xreq@#1}%
          {#2{#1}}%
          {\csname mermin@xreq@#1\endcsname{#2}}}%
       {#2{eq:#1}}}%
    {#2{#1}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\eqcite}
% \begin{macro}{\mermin@cite}
% \begin{macro}{\mermin@check@ambiguous}
% \cs{eqcite} resolves its argument.
% |\mermin@cite| receives the argument and the key.
% It warns if the key is a label but not an equation, and, through
% |\mermin@check@ambiguous|, if the argument and |eq:|\meta{argument} are
% both labels of different equations, as when a revision adds
% |\aka{mass}| to one equation while an older |\eq{mass}| meant
% |\label{eq:mass}| on another.
% It then prints the phrase, a tie, and the number from \cs{eqref}, which
% \textsf{hyperref} makes a link.
% In math mode the whole citation is set as text.
%    \begin{macrocode}
\NewDocumentCommand\eqcite{m}{%
   % Alignment guard, as in \mermin@markargs: \eq{A & B} in a cell of
   % align or tabular builds csnames from the key.
  \iffalse{\fi
  \mermin@resolve{#1}{\mermin@cite{#1}}%
  \iffalse}\fi}
\def\mermin@cite#1#2{%
  \mermin@ifpreferprefix{#1}{}{\mermin@check@ambiguous{#1}}%
  \@ifundefined{r@#2}{}{%
    \ifdefined\mermin@signed
      \@ifundefined{mermin@eq@#2}%
        {\mermin@warn{%
           `#1' is not an equation\MessageBreak
           label, yet \mermin@eqcs\space prints a parenthesized%
           \MessageBreak
           equation number; for a figure, section, or\MessageBreak
           theorem, use \string\ref\space or \string\cref}}%
        {}%
    \fi}%
  \ifmmode\@xp\text\else\@xp\@firstofone\fi
    {\mermin@phrase{#1}{#2}~\mermin@keepflag{\mermin@eqref{#2}}}}
\def\mermin@check@ambiguous#1{%
  \@ifundefined{r@#1}{}{\@ifundefined{r@eq:#1}{}{%
     % \ifx compares the two labels' number and page: keys of one line
     % agree.
    \@xp\ifx\csname r@#1\@xp\endcsname\csname r@eq:#1\endcsname
    \else
      \mermin@warn{%
        `#1' and `eq:#1' label different\MessageBreak
        equations;\MessageBreak
        \mermin@eqcs\space cites `#1'}%
    \fi}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mermin@phrase}
% \begin{macro}{\mermin@warn@unprintable}
% The phrase is the typeset phrase recorded for the key, or else the
% key's text: for a key imported through \textsf{xr}, the key without the
% prefix, and otherwise the argument without a leading |eq:|.
% The key's text is printed verbatim if it is not printable text.
% Once the label exists, a verbatim key also warns, saying where to give
% it a phrase; before the first run defines it, every key is quietly
% verbatim if need be, because its phrase may simply not be known yet.
% |\mermin@keytext{|\meta{argument}|}{|\meta{key}|}{|\meta{continuation}|}|
% chooses the key's text and passes it to \meta{continuation} as an
% argument, unexpanded; bookmarks use it too.
% \cs{eqcite}'s continuation is |\def\mermin@text|: expanding the text
% instead would fail on accent commands such as |\"| in a key, and would
% expand macros that should print as written.
%    \begin{macrocode}
\def\mermin@keytext#1#2#3{%
  \@ifundefined{mermin@xrdef@#2}%
    {\mermin@ifeqprefix{#1}%
       {\@xp\mermin@apply\@xp{\@gobblethree#1}{#3}}%
       {#3{#1}}}%
     % Expand the \csname, then the macro it names, once each.
    {\@xp\@xp\@xp\mermin@apply
       \@xp\@xp\@xp{\csname mermin@xrdef@#2\endcsname}{#3}}}
\def\mermin@apply#1#2{#2{#1}}
\def\mermin@phrase#1#2{%
  \@ifundefined{mermin@ph@#2}%
    {\mermin@keytext{#1}{#2}{\def\mermin@text}%
     \@xp\mermin@ifunprintable\@xp{\mermin@text}%
       {\@ifundefined{r@#2}{}%
          {\@xp\mermin@warn@unprintable\@xp{\mermin@text}{#1}{#2}}%
        \@xp\mermin@verbatim\@xp{\mermin@text}}%
       {\mermin@text}}%
    {\csname mermin@ph@#2\endcsname}}
 % #1 is the text printed, #2 the argument, #3 the key.
\def\mermin@warn@unprintable#1#2#3{%
  \@ifundefined{mermin@xrdef@#3}%
    {\mermin@warn{`#2' has no typeset phrase\MessageBreak
       and is not text, so it is printed verbatim;\MessageBreak
       where it is defined, write\MessageBreak
       \mermin@akacs[<phrase>]{#3}}}%
    {\mermin@warn{`#2' has no typeset phrase\MessageBreak
       and is not text, so it is printed verbatim;\MessageBreak
       give it a phrase in the document\MessageBreak
       that defines `#1'}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Headings, bookmarks, and running heads}\label{impl:heads}
%
% \begin{macro}{\mermin@pdf@eq}
% \textsf{hyperref}'s bookmarks need an expandable \cs{eqcite}, which
% gives the phrase or key text and the number as plain text.
% It uses the same resolver, and the same choice of text as
% |\mermin@phrase|, but expandably and without the verbatim form.
%    \begin{macrocode}
\def\mermin@pdf@eq#1{\mermin@resolve{#1}{\mermin@pdf@cite{#1}}}
\def\mermin@pdf@cite#1#2{%
  \@ifundefined{mermin@ph@#2}%
    {\mermin@keytext{#1}{#2}\@firstofone}%
    {\csname mermin@ph@#2\endcsname}%
  \space(\ref{#2})}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mermin@stripaka}
% \textsf{nameref} takes the name of a section or a caption from its
% title, through \textsf{gettitlestring}.
% That package's default method, which does not expand the title, removes
% \cs{label} from it but knows nothing of \cs{alsolabel}, which would stay
% in the name and define its key again wherever the name is typeset.
% The package wraps the method to remove \cs{alsolabel} too, with its
% arguments: spaces around it go with it at the end of the title, but in
% the middle they stay, so that |Chiral \aka{chiral}fermions| keeps its
% space.
% \cs{aka} is removed only when it is this package's.
% (The expanding method needs nothing: \cs{alsolabel} expands to nothing
% there, since that method disables \cs{label}.)
%    \begin{macrocode}
\ExplSyntaxOn
 % The pieces of one command: its name, an optional <overlay>, an
 % optional [phrase], and the {key}.  A phrase or key may hold brace
 % groups one level deep, as in [\emph{x}] or {a{b}}; a lazy .*? would
 % instead run on to the title's last closing brace.
\tl_const:Nn \c__mermin_overlay_tl { (?: \< \c[^BE][^\>]* \> )? }
\tl_const:Nn \c__mermin_phrase_tl
  { (?: \[ (?: \c[^BE][^\]] | \cB. \c[^BE]* \cE. )* \] )? }
\tl_const:Nn \c__mermin_key_tl
  { \cB. (?: \c[^BE]. | \cB. \c[^BE]* \cE. )* \cE. }
\cs_new:Npn \__mermin_aka_one:n #1
  {
    \exp_not:n { \c{(?:#1)} }
    \exp_not:V \c__mermin_overlay_tl
    \exp_not:V \c__mermin_phrase_tl
    \exp_not:V \c__mermin_key_tl
  }
 % A run of the commands at the end of the title goes with the spaces
 % around it; one anywhere else goes alone, and its spaces stay.
\cs_new:Npn \__mermin_aka_pattern:n #1
  {
    \exp_not:n { \s* (?: } \__mermin_aka_one:n {#1}
    \exp_not:n { \s* )+ \Z | } \__mermin_aka_one:n {#1}
  }
\exp_args:NNx \regex_const:Nn \c__mermin_aka_regex
  { \__mermin_aka_pattern:n { aka|alsolabel|mermin@alsolabel } }
\exp_args:NNx \regex_const:Nn \c__mermin_long_regex
  { \__mermin_aka_pattern:n { alsolabel|mermin@alsolabel } }
\cs_new_protected:Npn \mermin@stripaka
  {
    \ifx \aka \alsolabel
      \regex_replace_all:NnN \c__mermin_aka_regex { }
        \GetTitleStringResult
    \else
      \regex_replace_all:NnN \c__mermin_long_regex { }
        \GetTitleStringResult
    \fi
  }
\ExplSyntaxOff
\AddToHook{begindocument/end}{%
   % nameref, through gettitlestring, takes section and caption names
   % from their titles; this is its default, non-expanding method.
  \ifdefined\GetTitleStringNonExpand
    \let\mermin@gts@orig\GetTitleStringNonExpand
    \def\GetTitleStringNonExpand#1{%
      \mermin@gts@orig{#1}\mermin@stripaka}%
  \fi}
%    \end{macrocode}
% \end{macro}
%
% In text that \cs{MakeUppercase} changes, such as running heads, the
% argument of \cs{eqcite} is a key and must not change; nor may the key of
% |\mermin@alsolabel|.
% The \LaTeX\ kernel from 2022-06 keeps the argument of every command in
% |\l_text_case_exclude_arg_tl| as written.
%    \begin{macrocode}
 % l_text_case_exclude_arg_tl exists in LaTeX 2022-06 and later.
\AddToHook{begindocument/end}{%
  \@ifundefined{l_text_case_exclude_arg_tl}{}{%
    \@xp\g@addto@macro\csname l_text_case_exclude_arg_tl\endcsname
      {\eqcite\mermin@alsolabel}%
    \ifx\eq\eqcite
      \@xp\g@addto@macro\csname l_text_case_exclude_arg_tl\endcsname
        {\eq}%
    \fi}}
%    \end{macrocode}
%
% \subsection{The short names}\label{impl:short}
%
% \begin{macro}{\eq}
% \begin{macro}{\aka}
% The short names \cs{eq} and \cs{aka} may already be taken, \cs{eq} for
% instance by \textsf{nccmath}.
% Each is therefore assigned only at |\begin{document}|, after every
% package and the preamble have had the chance to define it, and only if
% it is still free; otherwise the existing command is kept and the user is
% told to write the long name.
% Each short name is a copy of the long one made with \cs{let}, so it
% behaves like it in moving arguments, and |\ifx\aka\alsolabel| tells
% whether \cs{aka} is this package's.
% The warnings then name the commands the document can use.
% The bookmark versions of the commands are installed at the same time.
%    \begin{macrocode}
\def\mermin@akacs{\string\alsolabel}
\def\mermin@eqcs{\string\eqcite}
\def\mermin@shortname#1#2{%
  \@ifundefined{#1}%
    {\@xp\let\csname #1\endcsname#2}%
    {\mermin@warn@noline{\@backslashchar#1
      was already defined, by a package or\MessageBreak
      the preamble, and is left alone;\MessageBreak
      write \string#2\space instead}}}
 % env/document/begin, not begindocument/before or later: beamer builds
 % the PDF title, which may hold \eq, in this same hook (ordered after
 % this code by the rule below), and its themes typeset section titles
 % in \AtBeginDocument code.
\AddToHook{env/document/begin}[mermin-equations]{%
  \mermin@shortname{eq}\eqcite
  \mermin@shortname{aka}\alsolabel
  \ifx\aka\alsolabel\def\mermin@akacs{\string\aka}\fi
  \ifx\eq\eqcite\def\mermin@eqcs{\string\eq}\fi
  \ifdefined\pdfstringdefDisableCommands
    \pdfstringdefDisableCommands{%
       % Test the short names before the long ones change.
      \ifx\aka\alsolabel\let\aka\mermin@gobble@aka\fi
      \ifx\eq\eqcite\let\eq\mermin@pdf@eq\fi
      \let\alsolabel\mermin@gobble@aka
      \let\eqcite\mermin@pdf@eq
       % latexdiff's markers, which this package makes robust.
      \let\DIFdelbegin\@empty
      \let\DIFdelend\@empty
       % Neither of Mermin's rules is for bookmarks.
      \let\eqref\mermin@eqref
      \ifdefined\mermin@maketag\let\maketag@@@\mermin@maketag\fi}%
  \fi}
\DeclareHookRule{env/document/begin}
  {mermin-equations}{before}{beamer}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Mermin's rules as warnings}\label{impl:rules}
%
% \begin{macro}{\ifmermin@numbered}
% \begin{macro}{\mermin@fisher@install}
% Fisher's rule: number every displayed equation.
% A display counts as numbered when it typesets at least one number,
% automatic or given by \cs{tag}; a derivation whose intermediate lines
% are \cs{notag} is one equation, and is numbered.
% Every number that \textsf{amsmath} typesets passes through
% |\maketag@@@|, and every one the kernel's \textsf{eqnarray} typesets
% through |\@eqnnum|; both raise a flag, which each display lowers when it
% begins and reads when it has ended.
% The flag is global, because the number is typeset inside the cells of
% an alignment, which are groups.
% A citation by \cs{eqref} inside a display typesets a number through
% |\maketag@@@| too, so the citation commands keep the flag as they found
% it, with |\mermin@keepflag|.
% \textsf{empheq} typesets its inner display twice, the second time
% without |\maketag@@@|, so it is checked as a whole instead.
% A display that \textsf{latexdiff} marks as deleted is not checked, and
% on \textsf{beamer}'s later overlay slides of a frame neither rule warns
% again.
% With the \textsf{mathtools} option |showonlyrefs| a display is numbered
% only if it is cited, the practice Mermin calls Occam's rule; then one
% warning says so, and displays are not checked one by one.
%    \begin{macrocode}
\newif\ifmermin@numbered
\def\mermin@fisher@begin{\global\mermin@numberedfalse}
\def\mermin@fisher@after#1{%
  \ifmermin@numbered\else\ifmermin@deleted\else
  \ifnum\mermin@slide>\@ne\else
    \mermin@warn{%
      `#1' display without a number.\MessageBreak
      Fisher's rule: number every display\MessageBreak
      (option nofisher)}%
  \fi\fi\fi}
 % beamer typesets a frame once per overlay slide; warn on the first.
 % A number, tested with a real \ifnum: a macro expanding to \ifnum
 % would not be counted as a conditional when TeX skips a branch.
\def\mermin@slide{%
  \ifdefined\beamer@slideinframe\beamer@slideinframe\else1\fi}
\def\mermin@fisherenv#1{%
  \AddToHook{env/#1/begin}{\mermin@fisher@begin}%
  \AddToHook{env/#1/after}{\mermin@fisher@after{#1}}}
 % A macro of its own: written inline, a false \ifmermin@fisher would
 % take the showonlyrefs test's \else and \fi as its own.
\def\mermin@fisher@install{%
     % mathtools' showonlyrefs: \MH_if_boolean_show_only_refs: is
     % \iftrue or \iffalse.
    \@ifundefined{MH_if_boolean_show_only_refs:}{\@secondoftwo}%
      {\csname MH_if_boolean_show_only_refs:\endcsname
         \@xp\@firstoftwo\else\@xp\@secondoftwo\fi}%
      {\mermin@warn@noline{%
         mathtools' showonlyrefs numbers only the\MessageBreak
         equations that are cited, which Mermin calls\MessageBreak
         Occam's rule; Fisher's rule numbers them all\MessageBreak
         (option nofisher silences this)}}%
      {\let\mermin@maketag\maketag@@@
       \def\maketag@@@{\global\mermin@numberedtrue\mermin@maketag}%
       \let\mermin@eqnnum\@eqnnum
       \def\@eqnnum{\global\mermin@numberedtrue\mermin@eqnnum}%
        % \[ is equation* under amsmath, and so is displaymath, which
        % would otherwise be checked twice.
       \@for\mermin@env:=%
           equation,equation*,align,align*,gather,gather*,%
           multline,multline*,flalign,flalign*,alignat,alignat*,%
           eqnarray,eqnarray*%
         \do{\@xp\mermin@fisherenv\@xp{\mermin@env}}%
        % empheq typesets its inner environment twice, the second time
        % with \maketag@@@ replaced by a \vphantom: check empheq as a
        % whole, and silence the inner environment's own check.
       \AddToHook{env/empheq/begin}{%
         \mermin@fisher@begin
         \let\mermin@fisher@begin\relax
         \let\mermin@fisher@after\@gobble}%
       \AddToHook{env/empheq/after}{\mermin@fisher@after{empheq}}}}
\AddToHook{begindocument/end}{%
  \ifmermin@fisher\@xp\mermin@fisher@install\fi}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\mermin@eqref}
% \begin{macro}{\mermin@samaritan}
% The Good Samaritan rule: when referring to an equation, identify it by a
% phrase as well as a number.
% The warning is given once per citation: not again in \textsf{amsmath}'s
% measuring pass, in \textsf{caption}'s trial typesetting, on a later
% \textsf{beamer} slide, or when the table of contents or list of figures
% is read back.
% \cs{eqref}, and \textsf{cleveref}'s \cs{cref} and \cs{Cref}, warn when
% they cite an equation by number alone; \cs{eqcite} calls the original
% \cs{eqref}, saved as |\mermin@eqref|, and so never warns.
% Only equations are checked, and only in a signed document, where they
% are known.
% Plain \cs{ref} is not checked: other commands, \cs{eqref} among them,
% use it internally.
%    \begin{macrocode}
 % A first copy now, for beamer, which typesets its title, perhaps with
 % \eq, before begindocument/end; the copy is refreshed there.
\NewCommandCopy\mermin@eqref\eqref
 % An \eqref in a display typesets its number through \maketag@@@ too;
 % \mermin@keepflag runs #1 without letting it count as the display's
 % own number.
\def\mermin@keepflag#1{%
  \ifmermin@numbered\@xp\@firstofone\else\@xp\mermin@keepflag@\fi{#1}}
\def\mermin@keepflag@#1{#1\global\mermin@numberedfalse}
\def\mermin@samaritan#1{%
   % Warn once: not in amsmath's measuring pass, nor caption's
   % single-line check (\label is \caption@gobble there), nor on a later
   % beamer slide; the table of contents is handled below.
  \ifx\label\caption@gobble\else\ifmeasuring@\else
  \ifnum\mermin@slide>\@ne\else
  \ifdefined\mermin@signed
    \@ifundefined{r@#1}{}{\@ifundefined{mermin@eq@#1}{}{%
      \mermin@warn{%
        `#1' is cited by number alone.\MessageBreak
        Good Samaritan rule: name it, as \mermin@eqcs\space does%
        \MessageBreak
        (option nosamaritan)}}}%
  \fi\fi\fi\fi}
\AddToHook{begindocument/end}{%
   % A command copy, not \let, since \eqref is robust.  mathtools'
   % showonlyrefs, or the document, may have redefined \eqref by now;
   % the copy keeps their version.
  \DeclareCommandCopy\mermin@eqref\eqref
  \ifmermin@samaritan
     % \protected, not \DeclareRobustCommand: that would redefine the
     % inner command `\eqref ', which a \let copy of \eqref made earlier
     % (\let\oldeqref\eqref, or refcheck's) still calls, and the two
     % would call each other forever.  Bookmarks use the original.
    \protected\def\eqref#1{%
      \mermin@samaritan{#1}\mermin@keepflag{\mermin@eqref{#1}}}%
     % cleveref's \cref, \Cref, and \labelcref, starred or not, pass
     % their list of keys to \@cref, with their own name as #1; so do
     % its page references (\cpageref, and varioref's \vref), which
     % cite a page, not an equation, and are not checked.
    \ifdefined\@cref
      \let\mermin@cref\@cref
      \def\@cref#1#2{%
        \in@{,#1,}{,cref,Cref,labelcref,}%
        \ifin@
          \@for\mermin@k:=#2\do{\@xp\mermin@samaritan\@xp{\mermin@k}}%
        \fi
        \mermin@cref{#1}{#2}}%
    \fi
     % Reading back the table of contents or list of figures would warn
     % again for each citation in a title or caption.
    \let\mermin@starttoc\@starttoc
    \def\@starttoc#1{%
      {\let\mermin@samaritan\@gobble\mermin@starttoc{#1}}}%
  \fi}
%</package>
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \Finale
\endinput
