6

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?

  • Are you talking about a caption? Within the algorithm environment, just provide a \caption{my text here}. – Sean Allred Apr 6 '14 at 18:13
  • That puts the text the top of the algorithm beside the algorithm name, I want it under the line at the end of the algorithm. – sonicboom Apr 6 '14 at 18:25
7

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

4

This is another option, removing the limitation in Karl Koeller's answer (the environment can freely float if desired, and \algcomment can be used inside the algorithm environment):

\documentclass{article}s
\usepackage{etoolbox}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{lipsum} % just for the example

\makeatletter
\AfterEndEnvironment{algorithm}{\let\@algcomment\relax}
\AtEndEnvironment{algorithm}{\kern2pt\hrule\relax\vskip3pt\@algcomment}
\let\@algcomment\relax
\newcommand\algcomment[1]{\def\@algcomment{\footnotesize#1}}

\renewcommand\fs@ruled{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled
  \def\@fs@pre{\hrule height.8pt depth0pt \kern2pt}%
  \def\@fs@post{}%
  \def\@fs@mid{\kern2pt\hrule\kern2pt}%
  \let\@fs@iftopcapt\iftrue}
\makeatother

\begin{document}

Some test text.
\begin{algorithm}
  \algcomment{This algorithm talks about a really special procedure which will be described elsewhere in this document}
  \begin{algorithmic}[1]
    \For{$x<10$}
    \State $x\gets x+1$
    \EndFor
  \end{algorithmic}
  \caption{My algorithm}
\end{algorithm}
\lipsum[4]

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.