I am trying to creatate circular hyperlinks between endnotes marks and their respective content. I ended up finding this answer that basicaly does what I want. However, in my project the endnote counter is reseted each chapter, what makes the code to fail.
I modified the original code a little trying to fix it. I created a new counter glenote that is never reseted. I am triying to use it to create unique labels and hyperrefs for each endnote, but because some reason the links just work from the endnotes body to their marks. The straight path (from the endnote mark to its content) just works until I reset the endnote counter.
Below is the code with the modifications I made.
\begin{filecontents*}{hyperendnote.sty}
%%% hyperendnotes.sty
\makeatletter
\newif\ifenotelinks
\newcounter{Hendnote}
\newcounter{glenote}
% Redefining portions of endnotes-package:
\let\savedhref\href
\let\savedurl\url
\def\endnotemark{%
\@ifnextchar[\@xendnotemark{%
\stepcounter{endnote}%
\stepcounter{glenote}%
\protected@xdef\@theenmark{\theendnote}%
\protected@xdef\@theenvalue{\number\c@glenote}%
\@endnotemark
}%
}%
\def\@xendnotemark[#1]{%
\begingroup\c@glenote#1\relax
\unrestored@protected@xdef\@theenmark{\theendnote}%
\unrestored@protected@xdef\@theenvalue{\number\c@glenote}%
\endgroup
\@endnotemark
}%
\def\endnotetext{%
\@ifnextchar[\@xendnotenext{%
\protected@xdef\@theenmark{\theendnote}%
\protected@xdef\@theenvalue{\number\c@glenote}%
\@endnotetext
}%
}%
\def\@xendnotenext[#1]{%
\begingroup
\c@glenote=#1\relax
\unrestored@protected@xdef\@theenmark{\theendnote}%
\unrestored@protected@xdef\@theenvalue{\number\c@glenote}%
\endgroup
\@endnotetext
}%
\def\endnote{%
\@ifnextchar[\@xendnote{%
\stepcounter{endnote}%
\stepcounter{glenote}%
\protected@xdef\@theenmark{\theendnote}%
\protected@xdef\@theenvalue{\number\c@glenote}%
\@endnotemark\@endnotetext
}%
}%
\def\@xendnote[#1]{%
\begingroup
\c@glenote=#1\relax
\unrestored@protected@xdef\@theenmark{\theendnote}%
\unrestored@protected@xdef\@theenvalue{\number\c@glenote}%
\show\@theenvalue
\endgroup
\@endnotemark\@endnotetext
}%
\def\@endnotemark{%
\leavevmode
\ifhmode
\edef\@x@sf{\the\spacefactor}\nobreak
\fi
\ifenotelinks
\expandafter\@firstofone
\else
\expandafter\@gobble
\fi
{%
\Hy@raisedlink{%
\hyper@@anchor{Hendnotepage.\@theenvalue}{\empty}%
}%
}%
\hyper@linkstart{link}{Hendnote.\@theenvalue}%
\makeenmark
\hyper@linkend
\ifhmode
\spacefactor\@x@sf
\fi
\relax
}%
\long\def\@endnotetext#1{%
\if@enotesopen
\else
\@openenotes
\fi
\immediate\write\@enotes{%
\@doanenote{\@theenmark}{\@theenvalue}%
}%
\begingroup
\def\next{#1}%
\newlinechar='40
\immediate\write\@enotes{\meaning\next}%
\endgroup
\immediate\write\@enotes{%
\@endanenote
}%
}%
\def\theendnotes{%
\immediate\closeout\@enotes
\global\@enotesopenfalse
\begingroup
\makeatletter
\edef\@tempa{`\string>}%
\ifnum\catcode\@tempa=12
\let\@ResetGT\relax
\else
\edef\@ResetGT{\noexpand\catcode\@tempa=\the\catcode\@tempa}%
\@makeother\>%
\fi
\def\@doanenote##1##2##3>{%
\def\@theenmark{##1}%
\def\@theenvalue{##2}%
\par
\smallskip %<-small vertical gap between endnotes
\begingroup
\def\href{\expandafter\savedhref}%
\def\url{\expandafter\savedurl}%
\@ResetGT
\edef\@currentlabel{\csname p@glenote\endcsname\@theenvalue}%
\enoteformat
}%
\def\@endanenote{%
\par\endgroup
}%
% Redefine, how numbers are formatted in the endnotes-section:
\renewcommand*\@makeenmark{%
\hbox{\normalfont\@theenmark~}%
}%
% header of endnotes-section
\enoteheading
% font-size of endnotes
\enotesize
\input{\jobname.ent}%
\endgroup
}%
\def\enoteformat{%
\rightskip\z@
\leftskip1.8em
\parindent\z@
\leavevmode\llap{%
\setcounter{Hendnote}{\@theenvalue}%
\addtocounter{Hendnote}{-1}%
\refstepcounter{Hendnote}%
\ifenotelinks
\expandafter\@secondoftwo
\else
\expandafter\@firstoftwo
\fi
{\@firstofone}%
{\hyperlink{Hendnotepage.\@theenvalue}}%
{\makeenmark}%
}%
}%
% stop redefining portions of endnotes-package:
\makeatother
% Toggle switch in order to turn on/off back-links in the
% endnote-section:
\enotelinkstrue
%\enotelinksfalse
\end{filecontents*}
\documentclass{book}
\usepackage{ragged2e}
\usepackage[%
pdftex
]{hyperref}
\usepackage{endnotes}
%\makeatletter
%\newbox\endnotebox%
%\setbox\endnotebox=\hbox{11}%
%
%\newlength{\enotehangalign}%
%\setlength{\enotehangalign}{13\p@}%
%
%\AtBeginDocument{%
%\def\enoteformat{%
%\def@makeenmark{\hbox to \wd\endnotebox{\hss{\normalfont@theenmark}}}%
%\rightskip\z@ \leftskip\z@ \parindent=\z@\parskip\z@\def@textsuperscript{}\hangindent\enotehangalign%
%\leavevmode{\makeenmark\ignorespaces\enskip\ignorespaces}\RaggedRight}%
%\def\enoteheading{\section*{Notes}%
%}%
%\def\enotesize{\fontsize{8}{10}\selectfont}%
%}%
%@addtoreset{endnote}{chapter}%
%\makeatother
\usepackage{hyperendnote}
\begin{document}
\chapter{Test 1}
This is for test\endnote{Test}.
This is for test\endnote{Test 2}.
This is for test\endnote{Test 3}.
This is for test\endnote{Test 4}.
This is for test\endnote{Test 5}.
This is for test\endnote{Test 6}.
This is for test\endnote{Test 7 Test 7 Test 7 Test 7 Test 7 Test 7
Test 7 Test 7 Test 7 Test 7 Test 7 Test 7 Test 7 Test 7 Test 7 Test 7
Test 7 Test 7 Test 7 Test 7 Test 7 Test 7 Test 7 Test 7 Test 7
Test 7 Test 7 Test 7 Test 7}.
This is for test\endnote{Test 8}.
\setcounter{endnote}{0}%}
% from this line onwards the straight link
% between the endnote mark and its content sticks to the
% first endnote 8
This is for test\endnote{Test 2.1}.
This is for test\endnote{Test 2.2}.
This is for test\endnote{Test 2.3}.
This is for test\endnote{Test 2.4}.
This is for test\endnote{Test 2.5}.
This is for test\endnote{Test 2.6}.
This is for test\endnote{Test 2.7}.
This is for test\endnote{Test 2.8}.
This is for test\endnote{Test 2.9}.
\clearpage
\theendnotes
\end{document}
Any help is welcome.
the straight path (from the endnote mark to its content) just works until I reset the endnote counter.Whatstraight path? – daleif Jan 12 '21 at 13:29straight pathis a link from the note marker, in the body of the text, to the note content at the end of the document. It's the ref, not the back ref. – Gabriel Araújo Jan 12 '21 at 15:20endnotecounter, the markers of notes 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8 and 2.9 points toTest 8– Gabriel Araújo Jan 12 '21 at 15:23