Open gabor-braun opened 5 years ago
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been automatically marked as stale because it has not had recent activity.
This issue has been automatically marked as stale because it has not had recent activity.
(Thoughts welcome.)
When amsmath is loaded, the showkeys package patches it in two places to insert the equation label: \maketag@@@
and \tagform@
, which are called by amsmath's \tag*
and \tag
respectively (and \thetag
and automatically to label equations). Since \tagform@
itself calls \maketag@@@
, the showkeys patch is run twice. Without the leqno
option, the code is schematically
\def\tagform@#1{%
Define \SK@lab
\SK@tagform@{#1}% (amsmath's \tagform@ which calls \maketag@@@)
Typeset \SK@lab and empty it}
\def\maketag@@@#1{%
Define \SK@lab
\SK@maketag@@@{#1}% (amsmath's \maketag@@@)
Typeset \SK@lab and empty it}
This means that \SK@lab
is defined twice, then typeset (after amsmath's \maketag@@@
) and emptied, then an empty box is typeset the second time. With the leqno
option, the order is a bit different so \SK@lab
is defined and typeset and defined and typeset a second time.
\def\tagform@#1{%
Define \SK@lab
Typeset \SK@lab and empty it
\SK@tagform@{#1}} % calls \maketag@@@
\def\maketag@@@#1{%
Define \SK@lab
Typeset \SK@lab and empty it
\SK@maketag@@@{#1}}
Remove the patch on \tagform@
entirely.
I've checked redefinitions in TeXLive 2021 packages, and saw only one non-trivial place (most redefinitions of \tagform@
call \maketag@@@
, some set \tagform@
to \@gobble
). In amsmath.dtx
itself, when \tag*
is used inside an equation
(or equation*
) environment, see \tag@in@display
. Then two definitions occur: \alt@tag
is defined to change \tagform@
and \df@tag
is defined to call \maketag@@@
. Elsewhere, the code runs \alt@tag \df@tag
in this order: the first redefines \tagform@
to not call \maketag@@@
, but then the \df@tag
simply calls \maketag@@@
. In fact, I cannot see how \alt@tag
has any useful effect in this scenario.
When amsmath is loaded, the showkeys patch to \@eqnnum
(to support eqnarray
) does not end up showing any key because it looks for \df@label
, which seems like it is not set up by amsmath for this environment.
(Thoughts welcome.)
Origin of bug.
When amsmath is loaded, the showkeys package patches it in two places to insert the equation label:
\maketag@@@
and\tagform@
, which are called by amsmath's\tag*
and\tag
respectively (and\thetag
and automatically to label equations).
A more accurate and concise description is that these are called when typesetting equation tags, with \maketag@@@
typesetting the raw tag, and \tagform@ typesetting the tag in document convention (adding parentheses by default), internally using \maketag@@@
.
In short the informative part of equation tags are always typeset by \maketag@@@
, making it a suitable as the only place to add showing the label (except for references like \ref and friends of course):
Proposed solution
Remove the patch on
\tagform@
entirely.
For the proposed solution,
probably the following snippet in amsmath needs adjusting too,
as then redefining \SK@tagform@
would be a no-op
(inside \tag@in@aling@a
):
\gdef\alt@tag{\def\SK@tagform@{#2\@gobble}%
\ifx\SK@@label\relax \let\tagform@\SK@tagform@ \fi
}%
It should probably become what it would be without showkeys support in the first place (ignoring backwards compatibility):
\gdef\alt@tag{\def\tagform@{#2\@gobble}%
}%
(most redefinitions of
\tagform@
call\maketag@@@
, some set\tagform@
to\@gobble
).
These fit into the high-level picture above:
equation tags are always typeset by \maketag@@@
.
(The \@gobble
variants don't typeset anything.)
In
amsmath.dtx
itself, when\tag*
is used inside anequation
(orequation*
) environment, see\tag@in@display
. Then two definitions occur:\alt@tag
is defined to change\tagform@
and\df@tag
is defined to call\maketag@@@
. Elsewhere, the code runs\alt@tag \df@tag
in this order: the first redefines\tagform@
to not call\maketag@@@
, but then the\df@tag
simply calls\maketag@@@
. In fact, I cannot see how\alt@tag
has any useful effect in this scenario.
Even here \maketag@@@
is called to typeset the equation tag, so fits in the scheme above.
By the way, I am wondering why \tag@in@display
cannot be simplified to
\let\tag@in@display\tag@in@align
as the code of \tag@in@align
seems to be independent of the equation environment?
Then of course \tag@in@display@a
and \alt@tag
would no longer be needed
(the first one becomes unused, the latter one always empty).
(This would also make the above suggested change in \tag@in@aling@a
irrelevant.)
Minor unrelated bug?
When amsmath is loaded, the showkeys patch to
\@eqnnum
(to supporteqnarray
) does not end up showing any key because it looks for\df@label
, which seems like it is not set up by amsmath for this environment.
From showkeys code documentation:
When the AMS packages are loaded showkeys assumes environments work ‘The AMS way’ However eqnarray (unlike equation) is not redefined, so here we need to remove some of the AMS hacks.
\toks@\expandafter{\eqnarray}
\edef\eqnarray{\let\noexpand\tagform@\noexpand\SK@tagform@\the\toks@}
I.e. showkeys is trying to restore the old definition of \tagform@
,
whch fails but forgetting to restore \maketag@@@
, too. I.e,, it should do:
\toks@\expandafter{\eqnarray}
\edef\eqnarray{%
\let\noexpand\maketag@@@\noexpand\SK@maketag@@@
\let\noexpand\tagform@\noexpand\SK@tagform@\the\toks@}
(Obviously, if the above proposal is accepted, \tagform@
shouldn't be restored.)
This issue has been automatically marked as stale because it has not had recent activity.
For what it's worth, showlabels had the same issue (I wonder if this is because I stole the idea from @davidcarlisle (hello, David, fancy meeting you here!), or simply jumped to the same conclusion myself). I made the suggested change to showlabels (ie, redefining \maketag@@@
and leaving \tagform@
alone) and it does appear as if the duplication has disappeared.
I haven't torture-tested the fix, because looking through amsmath.sty
, I end up agreeing with @gabor-braun (to whom many thanks!) that monkeypatching \maketag@@@
should be sufficient here.
Incidentally, the bug is reproducible both with and without the [leqno]
option.
Brief outline of the bug
In the log file, the output of
\showlists
clearly shows that the label `eq:1' appears twice at exactly the same spot. In the PDF file this exact overprinting is present but not visible, so this is an efficiency problem.In particlar, the following snipper appears twice in the output of
\showlists
Minimal example showing the bug
Log file (required) and possibly PDF file
Discussion
Some macro definitions valid in the document:
It seems that \tagform@ prints the key twice: once directly via
\llap{...}
and once indirectly via the call chain\SK@tagform@
->\maketag@@@
->\llap
.Maybe
showkeys
shouldn't modify\tagform@
at all but only\maketag@@@
.