[Tex/LaTex] Footnotes: Label in paragraph, text somewhere else

footnoteslyx

TL;DR: Is there a package that allows writing the
text of a footnote after the position where the
mark should be inserted? Note that I'm asking
about positioning of source code, not about
positioning of the note in the output document.


When writing LaTeX I find the inline character of
footnote source somewhat distracting.

This is some text\footnote{That for
whatever reason contains a long 
footnote. The main text becomes split 
apart} so far that it is barely readable. 

Doing tricks with newlines like

This is some text\footnote{
  That for whatever reason contains a long
  footnote. The main text becomes split
  apart}
so far that it is barely readable.

Also helps only so much. I'd much prefer to write

This is some text\fn{1} that contains a
couple\fn{2} footnotes yet remain readable.

\text{1}{Here is the text of the first
footnote, conveniently readable while reading
the main source, yet not disrupting it.}

\text{2}{-/2}

I know that some tricks are possible with
\footnotemark and \footnotetext. When having
more than one footnote in a paragraph however this
would require manually changing the footnote
counter, which is too inconvenient to make this a
sensible alternative. Also if I'm not mistaken, it
carries the risk of causing footnotes to be
displayed on the wrong page, when the paragraph
stretches to the next page but the \footnotemark
is in text on the current page.

+---------------+  +---------------+
|...            |  |end of the page|
|               |  |...            |
|               |  |               |
|some text with²|  |—————          |
|footnote on the|  |²The FNText    |
+---------------+  +---------------+

Is there maybe some package that automates such a
style?

As a quick hack I sometimes used constructs like

\def\fnlater#1#2{\def#1{\footnote{#2}}}
...
\fnlater\myfnI{The footnote text must be
defined before the paragraph.}
...
Here is some textparagraph\myfnI that refers
to the previously defined footnote. 

Sadly this custom solution has two major flaws:

  1. It requires defining the footnote before
    using it, while intuitively footnotes are expected
    after the surrounding text.

  2. It should cause a compilation error, when the
    footnote is accidentially used twice.

  3. It should also cause a compilation error, when
    the footnote isn't used at all.

Since I don't think that these issues are easily
fixed, especially the requirement to define the
footnote before the surrounding text, using custom
hacks is undesireable.

If such a package exists for LaTeX, availability of a
corresponding module for LyX would be even better.


As a minimal compileable example put any of these
code examples into a skeleton

\documentclass{article}
\begin{document}
...
\end{document}

Best Answer

I managed to put this together. It only allows clear text in the footer, but apart from that it should do all you wanted. No error is raised if a label is undefined, however, as that would give an error every time a new footnote is added.

Edit: Now it supports formatting thanks to \detokenize :D

Footnotes are defined anywhere in the document with \definefootnote{label}{footnotetext} and used anywhere else in the document with \usefootnote{label}.

\documentclass{article}

\makeatletter
\catcode`#=11
\AtEndDocument{
\immediate\write\@auxout{\detokenize{%
    \newcommand{\@makefootnote}[2]%
        {\expandafter\gdef\csname footnote@#1\endcsname{#2}%
        % Check if footnote is unused
        \AtEndDocument%
            {\expandafter\ifx\csname footnoteused@#1\endcsname\relax%
                \errmessage{Error: Footnote #1 never used!}%
            \fi}}}}}
\catcode`#=6

\newcommand{\@addfootnotetolist}[2]
    {\AtEndDocument{\immediate\write\@auxout
        {\detokenize{\@makefootnote{#1}{#2}}}}}

\newcommand{\definefootnote}[2]
    {% Check if footnote has been defined earlier
    \expandafter\ifx\csname footnotedefd@#1\endcsname\relax
        \@addfootnotetolist{#1}{#2}%
        \expandafter\def\csname footnotedefd@#1\endcsname{}%
    \else
        \errmessage{Error: Footnote #1 defined twice!}
    \fi}
\newcommand{\usefootnote}[1]
    {% Check if footnote is defined
    \expandafter\ifx\csname footnote@#1\endcsname\relax
        % not yet defined, give warning?
        % always happens first time used, of course
        \expandafter\def\csname footnoteused@#1\endcsname{}%
    \else
        % Check if footnote has been used
        \expandafter\ifx\csname footnoteused@#1\endcsname\relax
            \footnote{\csname footnote@#1\endcsname}%
            \expandafter\def\csname footnoteused@#1\endcsname{}%
        \else
            \errmessage{Error: Footnote #1 used twice!}
        \fi
    \fi}
\makeatother

\begin{document}

text\usefootnote{label}

\definefootnote{label}{footnotetext}

\end{document}
Related Question