Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Referencing tagged equation by cleveref no longer works due to firstaid patch #1560

Open
zyoshoka opened this issue Nov 23, 2024 · 5 comments

Comments

@zyoshoka
Copy link

Brief outline of the bug

After updating firstaid to 1.1g, referencing tagged equation by cleveref does not work correctly.

Minimal example showing the bug

% \RequirePackage{latexbug}       % <--should be always the first line (see CONTRIBUTING)!
\documentclass{article}
\usepackage{amsmath,cleveref}
\begin{document}
\cref{eq:a}
\begin{equation}\tag{$*$}\label{eq:a}
  a
\end{equation}
\end{document}

Expected

It should print like eq. (*) on the first line.

Actual

It prints eq. (0) on the first line.

@u-fischer
Copy link
Member

In your example you should use the unnumbered equation* if you use a \tag. equation gives wrong links with hyperref. E.g in the following example the reference jumps to the first equation and not as wanted to the second:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\begin{document}
\ref{eq:b}
\begin{equation}\tag*{$*$}\label{eq:a}
  a
\end{equation}

\newpage 
\begin{equation}\label{eq:b}
  b
\end{equation}
\end{document}

With the starred version both the links and also cleveref works fine:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{cleveref}
\begin{document}
\cref{eq:a}, \cref{eq:b}
\begin{equation*}\tag*{$*$}\label{eq:a}
  a
\end{equation*}

\newpage 
\begin{equation}\label{eq:b}
  b
\end{equation}

\end{document}

image

@zyoshoka
Copy link
Author

Your example works correctly, but is not a complete workaround since referencing tagged equation* inside referenceable environments doesn't work. For example, the following example gives 1. item 1 instead of 1. eq. (*):

\documentclass{article}
\usepackage{amsmath,cleveref}
\begin{document}
\begin{enumerate}
  \item \cref{eq:a}
  \begin{equation*}\tag{$*$}\label{eq:a}
    a
  \end{equation*}
\end{enumerate}
\end{document}

@jfbu
Copy link

jfbu commented Nov 24, 2024

@u-fischer sorry to drop-in, but I got a revelation when reading and then testing your advice about not using \tag within equation but only equation*. The amsmath documentation says

You can also override a number with a tag of your own using \tag{⟨label⟩}, where ⟨label⟩ means arbitrary text such as $*$ or ii used to “number” the equation. [...]. If you are using hyperref,

As I read this decades ago (in amsldoc.pdf) and it still says "override a number", and as the paragraph mentions hyperref without discussing the issue, I may have been doing bad practice for ages without realizing it. I confirmed I used \tag in equation as recent as one week ago, but in a document not using hyperref. With hyperref I see that pdftex raises warnings, but such warning not originating in LaTeX are not reported by my editor (Emacs AUCTeX). It could be that I never realized that hyperlinks were wrong in some documents.

Could an update to amsmath documentation explain the good practice?

@u-fischer
Copy link
Member

u-fischer commented Nov 24, 2024

@zyoshoka ok I think testing for \iftag@ in the hook should work, at least I get for all the variants here the same output as in tl2022:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{cleveref}
\makeatletter
\RemoveFromHook{label}[firstaid/cleveref]
\ExplSyntaxOn
 \newif\iftag@ \tag@false %if amsmath is not loaded
 \AddToHook{label}[firstaid/cleveref]
         {%
          \ifx
           \@currentcounter\@empty
          \else
          \iftag@\else
           \firstaid@cref@updatelabeldata{\@currentcounter}%
          \fi 
          \fi}
\ExplSyntaxOff          
\makeatother
\begin{document}
\cref{sec},\\
\cref{a1},\\
\cref{a2},\\
\cref{b},\\
\cref{c},\\
\cref{d},\\
\cref{e},\\
\cref{f},\\
\cref{g},\\
\cref{h}
\section{section}\label{sec}
\begin{align}
a& b\tag{align}\label{a1}\\
a& b \label{a2}
\end{align}
\begin{equation}
a= b\label{b}
\end{equation}
\begin{equation*}
a= b\label{c}
\end{equation*}

\begin{equation}
a= b\tag{tageq}\label{d}
\end{equation}

\begin{equation*}
a= b\tag{tageq*}\label{e}
\end{equation*}

\[
a= b\label{f}
\]

\[
a= b\tag{tagX}\label{g}
\]

\begin{enumerate}
  \item item
   \begin{equation*}
    a= b \tag{inenum}\label{h}
  \end{equation*}
\end{enumerate}
\end{document}

image

u-fischer added a commit that referenced this issue Nov 24, 2024
josephwright pushed a commit that referenced this issue Nov 25, 2024
* add cleveref firstaid for issue #1560

* update hook tests
josephwright added a commit that referenced this issue Nov 25, 2024
* add cleveref firstaid for issue #1560

* update hook tests
@FrankMittelbach
Copy link
Member

on its way to CTAN and from there in 1-2 days to the distributions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants