# issue with endnotes and macros

I have the following issue with endnotes and macros. Here is the code:

\documentclass{article}

\usepackage{endnotes}

\begin{document}

\def\tmp{yellow}
yellow\endnote{\tmp}

\def\tmp{blue}
blue\endnote{\tmp}

\theendnotes

\end{document}

Output is:

In other words the second definition of /tmp kills the content of the first endnote. This is caused by the way endnotes handles macros and generates the .ent file:

\@doanenote {1}
macro:->\tmp

\@endanenote
\@doanenote {2}
macro:->\tmp

\@endanenote

Is there a way around it? I have a fairly complex tex application where endnote content is assembled at runtime from a database (using package datatool) and I can't avoid the use of macros. Content is however fairly simple and made of simple text, nothing fancy. I'd like to pass to \endnote{} this simple text and not the macro used to generate it, but I'm at a loss. Sorry if the answer is kinda trivial.

• Welcome to TeX.se, and thanks for including a minimal document! – Alan Munn Aug 25 at 16:15

You can expand the \tmp argument before the call to \endnote using \expandafter. This macro expands the token after the next token. A curly bracket ({) counts as a token, so in this case you need two \expandafter macros, one to skip over \endnote and one to skip over the curly bracket.

MWE:

\documentclass{article}

\usepackage{endnotes}

\begin{document}

\def\tmp{yellow}
yellow\expandafter\endnote\expandafter{\tmp}

\def\tmp{blue}
blue\expandafter\endnote\expandafter{\tmp}

\theendnotes

\end{document}

Result:

I have recently faced the same problem, but I needed also that that the optional \endnote parameter to be recognized.

This is my solution, expanding the Marijn's answer:

\documentclass{article}

% redefine \endnote
\usepackage{endnotes}
\let\eendnote\endnote % original \endnote
\renewcommand{\endnote}[2][]{%
\def\optarg{#1}%
\edef\etext{#2}%
\ifx\optarg\empty%
\let\enote\eendnote%
\else%
\edef\enote{\noexpand\eendnote[#1]}%
\fi%
\expandafter\enote\expandafter{\etext}%
}

\begin{document}

\def\tmp{yellow}
Yellow\endnote{This is \tmp.}\par
\def\tmp{blue}
Blue\endnote{This is \detokenize{\textit}{\tmp} and more \tmp.}\par
\def\tmp{green}
Green\endnote[10]{\tmp\ at last.}\par

\theendnotes

\end{document}

I'm not sure my approach to use the optional parameter is usual or not, so feel free to comment!