The endnotes-hy Package
D. P. Story
Email: dpstory@uakron.edu processed April 10, 2020
Contents
1 Introduction 1
2 Required packages 2
3 Package code 2
4 Index 5
5 Change History 5
1
h∗packagei
1 Introduction
This package is an extension of endnotes.
Background: A colleague—a dedicated user of the endnotes package—complained to me (for some reason) that links created by hyperref do not point to the endnotes at the end of the file; for example, if we say
\endnote{\label{hnamei}htexti}
then any link, such as \hyperref[hnamei]{endnote~\ref*{hnamei}} jumps to the most recent anchor (Doc-Start, for example); no hypertext anchor is created where the endnote is inserted into the document. This short package is an attempt to rectify this problem.
Syntax: The \endnote command has been modified to conform to the following syntax:
\endnote*[hnumi]{htexti}\label{hnamei}
Refer to the documentation of the endnotes package for a description of the
\endnote command and its hnumi and htexti arguments. If the *-option (new
syntax) is used, the endnote mark is not placed, but the endnote is written to the ENT file. Such a “secret” endnote can be referred to using standard hyperref commands, see the endnote 1 on page 4 for more information. The placement of the \label has changed for this package as well. To define an endnote label, place \label{name} following the \endnote command, a more traditional loca- tion. This was done to get access to the hnamei, if it exist. This enables the package to place a hypertext link around the endnote mark.
2The optional hnumi can be used to write special endnotes.
N1As a side benefit to this new syntax, you can place verbatim text into an endnote.
3If there is no \label following
\endnote, no hypertext link is created.
4Note: This package will work correctly without the hyperref package (of course, no links). The advantage of using endnotes-hy without hyperref is to use the extended syntax: \endnote*[hnumi]{htexti}\label{hnamei}
2 Required packages
2
\RequirePackage{endnotes}
Require etoolbox because we redefine several commands using \patchcmd; this is not really necessary, we could have redefined the whole command instead.
3
\RequirePackage{etoolbox}
3 Package code
We allow the author not to use hyperref; this enables the use of the extended syntax of the \endnote command.
4
\newcommand\hyperref@nohy[2][]{#2}
5
\AtBeginDocument{\@ifundefined{hyperref}{\let\hyperref@en\hyperref@nohy
6
\let\phantomendnote\relax}
7
{\let\hyperref@en\hyperref}}
\phantomendnote The key is to create an hyperref anchor to reference; this definition is based on
\phantomsection of hyperref. This is used internally.
8
\def\phantomendnote{%
9
\Hy@MakeCurrentHrefAuto{endnote}%
10
\Hy@raisedlink
11
{\hyper@anchorstart{endnote.\@theenmark}\hyper@anchorend}}
12
\def\endnoteautorefname{endnote}
Redefinition of \theendnotes
\theendnotes
13
\patchcmd{\theendnotes}{\enoteformat}
14
{\phantomendnote\def\@currentHref{endnote.\@theenmark}%
15
\enoteformat}{}{}
Redefine \endnote to take an optional *, if this option is used, the mark does not
\endnote
appear in the text. This enables us to easily define an endnote and refer to it even
with multiple paragraphs. I didn’t like the syntax of inserting the \label within
the argument of \endnote, this seems to have problems. The new syntax is as follows:
Syntax: \endnote*[hnumi]{htexti}\label{hnamei}
This new syntax could be a problem if an author has already used endnotes and then converts to endnotes-hy. In endnotes the *-option is note defined and the
\label command is typically placed within the argument of the {htexti}. Labels would have to be moved to a position following {htexti}.
To obtain this syntax, we need several stages of parsing. The major problem is to get the label name in time to build a hyperref link around \@endnotemark.
16
\newtoks\@entoks
17
\def\endnote{\let\@encurrlabelname\@empty
18
\@ifstar{\let\@noMrk1\endnote@i}{\let\@noMrk0\endnote@i}}
19
\def\endnote@i{\@ifnextchar[%]
20
{\@xendnote}
21
{\stepcounter{endnote}%
22
\protected@xdef\@theenmark{\theendnote}%
We placed the {text} argument into a token register, this removes that argument from the input stream, and allows us to see of the next token is \label, if so, we get the label name argument.
23
\afterassignment\endnote@ii\@entoks}}
If the next token is \label, get the label name with \endnote@iii, otherwise, move on to \endnote@iv, the final step.
24
\def\endnote@ii{\@ifnextchar\label{\endnote@iii}{\endnote@iv}}
25
\def\endnote@iii\label#1{\def\@encurrlabelname{#1}\endnote@iv}
If the flag \@noMrk is 0 (mark is typeset), and if there is a label name, we make an hypertext link.
26
\def\endnote@iv{%
27
\if\@noMrk0\relax
28
\ifx\@encurrlabelname\@empty
29
\@endnotemark
30
\else
31
\hyperref@en[\@encurrlabelname]{\@endnotemark}%
32
\fi
33
\fi
Finish by expanding \@endnotetext with its argument that was saved in the
\@entoks register.
34
\expandafter\@endnotetext\expandafter{\the\@entoks}}
We patch into \@endnotetext just after \def\next{#1}. If there was a \label,
\@endnotetext
we insert it back into the argument of \endnote.
35
\patchcmd{\@endnotetext}
36
{\def\next{#1}}
37
{\ifx\@encurrlabelname\@empty
38
\def\next{#1}\else
39
\edef\x{\noexpand\label{\@encurrlabelname}}%
40
\expandafter\def\expandafter\next\expandafter{\x#1}\fi}{}{}
If \endnote has an optional argument, the flow passes to \xendnote. We re-
\xendnote
place \@endnotemark\@endnotetext by redirecting flow to to \endnote@ii, after saving the argument in \@entoks.
41
\patchcmd{\@xendnote}
42
{\@endnotemark\@endnotetext}
43
{\afterassignment\endnote@ii\@entoks}
44
{}{}
45
h/packagei
Notes
1An endnote created with *-option
2This is normal endnote
N1This is special endnote
3Some verbatim text
%^$^&$%^&
4No hypertext link created