3

In a previous post i mentioned my attempt to create an environment that behaves alternately like a footnote, an endnote or a comment (i.e. hidden). Thanks to the help of those who answered the above mentioned question, i am now able to define a new environment that behaves like the footnote environment by employing one of the following two technique:

\documentclass{article}
\usepackage{environ}
\NewEnviron{mynote}{\footnote{\BODY}}
\begin{document}
Hello\begin{mynote}world\end{mynote}
\end{document}

or equivalently

\documentclass{article}
\newenvironment{mynote}{\footnote\bgroup}{\egroup}
\begin{document}
Hello\begin{mynote}world\end{mynote}.
\end{document}

Unfortunately, what works for footnotes fails for endnotes and comments. The following two blocks of code (the first for endnotes using the second technique and the second for comments using the first technique), don't even compile.

\documentclass{article}
\usepackage{endnotes}
\newenvironment{mynote}{\endnote\bgroup}{\egroup}
\begin{document}
Hello\begin{mynote}world\end{mynote}.
\theendnotes
\end{document}

\documentclass{article}
\usepackage{comment}
\usepackage{environ}
\NewEnviron{mynote}{\begin{comment}\BODY\end{comment}}
\begin{document}
Hello\begin{mynote}world\end{mynote}
\end{document}

Why do the techniques that work for footnotes fail for endnotes and comments? What can i do about it?

Evan Aad
  • 11,066
  • As I commented in the other question, the \footnote\bgroup trick seems to work, but it's just by chance and the result is not really correct. I don't see the reason for using an environment. – egreg Nov 09 '13 at 11:59
  • You can simply say \NewEnviron{mynote}{} for the “comment” case. You're not compelled to use \BODY. – egreg Nov 09 '13 at 13:58

1 Answers1

4

I don't see why using an environment. My best shot would be using a command.

\documentclass{article}
\usepackage{endnotes}

\makeatletter
% p for `perhaps'
\newcommand{\pfootnote}{\csname aad@footnote@\aad@footnotekind\endcsname}
\newcommand{\footnotekind}[1]{\def\aad@footnotekind{#1}}

\let\aad@footnote@footnote\footnote
\let\aad@footnote@endnote\endnote
\newcommand\aad@footnote@comment[2][]{}
\def\aad@footnotekind{footnote} % default
\makeatother

%% footnotes are footnotes
\footnotekind{footnote}

\begin{document}

Hello\pfootnote{world}

\footnotekind{endnote}

Hello\pfootnote{world}

\footnotekind{comment}

Hello\pfootnote{world}

\bigskip
\hrule
\bigskip

\theendnotes

\end{document}

Of course you won't be changing the kind of footnotes in the middle of the document; you'll probably set one among

\footnotekind{footnote}
\footnotekind{endnote}
\footnotekind{comment}

the first being the default (but I used it in the preamble for greater clarity).

enter image description here

(Note: the height of the example image has been artificially reduced just for showing the result.)

If you really want an environment, add to the above macros also

\usepackage{environ}
\NewEnviron{mynote}{\expandafter\pfootnote\expandafter{\BODY}}

A version that automatically adds the endnotes if \footnotekind{endnote} is issued. Something is added to be executed \AtEndDocument; in case footnotes or comments are chosen, this will be nothing because the corresponding sequence is not defined.

\documentclass{article}
\usepackage{endnotes}

\makeatletter
% p for `perhaps'
\newcommand{\pfootnote}{\csname aad@footnote@\aad@footnotekind\endcsname}
\newcommand{\footnotekind}[1]{%
  \def\aad@footnotekind{#1}%
  \AtEndDocument{\@nameuse{aad@footnoteatend@#1}}%
}

\let\aad@footnote@footnote\footnote
\let\aad@footnote@endnote\endnote
\newcommand\aad@footnote@comment[2][]{}
\def\aad@footnoteatend@endnote{\theendnotes}

\def\aad@footnotekind{footnote} % default
\makeatother

%% footnotes are endnotes
\footnotekind{endnote}

\begin{document}

Hello\pfootnote{world}

Hello\pfootnote{world again}

Hello\pfootnote{world and again}

\end{document}
egreg
  • 1,121,712
  • Thank you very much. Is it possible to modify the command to add \theendnotes automatically just before the \end{document} tag when \footnotekind{endnote} is activated? – Evan Aad Nov 09 '13 at 22:42
  • 1
    @EvanAad Yes, but with a little more work. – egreg Nov 09 '13 at 22:44