2

To convert footnotes to endnotes, I know I can use the endnotes package with the commands shown below. Because I often have long footnotes I have created a custom environment for them using the environ package.

I can't figure out how to convert this environment for endnotes. TeX gives the error that \BODY is not defined.

\documentclass{article}                                                                        

% Dummy text                                                                                   
\usepackage[nopar]{lipsum}                                                                     

% Define a real environment for footnotes                                                      
% But how to convert to endnotes?                                                              
\usepackage{environ}                                                                           
\NewEnviron{Footnote}{\footnote{\BODY}}                                                        

% Convert footnotes to endnotes                                                                
\usepackage{endnotes}                                                                          
\let\footnote=\endnote                                                                         
\AtEndDocument{\theendnotes}                                                                   

\begin{document}                                                                               

\lipsum[1]\footnote{\lipsum[2]}                                                                

\lipsum[3]%                                                                                    
  \begin{Footnote} % Works normally, but not with endnotes conversion                                                                             
  \lipsum[4]                                                                                   

  \lipsum[5]                                                                                   
  \end{Footnote}                                                                               

\end{document}                                                                                 
2
  • Does \NewEnviron{Footnote}{\expandafter\footnote\expandafter{\BODY}} work?
    – cgnieder
    Feb 2, 2016 at 18:49
  • The error message tells that obviously the macro \BODY is written to the ent file. However, \BODY is only defined within the Footnote environment (or any other of environ's environments). The real endnote is the first expansion of \BODY. Hence my idea…
    – cgnieder
    Feb 3, 2016 at 9:34

1 Answer 1

2

Let's see what happens with a standard \endnote command:

\documentclass{article}
\usepackage{endnotes}

\newcommand{\foo}{foo} % just for the test

\begin{document}

Some text.\endnote{With a \foo{} explanation.}

\end{document}

After processing this document, you'll find a file with extension .ent containing

\@doanenote {1}
macro:->With
a
\foo
{}
explanation.
\@endanenote 

You see that no macro expansion has taken place. So with your code, what's written with the test file

\documentclass{article}
\usepackage{endnotes}
\usepackage{environ}

\NewEnviron{Footnote}{\footnote{\BODY}}[] % no \ignorespacesafterend
\let\footnote\endnote

\newcommand{\foo}{foo} % just for the test

\begin{document}

Some text.
\begin{Footnote}
With a \foo{} explanation.
\end{Footnote}

\end{document}

in the .ent file is

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

\@endanenote 

Any Footnote environment would write \BODY, which however is not defined outside Footnote (it is loaded with the current environment's content and then forgotten) and it would be useless to define it anyway.

You need to expand \BODY before it's seen by \endnote:

\NewEnviron{Footnote}{\expandafter\footnote\expandafter{\BODY}}

so \endnote will act on the expansion of \BODY (that is, the environment's contents).

Technical note. Basically, \endnote{Text} does

\def\temp{Text}\immediate\write\endnotefile{\meaning\temp}

with some more inessential tricks for adding \@doanenote{<note number>} at the beginning and \@endanenote at the end and for splitting the footnote text across multiple lines.

2
  • I've since discovered that the definition of Footnote needs to end with a space, otherwise there will be no space after the footnote mark in the body text. Feb 8, 2016 at 21:39
  • @AndrewCashner No, that's not correct; if you don't want the following space to be ignored, add [] at the end of the code for Footnote: \NewEnviron{Footnote}{\footnote{\BODY}}[]
    – egreg
    Feb 8, 2016 at 21:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .