[Tex/LaTex] Text below line at end of algorithm

algorithmicxalgorithms

I am using the algorithm and algpseudocode packages. When I define an algorithm it ends with a horizontal line. I would like to have some text below this horizontal line explaining some special case scenarios. I've seen this used in various books. How do I put text below this horizontal line?

Best Answer

I've defined a new command \algcomment to be used just after the algorithm environment.

\newcommand{\algcomment}[1]{%
    \vspace{-\baselineskip}%
    \noindent%
    {\footnotesize #1\par}%
    \vspace{\baselineskip}%
    }

The only limitation is that you cannot allow the algorithm to float.

MWE:

\documentclass{article}

\usepackage{algorithm}
\usepackage{algpseudocode}

\usepackage{lipsum} % just for the example

\newcommand{\algcomment}[1]{%
    \vspace{-\baselineskip}%
    \noindent%
    {\footnotesize #1\par}%
    \vspace{\baselineskip}%
    }

\begin{document}
\lipsum[1-2]
\begin{algorithm}[H]
    \begin{algorithmic}[1]
        \For{$x<10$}
            \State $x\gets x+1$
        \EndFor
    \end{algorithmic}
    \caption{My algorithm}
\end{algorithm}
\algcomment{This algorithm talks about}
\lipsum[1-2]
\end{document} 

Output:

enter image description here

Related Question