[Tex/LaTex] How to reformat Algorithm in memoir document class

algorithmsformattingmemoir

I'm trying to typeset algorithms in the memoir class. I'm using the algorithmic package with a custom environment as this was the only way I could get the list of algorithm to display correctly in the table of content, and the algorithm to be listed correctly in the list algorithms. As shown in the following images,

table of content correct
list of algorithms correct

Currently the algorithm is displayed as follows,

algorithm display

How to I reformat the algorithms environment to look more like the following?

example one
example two

I don’t need the exact formatting, just something more professional. I would ideally like line numbering, the vertical lines, the horizontal lines, and some form of left and right indentation that is less than the text width. My current code is,

\documentclass[]{memoir}
\usepackage[utf8x]{inputenc}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{algorithmic}

\newcommand{\algorithmname}{Algorithm}
\newcommand{\listalgorithmname}{List of Algorithms} \newlistof{listofalgorithms}{loa}{\listalgorithmname}
\newfloat{algorithm}{loa}{\algorithmname}
\newfixedcaption{\falgcaption}{algorithm}
\newlistentry{algorithm}{loa}{0}

\begin{document}
\tableofcontents
\newpage
\listoffigures
\listoftables
\listofalgorithms

\newpage
\chapter{Example Chapter}
\section{Example Section}

\begin{algorithm}[htbp]
\hrulefill
\caption{An Algorithm Template}
\label{alg1}
\hrulefill
\begin{algorithmic}
\REQUIRE $n \geq 0$
\ENSURE $y = x^n$ 
\STATE $y \leftarrow 1$
\STATE $X \leftarrow x$
\STATE $N \leftarrow n$
 \WHILE{$N \neq 0$}
 \IF{$N$ is even}
\STATE $X \leftarrow X \times X$
\STATE $N \leftarrow N / 2$
\ELSE[$N$ is odd]
 \STATE $y \leftarrow y \times X$
\STATE $N \leftarrow N - 1$
\ENDIF
\ENDWHILE
\end{algorithmic}
\hrulefill
\end{algorithm}
\end{document}

Best Answer

You can load the float package after having defined the new float with memoir macros:

\newcommand{\algorithmname}{Algorithm}
\newcommand{\listalgorithmname}{List of Algorithms}
\newlistof{listofalgorithms}{loa}{\listalgorithmname}
\newfloat{algorithm}{loa}{\algorithmname}
\newfixedcaption{\falgcaption}{algorithm}
\newlistentry{algorithm}{loa}{0}

\usepackage{float}
\floatstyle{ruled}
\restylefloat{algorithm}

Then remove the \hrulefill commands and you'll get the "traditional" format.

enter image description here

Related Question