1

I would like to systematically specify the page where an endnote is. I could not find anything useful in the documentation.

The expected result should look like that: enter image description here

So far, I have built a newcommand which does the job but not in a very user friendly way. MWE:

\documentclass[oneside,10pt]{memoir} 
\usepackage{endnotes} % To write endnotes
\usepackage{lipsum}
\usepackage{refcount} % To access the page number where a label is

\newcommand{\MyEndNote}[2]{
\endnote{~page \getpagerefnumber{#2} : #1} \label{#2}
}


\begin{document}
\lipsum[1]\MyEndNote{first endnote}{lab1}\lipsum[2]
\newpage

\lipsum[3]\MyEndNote{another endnote}{lab2}\lipsum[1]\MyEndNote{a third one }{lab3}

\newpage
\theendnotes

\end{document}

What I would like is to remove the second argument and generate it, somehow, automatically.

Does anyone have any idea ? Thanks in advance.

1

The counter for \endnote is stepped after the endnote text has been processed. So we have to reach in the argument to increment it beforehand in order to pass the automatically generated label to \pageref

\documentclass[oneside,10pt]{memoir} 
\usepackage{endnotes} % To write endnotes
\usepackage{lipsum}

\newcommand{\addendnotepage}[1]{~page~#1: }

\newcommand{\MyEndNote}[1]{%
  \expandafter\endnote\expandafter{%
    \expandafter\addendnotepage\expandafter{%
      \expandafter\pageref\expandafter{%
        \the\numexpr\arabic{endnote}+1\relax:endnote%
      }%
    }%
    #1%
  }%
  \label{\arabic{endnote}:endnote}%
}

\begin{document}

\lipsum*[1]\MyEndNote{first endnote}\lipsum[2]

\newpage

\lipsum*[3]\MyEndNote{another endnote}\lipsum[1]\MyEndNote{a third one}

\newpage

\theendnotes

\end{document}

enter image description here

A different implementation with the more powerful expl3 language.

\documentclass[oneside,10pt]{memoir} 
\usepackage{endnotes} % To write endnotes
\usepackage{xparse} % more powerful
\usepackage{lipsum}

\ExplSyntaxOn
\NewDocumentCommand{\MyEndNote}{m}
 {
  \polyv_myendnote:nf { #1 } { \int_eval:n { \arabic{endnote}+1 } }
 }

\cs_new_protected:Nn \polyv_myendnote:nn
 {
  \endnote{\nobreakspace page \nobreakspace \pageref{#2:endnote}:~#1}
  \label{\arabic{endnote}:endnote}
 }
\cs_generate_variant:Nn \polyv_myendnote:nn { nf }
\ExplSyntaxOff


\begin{document}

\lipsum*[1]\MyEndNote{first endnote}\lipsum[2]

\newpage

\lipsum*[3]\MyEndNote{another endnote}\lipsum[1]\MyEndNote{a third one}

\newpage

\theendnotes

\end{document}

This is much more flexible; for instance, if you change the definition of \polyv_myendnote:nn into

\cs_new_protected:Nn \polyv_myendnote:nn
 {
  \endnote{#1\nobreakspace(page\nobreakspace\pageref{#2:endnote})}
  \label{\arabic{endnote}:endnote}
 }

with no other change, you'd get

enter image description here

With the f variant, the second argument to \polyv_myendnote:nf is expanded prior LaTeX passes it to the main function \polyv_myendnote:nn.

  • Thanks a lot ! I was miles away to find a solution. Better learn more about expandafter and xparse. I implemented the second solution. Much much better looking. Back to writing ! – PolyV Jul 1 '18 at 7:38
  • By the way, I don't suppose there's an easy solution to group the endnotes by page. Page 1 : 1. first note 2. second Page 3. 3. third 4. fourth Page 4: X. and so on It would need to modify the code of the command \theendnotes, right ? – PolyV Jul 1 '18 at 8:44
  • @PolyV Possibly with reprocessing the .ent file. – egreg Jul 1 '18 at 9:08
  • I should try that when I'll feel more fluent in LaTeX. So far, your solution is making my work much more easier already. – PolyV Jul 1 '18 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.