Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
add comment

I'm very happy with algorithm2e and memoir. But I had to add the following in the body of the text for the TOC (the text is is Spanish, that is why I had to reset the names, babel doesn't handle it, the spacing is to make the list of algorithms use the same spacing as the one adjusted for the other lists of...):

%%%
%%% For algorithm2e
%%%

\SetAlgorithmName{Algoritmo}{Algoritmo}{Índice de algoritmos}
\newlistof{listofalgorithms}{loa}{\listalgorithmcfname}
\makeatletter
\renewcommand{\l@algocf}{\@dottedtocline{1}{1em}{3.5em}}
\makeatother
share|improve this answer
    
I couldn't get this to work. If you could maybe provide a full example instead. –  entropy Feb 20 '13 at 9:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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