# Hyperlinks to endnotes from enotez have unecxpected targets

When using enotez, one can assign labels to an endnote and refer to them. However, I have found that hyperref links to unexpected targets.

\documentclass{article}
\usepackage{expl3}
\usepackage[backref]{enotez}
\usepackage[hyperfootnotes=true]{hyperref}

\usepackage{cleveref}
\crefname{endnote}{endnote}{endnotes}

% This code by Andrew Swann (https://tex.stackexchange.com/questions/395662)
% makes \cref recognize counter endnote
\ExplSyntaxOn
\cs_set_protected:Npn \enotez_endnote_mark:n #1
{
\int_gincr:N \g__enotez_endnote_id_int
\quark_if_no_value:nTF {#1}
{
\refstepcounter {endnote}
\enotez_write_mark:xn
{ \int_use:N \g__enotez_endnote_id_int }
{ \theendnote }
}
{
\cs_gset:cpn {@currentlabel} {#1}
\enotez_write_mark:xn { \int_use:N \g__enotez_endnote_id_int } {#1}
}
\bool_if:NT \l__enotez_disable_bool
{
\int_gdecr:N \g__enotez_endnote_id_int
}
}
\ExplSyntaxOff

\begin{document}

This is an endnote with a labeldefinition inside the note.\endnote{Label in endnote\label{labelin}}
This endnote refers to the one with the labeldefinition inside.\endnote{See \cref{labelin}}

This is an endnote with a label defined after the note.\endnote{Label after endnote}\label{labelafter}
This endnote refers to the one with a following labeldefinition.\endnote{See \cref{labelafter}}
\newpage

\printendnotes
\end{document}


As you can see in the picture, the link from endnote 2 to endnote 1 is obviously wrong. It seems that if the label is placed inside of \endnote{}, it will always point to the last endnote.

The link in endnote 4 seems to be correct on first sight. However, I expected the link would point directly to endnote 3 (on the second page, under "Notes"). Unfortunately, it points back to page 1, where the endnote has been set.

Is there any way I could link to the actual text of an endnote instead of the endnote definition?

• A look into the relevant .aux file after the compilation shows that the labels defined at the end appear first in the .aux file, so this is main culprit – user31729 Oct 12 '17 at 20:00
• @ChristianHupfer So there is nothing I could do about it (except filing a bug against enotez) -- right? – Andreas Oct 13 '17 at 12:03
• In a nutshell: Yes ;-) It depends whether it is a feature or a bug – user31729 Oct 13 '17 at 22:05