hyperref
hyperref copied to clipboard
autoref sometimes fails, a fix
Hi
The following MWE shows that (TL2023) autoref sometimes fails when used with mathtools and the showonlyrefs
\documentclass{article}
\usepackage{mathtools}
\mathtoolsset{showonlyrefs}
\usepackage[colorlinks]{hyperref}
\begin{document}
\section{Test}
\label{sec:test}
\begin{equation}
\label{eq:mathtools-autoref-solved:1}
\int f dx =0
\end{equation}
\begin{equation}
\label{eq:mathtools-autoref-solved:2}
a=b
\end{equation}
\subsection{Equation}
\label{sec:equation}
See \autoref{eq:mathtools-autoref-solved:1} and \autoref{sec:test}
\subsection{Section}
\label{sec:section}
\autoref{sec:test}
\end{document}
a fix was proposed in fix
And consists in adding
\usepackage{xpatch}
\makeatletter
\xpretocmd\HyRef@autoref{\noeqref{#2}}{}{}% kleiner Hack
\makeatother
After \usepackage[colorlinks]{hyperref}
Could that this patch somehow be included in future releases of hyperref?
I will also open an issue on the mathtools github page.
Thanks
Uwe Brauer
Slightly improved the proposed fix.
(adding a link to corresponding issue in latex3/mathtools https://github.com/latex3/mathtools/issues/56)
\documentclass{article}
\usepackage{mathtools}
\mathtoolsset{showonlyrefs}
\usepackage[colorlinks]{hyperref}
\makeatletter
\AddToHook{package/mathtools/after}{%
\def\HyRef@autoref#1#2{%
\begingroup
\Hy@safe@activestrue
\@nameuse{MH_if_boolean:nT}{show_only_refs}{%
% excerpted from \noeqref in mathtools.sty
\edef\@tempa{#2}%
%% will be done in \HyRef@autosetref
% \@ifundefined{r@\@tempa}{%
% \protect\G@refundefinedtrue%
% \@latex@warning{Reference `\@tempa' on page \thepage \space
% undefined (\string\noeqref)}%
% }{}%
\if@filesw
\protected@write\@auxout{}{\string\MT@newlabel{\@tempa}}%
\fi
}%
\expandafter\HyRef@autosetref\csname r@#2\endcsname{#2}{#1}%
\endgroup
}
}
\makeatother
\begin{document}
\section{title}\label{sec:a}
\begin{gather}
\int f dx = 0\label{eq:1} \\
\int f_2 dx = 0\label{eq:2} \\
\int f_3 dx = 0 \label{eq:3}
\end{gather}
\begin{gather}
\int g dx \tag{tag1} \label{eq:t1} \\
\int g_2 dx \tag{tag2} \label{eq:t2} \\
\int g_3 dx \tag{tag3} \label{eq:t3}
\end{gather}
% the most recent label before using \label{eq:1} is referenced
See \autoref{eq:1} and \autoref{eq:t1}
% this makes equation labeled "eq:2" numbered
See \eqref{eq:2} and \eqref{eq:t2}
% equations labeled "eq:3" and "eq:t3" should have no numbers
\end{document}
| Before | After |
|---|---|
Slightly improved the proposed fix
Well I don't quite like the idea to refer to internals of other packages. The official, documented interface of mathtools to mark a label as "referenced" is \noeqref.
But the main questions are
- is it really good if
\autorefexecutes and writes the mathtools label always (\autoref{sec:a}would write it too). - What about other reference commands?
One the whole I think hyperref should only insert a hook with argument and leave it to the author or to mathtools to activate the mathtools support.
Slightly improved the proposed fix.
(adding a link to corresponding issue in latex3/mathtools latex3/mathtools#56)
\documentclass{article} \usepackage{mathtools} \mathtoolsset{showonlyrefs} \usepackage[colorlinks]{hyperref} \makeatletter \AddToHook{package/mathtools/after}{% \def\HyRef@autoref#1#2{% \begingroup \Hy@safe@activestrue \@nameuse{MH_if_boolean:nT}{show_only_refs}{% % excerpted from \noeqref in mathtools.sty \edef\@tempa{#2}% %% will be done in \HyRef@autosetref % \@ifundefined{r@\@tempa}{% % \protect\G@refundefinedtrue% % \@latex@warning{Reference `\@tempa' on page \thepage \space % undefined (\string\noeqref)}% % }{}% \if@filesw \protected@write\@auxout{}{\string\MT@newlabel{\@tempa}}% \fi }% \expandafter\HyRef@autosetref\csname r@#2\endcsname{#2}{#1}% \endgroup } } \makeatother \begin{document} \section{title}\label{sec:a} \begin{gather} \int f dx = 0\label{eq:1} \\ \int f_2 dx = 0\label{eq:2} \\ \int f_3 dx = 0 \label{eq:3} \end{gather} \begin{gather} \int g dx \tag{tag1} \label{eq:t1} \\ \int g_2 dx \tag{tag2} \label{eq:t2} \\ \int g_3 dx \tag{tag3} \label{eq:t3} \end{gather} % the most recent label before using \label{eq:1} is referenced See \autoref{eq:1} and \autoref{eq:t1} % this makes equation labeled "eq:2" numbered See \eqref{eq:2} and \eqref{eq:t2} % equations labeled "eq:3" and "eq:t3" should have no numbers \end{document}Before After
![]()
Hi, thanks one problem of your fix is: it will not work in older TL version, I am sure not in TL 2019, so maybe having a fix, that takes into account the TL version, or is this too much to ask?
Slightly improved the proposed fix
Well I don't quite like the idea to refer to internals of other packages. The official, documented interface of mathtools to mark a label as "referenced" is
\noeqref.But the main questions are
- is it really good if
\autorefexecutes and writes the mathtools label always (\autoref{sec:a}would write it too).- What about other reference commands?
One the whole I think hyperref should only insert a hook with argument and leave it to the author or to mathtools to activate the mathtools support.
if I understand you correctly, the fix I proposed (or any other for that matter) should better go into mathtools, since it is the 'culprit' and not hyerref?
Hi, thanks one problem of your fix is: it will not work in older TL version, I am sure not in TL 2019, so maybe having a fix, that takes into account the TL version, or is this too much to ask?
Whatever hyperref will do will only work in a current texlive. A current hyperref expect current LaTeX tools and commands to be present and you can't install it in an old texlive. If you want to benefit from improvements, keep your system up-to-date.
if I understand you correctly, the fix I proposed (or any other for that matter) should better go into mathtools, since it is the 'culprit' and not hyerref?
There is no culprit. mathtools needs an information and the main question is how to provide. Btw: you could simply define a new command and use that wenn referencing an equation:
\newcommand\autoeqref[1]{\noeqref{#1}\autoref{#2}}
Hi, thanks one problem of your fix is: it will not work in older TL version, I am sure not in TL 2019, so maybe having a fix, that takes into account the TL version, or is this too much to ask?
Whatever hyperref will do will only work in a current texlive. A current hyperref expect current LaTeX tools and commands to be present and you can't install it in an old texlive. If you want to benefit from improvements, keep your system up-to-date.
if I understand you correctly, the fix I proposed (or any other for that matter) should better go into mathtools, since it is the 'culprit' and not hyerref?
There is no culprit. mathtools needs an information and the main question is how to provide. Btw: you could simply define a new command and use that wenn referencing an equation:
\newcommand\autoeqref[1]{\noeqref{#1}\autoref{#2}}
well, yes, but the problem is, I am using Emacs (AuCTex+reftex) so in order to include a new references into that I need to write an additional elisp style, which is not difficult, but I usually try to avoid to keep things simple
