# How to Create an Endnote able to Link to the Current Section with "Autolabelling"?

I would like tu use footnotes and chapter endnotes in my document (chapter endnotes will be used only for "large" notes).

In endnotes, I would be glad to be able to ref/link to the current chapter(/section/subsection/...) where the endnote was called and the text just before it because I found inconvenient to just have the list of endnotes at the end of a chapter and don't remember what extactly the note is dealing with (the context). So I would like to be able to cite a part of the text and the section number into the endnote in order to put the endnote in context and then help the reader...

Is there a way to have something like a "\myendnote" command with two arguments and ables to:

1. Print the first argument in the body where the command is called AND in the endnote (between quotes "" and in italic in the exemple below);
2. Print, in the endnote text, the link to the current chapter, section, subsection, etc. where the command is called and even the footnote number (if the endnote is called into a footnote, see MWE). This last functionality (dealing with the footnote could be adressed with another command like a "\myfootendnote" or with another argument, I'm open to everything...);
\newcommand{\myendnote}[2]{%
% Here maybe something to auto generate the "autolabel" ???
{#1}\endnote{\emph{\S{}~\ref{autolabel} \nameref{autolabel}, p. \pageref{autolabel}, " {#1} "~}: {#2}}}
% #1 : Text displayed in body document (which have the endnote superscript mark) AND in the endnote between quotes "" and in italic.
% #2 : Text of the endnote


See the final working solution below (EDIT 3)

Here a MWE:

\documentclass{report}
\usepackage{hyperref}
\usepackage{nameref}
\usepackage{enotez}
\setenotez{counter-format=alph}

\usepackage{verbatim}
\usepackage{fancyvrb}% Only used for verbatim footnotes
\VerbatimFootnotes

\newcommand{\myendnote}[2]{%
% Here maybe something to auto generate the "autolabel" ???
{#1}\endnote{\emph{\S{}~\ref{autolabel} \nameref{autolabel}, p. \pageref{autolabel}, " {#1} "~}: {#2}}}
% #1 : Text displayed in body document (which have the endnote superscript mark) AND in the endnote between quotes "" and in italic.
% #2 : Text of the endnote

\begin{document}

\chapter{First Chapter}
\chapter{Second Chapter}
\section{A section}
\subsection{A subsection}

\verb!\myendnote{Some text with an end note}{text in the end note no 1}!

Some text with a foot note\footnote{\verb!\myendnote{footnote text}{text in the end note no 2}!%
}

\printendnotes

\end{document}


And here the expected results:

## EDIT 3: final working solution thanks to John Kormylo answer

The final solution works with several chapters with endnotes but the hyperlinks related to footnote - in the endnote list - doesn't redirect to the right page but print the correct number.

The \myendnote command call is simplified because the user doesn't need to pass the counters as arguments anymore, the command deals alone with labelling automaticaly.

MWE:

\documentclass{report}
\usepackage{nameref}
\usepackage{hyperref}
\usepackage{enotez}
\setenotez{counter-format=alph}

\usepackage{verbatim}
\usepackage{fancyvrb}% Only used for verbatim footnotes
\VerbatimFootnotes

\newcounter{endnoteLabelCnt}
\setcounter{endnoteLabelCnt}{0}
\newcounter{endnoteLabelUsed}
\setcounter{endnoteLabelUsed}{0}
\newcounter{endnoteLabelInFootnote}
\setcounter{endnoteLabelInFootnote}{0}
\newcounter{endnoteLabelInFootnoteUsed}
\setcounter{endnoteLabelInFootnoteUsed}{0}

\newcommand{\autoLabelling}{%
\stepcounter{endnoteLabelCnt}%
\label{\thechapter\theendnoteLabelCnt}%
}%

\newcommand{\autoLabellingFootnote}{%
\stepcounter{endnoteLabelInFootnote}%
\label{\thechapter\theendnoteLabelInFootnote\thechapter}%
}%

% Modification of the command in order to deal with call inside a footnote
% <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
\usepackage{xparse}
\NewDocumentCommand{\myendnote}{m m o}{%
% <code>
\IfNoValueTF{#3}{% code when no optional argument #3 is passed
{#1}\autoLabelling\endnote{\stepcounter{endnoteLabelUsed}\emph{\S{}~\ref{\thechapter\theendnoteLabelUsed} \nameref{\thechapter\theendnoteLabelUsed}, p. \pageref{\thechapter\theendnoteLabelUsed}, " {#1} "~}: {#2}}}
{% code when the optional argument #3 is present
% Note that, on the next line, the \stepcounter is moved after the \ref and \nameref in order to link to the current section and after to the footnote numer
{#1}\autoLabelling\autoLabellingFootnote\endnote{\emph{\S{}~\ref{\thechapter\theendnoteLabelUsed} \nameref{\thechapter\theendnoteLabelUsed}, \stepcounter{endnoteLabelUsed}\stepcounter{endnoteLabelInFootnoteUsed}footnote \ref{\thechapter\theendnoteLabelInFootnoteUsed\thechapter}, p. \pageref{\thechapter\theendnoteLabelInFootnoteUsed\thechapter}, " {#1} "~}: {#2}}}
}
% #1 : Text displayed in body document (which have the endnote superscript mark) AND in the endnote between quotes "" and in italic.
% #2 : Text of the endnote
% #3 : Optionnal argument if command passed inside a footnote

\begin{document}

\chapter{First Chapter}
\section{A First section}
\subsection{A First subsection}

\verb!\myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}:! \myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}

Some text with a foot note\footnote{\verb!\myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}[footnote]:! \myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}[footnote]}

\printendnotes

\chapter{Second Chapter}
\section{A second section}
\subsection{A second subsection}

\verb!\myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}:! \myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}

Some text with a foot note\footnote{\verb!\myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}[footnote]:! \myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}[footnote]}

\printendnotes

\chapter{Third Chapter}
\section{A third section}
\subsection{A third subsection}

\verb!\myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}:! \myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}

