[Tex/LaTex] Add text right below the algorithms

algorithm2efootnotes

I try to add some texts used to illustrate the algorithms right below the bottom line of algorithms.

\documentclass{article}
\usepackage[ruled]{algorithm2e}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage[hang,flushmargin]{footmisc}
\usepackage{caption}
\begin{document}
\begin{algorithm}
 \caption{How to write algorithms}\label{algorithm1}
 \KwIn{this text}
 \KwOut{how to write algorithm with \LaTeX2e }
 initialization\;
 \While{not at end of this document}{
  read current\;
  \eIf{understand}{
   go to next section\;
   current section becomes this one\;
   }{
   $y_0\leftarrow y$\;
  }
 }
\end{algorithm}
\end{document}

enter image description here

I want to add some text below the bottom line as shown below:

enter image description here

Based on How to create footnote for algorithmicx's comment, one should be using a minipage. I update my codes as follows:

\documentclass{article}
\usepackage[ruled]{algorithm2e}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage[hang,flushmargin]{footmisc}
\usepackage{caption}
\begin{document}
\noindent\begin{minipage}{\textwidth}
\begin{algorithm}
 \caption{How to write algorithms}\label{algorithm1}\footnotetext{$y_0$ denotes the initial value.}
 \KwIn{this text}
 \KwOut{how to write algorithm with \LaTeX2e }
 initialization\;
 \While{not at end of this document}{
  read current\;
  \eIf{understand}{
   go to next section\;
   current section becomes this one\;
   }{
   $y_0\leftarrow y$\;
  }
 }
\end{algorithm}
\renewcommand\footnoterule{}
\end{minipage}
\end{document}

but errors occur.

Any suggestion will be greatly appreciated.

Best Answer

The construction of algorithm2e's float is intricate, since it allows for specifying a stay-Here-style float placement without the use of float. Additionally it manages placement of captions at the top or bottom to suit different styles. So accommodate your request for a footnote inside this float you need to split it in somewhere where it will not influence the other components in the construction. Here's a patch that provides \algorithmfootnote[<style>]{<stuff>} (<style> defaults to \footnotesize):

enter image description here

\documentclass{article}
\usepackage[ruled]{algorithm2e}
\usepackage{amssymb,amsmath,caption}
\usepackage[hang,flushmargin]{footmisc}
\usepackage{lipsum}
\makeatletter
\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}}%
  }%
}
\makeatother
\begin{document}
\lipsum[1-3]
\begin{algorithm}[tb]
  \caption{How to write algorithms}\label{algorithm1}
  \algorithmfootnote{$y_0$ denotes the initial value.}
  \KwIn{this text}
  \KwOut{how to write algorithm with \LaTeX2e }
  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
      }{
      $y_0 \leftarrow y$\;
    }
  }
\end{algorithm}
\end{document}
Related Question