4

I have created pseudocode for an algorithm and would like to add a footnote under the algorithm. However, when I try to apply the answer provided here, no footnote does appear. See the following example:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage[margin=2.5cm]{geometry}

\usepackage{amsmath}
\usepackage{amssymb}

\usepackage{algorithm, algpseudocode}
\makeatletter
\newcommand{\StatexIndent}[1][3]{%
  \setlength\@tempdima{\algorithmicindent}%
  \Statex\hskip\dimexpr#1\@tempdima\relax}
\algdef{S}[WHILE]{WhileNoDo}[1]{\algorithmicwhile\ #1}%

\newcommand{\algorithmfootnote}[2][\footnotesize]{%
  \let\old@algocf@finish\@algocf@finish% Store algorithm finish macro
  \def\@algocf@finish{\old@algocf@finish% Update finish macro to insert "footnote"
    \leavevmode\rlap{\begin{minipage}{\linewidth}
    #1#2
    \end{minipage}}%
  }%
}

\begin{document}

\begin{algorithm}[H]
    \caption{Algorithm}
    \algorithmfootnote{This is a footnote.}
    \label{alg:algorithm}
    \textbf{Input:} An input\\
    \textbf{Output:} An output
    \begin{algorithmic}[1]
        \Procedure{Test}{}
        \State $ x \gets$ 1
        \State \Return x
        \EndProcedure
    \end{algorithmic}
\end{algorithm}
\end{document}

which gives the following output:

enter image description here

Could someone tell me what I am doing wrong? Thanks in advance!

PS. I would like to keep using algorithmic (so not algorithmicx, algorithm2e, etc.) because of other layout structures

Mike
  • 125

1 Answers1

2

A possible solution is to use the savenotes environment from the footnote package. From the documentation:

there are several commands and environments (notably \parbox, minipage and tabular) which ‘trap’ footnotes so that they can’t escape and appear at the bottom of the page. The savenotes environment saves up any footnotes encountered within it, and performs them all at the end.

If you want the footnote directly below the algorithm instead of at the bottom of the page the you can use a minipage. The footnote line can be removed locally by redefining \footnoterule. Note that the minipage changes the numbering of the footnotes from numerical to alphabetical, which may be desired if you also use regular footnotes in the main text.

MWE, a6paper for the screenshot:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage[a6paper]{geometry}

\usepackage{algorithm, algpseudocode}
\usepackage{footnote}

\begin{document}
Regular text\footnote{with footnote}

% minipage to show footnote directly below the algorithm
% width of the minipage equal to the textwidth
\begin{minipage}{\textwidth}
% switch off footnote line locally
\renewcommand*\footnoterule{}
% collect footnotes within algorithm environment
\begin{savenotes}
\begin{algorithm}[H]
    \caption{Algorithm}
    \footnote{This is a footnote.}
    \label{alg:algorithm}
    \textbf{Input:} An input\\
    \textbf{Output:} An output
    \begin{algorithmic}[1]
        \Procedure{Test}{}
        \State $ x \gets$ 1
        \State \Return x
        \EndProcedure
    \end{algorithmic}
\end{algorithm}
\end{savenotes}
% footnotes are displayed at the end of the savenotes environment
\end{minipage}
\end{document}

Result:

enter image description here

Marijn
  • 37,699