Some text with a foot note\footnote{\verb!\myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}[footnote]:! \myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}[footnote]}

\printendnotes
\end{document}


## EDIT 1: First Attempt

My Non Working Solution is to use 2 new commands:

• \autoLabelling command which create a label based on a counter endnoteLabelCnt:
\newcommand{\autoLabelling}{
\stepcounter{endnoteLabelCnt}
\label{\arabic{endnoteLabelCnt}}
}

• \myendnote as presented above but with 3 arguments, the last one is the label created thanks to the counter endnoteLabelCnt with \autoLabelling command.

Here a Minimal NON Working Exemple:

\documentclass{report}
%\usepackage{hyperref}
\usepackage{nameref}
\usepackage{enotez}
\setenotez{counter-format=alph}

\usepackage{verbatim}
\usepackage{fancyvrb}% Only used for verbatim footnotes
\VerbatimFootnotes

\newcounter{endnoteLabelCnt}
\setcounter{endnoteLabelCnt}{0}

\newcommand{\autoLabelling}{
\stepcounter{endnoteLabelCnt}
\label{\arabic{endnoteLabelCnt}}
}

\newcommand{\myendnote}[3]{%
% Here maybe something to auto generate the "autolabel" ???
{#1}\endnote{\emph{\S{}~\ref{#3} \nameref{#3}, p. \pageref{#3}, " {#1} "~}: {#2}}}
% #1 : Text displayed in body document (which have the endnote superscript mark) AND in the endnote between quotes "" and in italic.
% #2 : Text of the endnote
% #3 : Label

\begin{document}

\chapter{First Chapter}
\chapter{Second Chapter}
\section{A section}
\subsection{A subsection}
\autoLabelling

\verb!\myendnote{Some text with an end note}{text in the end note no 1}!

\verb!{\arabic{AbsoluteSectionNumber}} :! \myendnote{Some text with an end note}{text in the end note no 1}{\arabic{endnoteLabelCnt}}

% Warning: \autoLabelling command is called into the footnote here:
Some text with a foot note\footnote{\autoLabelling\verb!\myendnote{footnote text}{text in the end note no 2} :! \myendnote{Some text with an end note}{text in the end note no 2}{\arabic{endnoteLabelCnt}}}

\printendnotes

\end{document}


The results:

The problem is highlighted with a red rectangle: all the links are pointing to the footnote number "1" (which corresponds to the last use of \autoLabelling command in the footnote). The first endnote "a" should point to the subsection 2.1.1 as schown in the expected results above. It seems that the counter endnoteLabelCnt is "incremented everywhere" even before the last call of \autoLabelling command...

## EDIT 2: Second Attempt after follow up comment

The solution proposed by John Kormylo works inside a chapter but I can't make it works if there are several chapters in my document.

I tried to modify the \myendnote command in order to deal with call inside a footnote thanks to an optional argument:

\usepackage{xparse}
\NewDocumentCommand{\myendnote}{m m m o}{%
% <code>
\IfNoValueTF{#4}{% code when no optional argument #4 is passed
{#1}\autoLabelling\endnote{\stepcounter{endnoteLabelCnt}\emph{\S{}~\ref{#3} \nameref{#3}, p. \pageref{#3}, " {#1} "~}: {#2}}}
{% code when the optional argument #4 is present
% Note that, on the next line, the \stepcounter is moved after the \ref and \nameref in order to link to the current section and after to the footnote numer
{#1}\autoLabelling\endnote{\emph{\S{}~\ref{#3} \nameref{#3}, \stepcounter{endnoteLabelCnt}footnote \ref{#3}, p. \pageref{#3}, " {#1} "~}: {#2}}}
}
% #1 : Text displayed in body document (which have the endnote superscript mark) AND in the endnote between quotes "" and in italic.
% #2 : Text of the endnote
% #3 : Label
% #4 : Optionnal argument if command passed inside a footnote


I tried this:

\documentclass{report}
\usepackage{nameref}
\usepackage{enotez}
\usepackage{hyperref}
\setenotez{counter-format=alph}

\usepackage{verbatim}
\usepackage{fancyvrb}% Only used for verbatim footnotes
\VerbatimFootnotes

\newcounter{endnoteLabelCnt}
\setcounter{endnoteLabelCnt}{0}

\newcommand{\autoLabelling}{%
\stepcounter{endnoteLabelCnt}
% Modification here by adding "\thechapter"
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
\label{\thechapter\theendnoteLabelCnt}
}

% Modification of the command in order to deal with call inside a footnote
% <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
\usepackage{xparse}
\NewDocumentCommand{\myendnote}{m m m o}{%
% <code>
\IfNoValueTF{#4}{% code when no optional argument #4 is passed
{#1}\autoLabelling\endnote{\stepcounter{endnoteLabelCnt}\emph{\S{}~\ref{#3} \nameref{#3}, p. \pageref{#3}, " {#1} "~}: {#2}}}
{% code when the optional argument #4 is present
% Note that, on the next line, the \stepcounter is moved after the \ref and \nameref in order to link to the current section and after to the footnote numer
{#1}\autoLabelling\endnote{\emph{\S{}~\ref{#3} \nameref{#3}, \stepcounter{endnoteLabelCnt}footnote \ref{#3}, p. \pageref{#3}, " {#1} "~}: {#2}}}
}
% #1 : Text displayed in body document (which have the endnote superscript mark) AND in the endnote between quotes "" and in italic.
% #2 : Text of the endnote
% #3 : Label
% #4 : Optionnal argument if command passed inside a footnote
% <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

\begin{document}

\chapter{First Chapter}
\section{A First section}
\subsection{A First subsection}
\verb!\myendnote{Some text with an end note}{text in the end note no 1}!

\verb!{\arabic{AbsoluteSectionNumber}} :! \myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}{\thechapter\theendnoteLabelCnt}

Some text with a foot note\footnote{\verb!\myendnote{footnote text}{text in the end note no \theendnoteLabelCnt} :! \myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}{\thechapter\theendnoteLabelCnt}[footnote]}

\setcounter{endnoteLabelCnt}{0}
\printendnotes

\chapter{Second Chapter}
\section{A second section}
\subsection{A second subsection}

\verb!\myendnote{Some text with an end note}{text in the end note no 1}!

\verb!{\arabic{AbsoluteSectionNumber}} :!\myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}{\thechapter\theendnoteLabelCnt}

Some text with a foot note\footnote{\verb!\myendnote{footnote text}{text in the end note no \theendnoteLabelCnt} :!\myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}{\thechapter\theendnoteLabelCnt}[footnote]}

\setcounter{endnoteLabelCnt}{0}
\printendnotes

\end{document}


But it doesn't work in the second chaper endnote list, I get only ?? characters...

• The first problem is that the endnotes are sent unexpanded, The second is that a \label inside a footnote does not work (no \newlabel in aux file). Feb 8 at 16:47
• BTW, I noticed that endnotes writes to an .ent file, while enotez writes the notes to the aux file. I'm surprised no one writes the notes to a savebox. It would be relatively easy to implement. I wouldn't recommend it for more than one page worth of notes, though. Feb 8 at 21:35

This sort of works. It prints the footnote counter, but \nameref still prints "A Section".

\documentclass{report}
\usepackage{nameref}
\usepackage{enotez}
\usepackage{hyperref}
%\setenotez{counter-format=alph}

\usepackage{verbatim}
\usepackage{fancyvrb}% Only used for verbatim footnotes
\VerbatimFootnotes

\newcounter{endnoteLabelCnt}
\setcounter{endnoteLabelCnt}{0}

\newcommand{\autoLabelling}{%
\stepcounter{endnoteLabelCnt}
\label{test\theendnoteLabelCnt}
}

\newcommand{\myendnote}[3]{% #1=
% Here maybe something to auto generate the "autolabel" ???
{#1}\endnote{\stepcounter{endnoteLabelCnt}\emph{\S{}~\ref{#3} \nameref{#3}, p. \pageref{#3}, " {#1} "~}: {#2}}}
% #1 : Text displayed in body document (which have the endnote superscript mark) AND in the endnote between quotes "" and in italic.
% #2 : Text of the endnote
% #3 : Label

\begin{document}

\chapter{First Chapter}
\chapter{Second Chapter}
\section{A section}
\subsection{A subsection}
\autoLabelling

\verb!\myendnote{Some text with an end note}{text in the end note no 1}!

\verb!{\arabic{AbsoluteSectionNumber}} :! \myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}{test\theendnoteLabelCnt}

% Warning: \autoLabelling command is called into the footnote here:
Some text with a foot note\footnote{\verb!\myendnote{footnote text}{text in the end note no \theendnoteLabelCnt} :! \myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}{test\theendnoteLabelCnt}}\autoLabelling

\setcounter{endnoteLabelCnt}{0}
\printendnotes

\end{document}


If you intend to call \printendnotes more than once, you will need two counters. All labels must have unique names, so you can't reuse the old numbers.

\documentclass{report}
\usepackage{nameref}
\usepackage{enotez}
\usepackage{hyperref}
\setenotez{counter-format=alph}

\usepackage{verbatim}
\usepackage{fancyvrb}% Only used for verbatim footnotes
\VerbatimFootnotes

\newcounter{endnoteLabelCnt}
\setcounter{endnoteLabelCnt}{0}
\newcounter{endnoteLabelUsed}
\setcounter{endnoteLabelUsed}{0}

\newcommand{\autoLabelling}{%
\stepcounter{endnoteLabelCnt}
% Modification here by adding "\thechapter"
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
\label{\thechapter\theendnoteLabelCnt}
}

% Modification of the command in order to deal with call inside a footnote
% <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
\usepackage{xparse}
\NewDocumentCommand{\myendnote}{m m m o}{%
% <code>
\IfNoValueTF{#4}{% code when no optional argument #4 is passed
{#1}\autoLabelling\endnote{\stepcounter{endnoteLabelUsed}\emph{\S{}~\ref{#3} \nameref{#3}, p. \pageref{#3}, " {#1} "~}: {#2}}}
{% code when the optional argument #4 is present
% Note that, on the next line, the \stepcounter is moved after the \ref and \nameref in order to link to the current section and after to the footnote numer
{#1}\autoLabelling\endnote{\emph{\S{}~\ref{#3} \nameref{#3}, \stepcounter{endnoteLabelUsed}footnote \ref{#3}, p. \pageref{#3}, " {#1} "~}: {#2}}}
}
% #1 : Text displayed in body document (which have the endnote superscript mark) AND in the endnote between quotes "" and in italic.
% #2 : Text of the endnote
% #3 : Label
% #4 : Optionnal argument if command passed inside a footnote
% <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

\begin{document}

\chapter{First Chapter}
\section{A First section}
\subsection{A First subsection}
\verb!\myendnote{Some text with an end note}{text in the end note no 1}!

\verb!{\arabic{AbsoluteSectionNumber}} :! \myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}{\thechapter\theendnoteLabelCnt}

Some text with a foot note\footnote{\verb!\myendnote{footnote text}{text in the end note no \theendnoteLabelCnt} :! \myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}{\thechapter\theendnoteLabelCnt}[footnote]}

\printendnotes

\chapter{Second Chapter}
\section{A second section}
\subsection{A second subsection}

\verb!\myendnote{Some text with an end note}{text in the end note no 1}!

\verb!{\arabic{AbsoluteSectionNumber}} :!\myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}{\thechapter\theendnoteLabelCnt}

Some text with a foot note\footnote{\verb!\myendnote{footnote text}{text in the end note no \theendnoteLabelCnt} :!\myendnote{Some text with an end note \theendnoteLabelCnt}{text in the end note no \theendnoteLabelCnt}{\thechapter\theendnoteLabelCnt}[footnote]}

\printendnotes

\end{document}

• It works perfectly inside of a chapter ! Thanks a lot ! However, do you no by chance if there is a way to make it works in another chapter also without any further action for the user? I tried to replace test\theendnoteLabelCnt} by \arabic{chapter}\theendnoteLabelCnt} but it doesn't work... Feb 9 at 7:38
• Remember that the labels are not expanded until \printendnotes. You may need to reset the chapter counter and add \addtoendnotes{\stepcounter{chapter}} at the beginning of each chapter. Also, chapter 1 endnote 1 will produce label 11 (eleven). Feb 9 at 14:03
• Thanks for your proposition. I tried something but it doesn't work (see my second edit). I think I don't understand how counter works in LaTeX... Sorry. Feb 10 at 16:42