TransWikia.com

Link does not work properly with endnotes and hyperref when endnote counter is reset

TeX - LaTeX Asked by Gabriel Araújo on March 11, 2021

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
newififenotelinks
newcounter{Hendnote}
newcounter{glenote}
% Redefining portions of endnotes-package:
letsavedhrefhref
letsavedurlurl
defendnotemark{%
@ifnextchar[@xendnotemark{%
stepcounter{endnote}%
stepcounter{glenote}%
protected@xdef@theenmark{theendnote}%
protected@xdef@theenvalue{numberc@glenote}%
@endnotemark
}%
}%
def@xendnotemark[#1]{%
begingroupc@glenote#1relax
unrestored@protected@xdef@theenmark{theendnote}%
unrestored@protected@xdef@theenvalue{numberc@glenote}%
endgroup
@endnotemark
}%
defendnotetext{%
@ifnextchar[@xendnotenext{%
protected@xdef@theenmark{theendnote}%
protected@xdef@theenvalue{numberc@glenote}%
@endnotetext
}%
}%
def@xendnotenext[#1]{%
begingroup
c@glenote=#1relax
unrestored@protected@xdef@theenmark{theendnote}%
unrestored@protected@xdef@theenvalue{numberc@glenote}%
endgroup
@endnotetext
}%
defendnote{%
@ifnextchar[@xendnote{%
stepcounter{endnote}%
stepcounter{glenote}%
protected@xdef@theenmark{theendnote}%
protected@xdef@theenvalue{numberc@glenote}%
@endnotemark@endnotetext
}%
}%
def@xendnote[#1]{%
begingroup
c@glenote=#1relax
unrestored@protected@xdef@theenmark{theendnote}%
unrestored@protected@xdef@theenvalue{numberc@glenote}%
show@theenvalue
endgroup
@endnotemark@endnotetext
}%
def@endnotemark{%
leavevmode
ifhmode
edef@x@sf{thespacefactor}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
}%
longdef@endnotetext#1{%
if@enotesopen
else
@openenotes
fi
immediatewrite@enotes{%
@doanenote{@theenmark}{@theenvalue}%
}%
begingroup
defnext{#1}%
newlinechar='40
immediatewrite@enotes{meaningnext}%
endgroup
immediatewrite@enotes{%
@endanenote
}%
}%
deftheendnotes{%
immediatecloseout@enotes
global@enotesopenfalse
begingroup
makeatletter
edef@tempa{`string>}%
ifnumcatcode@tempa=12
let@ResetGTrelax
else
edef@ResetGT{noexpandcatcode@tempa=thecatcode@tempa}%
@makeother>%
fi
def@doanenote##1##2##3>{%
def@theenmark{##1}%
def@theenvalue{##2}%
par
smallskip %<-small vertical gap between endnotes
begingroup
defhref{expandaftersavedhref}%
defurl{expandaftersavedurl}%
@ResetGT
edef@currentlabel{csname p@glenoteendcsname@theenvalue}%
enoteformat
}%
def@endanenote{%
parendgroup
}%
% 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
}%
defenoteformat{%
rightskipz@
leftskip1.8em
parindentz@
leavevmodellap{%
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
%newboxendnotebox%
%setboxendnotebox=hbox{11}%
%
%newlength{enotehangalign}%
%setlength{enotehangalign}{13p@}%
%
%AtBeginDocument{%
  %defenoteformat{%
  %def@makeenmark{hbox to wdendnotebox{hss{normalfont@theenmark}}}%
  %rightskipz@ leftskipz@ parindent=z@parskipz@def@textsuperscript{}hangindentenotehangalign%
    %leavevmode{makeenmarkignorespacesenskipignorespaces}RaggedRight}%
  %defenoteheading{section*{Notes}%
  %}%
  %defenotesize{fontsize{8}{10}selectfont}%
%}%
%@addtoreset{endnote}{chapter}%
%makeatother
usepackage{hyperendnote}


begin{document}

chapter{Test 1}
This is for testendnote{Test}. 
This is for testendnote{Test 2}. 
This is for testendnote{Test 3}. 
This is for testendnote{Test 4}. 
This is for testendnote{Test 5}. 
This is for testendnote{Test 6}. 
This is for testendnote{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 testendnote{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 testendnote{Test 2.1}. 
This is for testendnote{Test 2.2}. 
This is for testendnote{Test 2.3}.
This is for testendnote{Test 2.4}. 
This is for testendnote{Test 2.5}. 
This is for testendnote{Test 2.6}.
This is for testendnote{Test 2.7}. 
This is for testendnote{Test 2.8}. 
This is for testendnote{Test 2.9}.

clearpage

theendnotes
end{document}

Any help is welcome.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP