9

I wish to create an environment that behaves alternately like a footnote, an endnote or a comment (i.e. hidden). For starters i've defined an environment "note" thus:

\newenvironment{note}
 { \footnote{ }
 { } }

expecting that now

Hello\begin{note}world\end{note}

would expand into

Hello\footnote{world}

but this doesn't seem to be the case. The document compiles with no warnings, and the pdf output does in fact contain a footnote, but the footnote is empty.

What might the problem be, and how can i fix it?

Evan Aad
  • 11,066

2 Answers2

12

Probably the simpliest way is to use \NewEnviron from the environ package and define your note environment as

\NewEnviron{note}{\footnote{\BODY}}

Complete code

\documentclass{article}
\usepackage{environ}

\NewEnviron{note}{\footnote{\BODY}}

\begin{document}

Hello\begin{note}world\end{note}

\end{document}

Output:

enter image description here

karlkoeller
  • 124,410
7

To create a new environment, it has to have the form

\newenvironment{<env>}
  {<begin env>}% \begin{<env>}
  {<end env>}% \end{<env>}

where <begin env> is execute at \begin{<env>} and <end env> is executed at \end{<env>}. Using your approach causes confusion, even though you think it might work:

\newenvironment{note}
  { \footnote{ }
  { } }

For example, even though you think \begin{note} should expand to \footnote{ and \end{note} should expand to }, thereby embracing the enviroment contents, TeX creates the environment at definition time, and the braces in your definition is ambiguous. In fact \begin{note} actually expands to \footnote{ } { } since the balanced pair of braces are matched at the same level/depth.

Your way around this predicament is to capture the environment contents first - made possible with the aid of environ, and then pass that to \footnote. The environment contents is grabbed inside a macro called \BODY:

enter image description here

\documentclass{article}
\usepackage{environ}% http://ctan.org/pkg/environ
\NewEnviron{note}{\footnote{\BODY}}% Environment-form of \footnote
\begin{document}

Hello\footnote{world}. Hello\begin{note}world\end{note}.

\end{document}

Note that, even with the environment-like approach, you're still limited in terms of what can be contained within (no verbatim content, for example).

Werner
  • 603,163