# Changing the default layout produced by algorithm and algorithmic packages

If the default output of the algorithm and algorithmic packages for the following code:

\begin{algorithm}
\begin{algorithmic}[1]
\PRINT \texttt{‘‘Hello, World!’’}
\end{algorithmic}
\end{algorithm}


Is:

Can this be changed so that the lines are removed and a caption be centrally displayed under the pseudocode such as:

Algorithm 4.3: Printing HelloWorld.

So that an algorithm is referenced by chapter number and then its number within that chapter, and that this format is also present in a List of Algorithms at the beginning of a document?

Thanks to the answer given below, I have been able to largely achieve what I want. But I notice that I can use the \print and \State instructions but if I try to use a \FOR like this:

\setcounter{algorithm}{0}
\begin{algorithm}
\begin{algorithmic}[1]
\State $pollingDateTime \leftarrow now - 1 hour$
\FOR{\texttt{each} $rssFeed$}
\print \texttt{‘‘Hello, World!’’}
\ENDFOR
\end{algorithmic}
\caption{Algorithm 1.}
\end{algorithm}


That this ends in a LaTeX Undefined control sequence. error. What can be done about this?

Thanks

Mr Morgan.

• The algorithms bundle provides \STATE, while algorithmicx provides \State. If you wish to use algorithmicx with the older algorithms syntax, you need to load \usepackage{algcompatible}.
– Werner
Sep 7, 2014 at 0:51

Yes, of course!

The main idea is to restyle the algorithm float construction. Since algorithm loads the float package, you can easily define your own float construction, say newruled:

\makeatletter
\newcommand\fs@newruled{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@plain% Caption style
\def\@fs@pre{\hrule height.8pt depth0pt \kern2pt}% Line at top of algorithm + 2pt gap
\def\@fs@post{}%\def\@fs@post{\kern2pt\hrule\relax}% Remove line below \caption
\def\@fs@mid{\kern2pt\hrule\kern2pt}% Line at bottom of algorithm
\let\@fs@iftopcapt\iffalse}% Place caption at bottom
\makeatother
\floatstyle{newruled}
\restylefloat{algorithm}


The 6 components of the float is defined within a \fs@<style> macro, which is called when you use \floatstyle{<style>}:

• \@fs@cfont defines the caption label font;

• \@fs@capt defines the way the caption is handled. float already defines a number of these (including plain, which I've used);

• \@fs@pre specifies whatever is precedes (or is printed before) your algorithm;

• \@fs@post specifies whatever is at the posterior of (or printed after) your algorithm float. In the regular setup, with a caption below a float, this macro comes after the caption;

• \@fs@mid is inserted above the caption and below the algorithm;

• \@fs@iftopcapt is a conditional for setting the caption at the top or at the bottom of the algorithm. If it's set to \iftrue, then the caption will be set at the top, otherwise (if set to \iffalse) the caption will end up at the bottom of the algorithm.

For adjusting the counter representation, the usual things are at play:

\renewcommand{\thealgorithm}{\thechapter.\arabic{algorithm}}
\makeatletter
\makeatother


We first adjust \thealgorithm to include \thechapter., and also reset the algorithm counter with every change in the chapter counter (using \@addtoreset).

\documentclass{report}
\usepackage{algorithm,algpseudocode}

\makeatletter
\newcommand\fs@newruled{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@plain% Caption style
\def\@fs@pre{\hrule height.8pt depth0pt \kern2pt}% Line at top of algorithm + 2pt gap
\def\@fs@post{}%\def\@fs@post{\kern2pt\hrule\relax}% Remove line below \caption
\def\@fs@mid{\kern2pt\hrule\kern2pt}% Line at bottom of algorithm
\let\@fs@iftopcapt\iffalse}% Place caption at bottom
\makeatother
\floatstyle{newruled}
\restylefloat{algorithm}

\algnewcommand{\print}{\State \textbf{print} }
\renewcommand{\thealgorithm}{\thechapter.\arabic{algorithm}}
\makeatletter
\makeatother

\begin{document}
\tableofcontents
\listofalgorithms
\setcounter{chapter}{3}% Just for this example

\chapter{A chapter}

\setcounter{algorithm}{2}% Just for this example
\begin{algorithm}
\begin{algorithmic}[1]
\print \texttt{Hello World!''}
\end{algorithmic}
\caption{Printing HelloWorld}
\end{algorithm}

\end{document}


Above I've used algpseudocode from the algorithmicx package, although you can stick to algorithmic (provided by the algorithms bundle.

For the numbering, it is enough to load the algorithm package with option chapter. On another hand, it accepts a number of commands from the caption package, in particular those pertaining to fonts and alignment — not skip.

Here is an example with the boxed style and a left-aligned caption:

\documentclass[12pt,leqno]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier}

\usepackage{xcolor}
\usepackage[fleqn]{mathtools}%
\usepackage{caption}
\DeclareCaptionFont{red}{\color{red}}
\captionsetup[algorithm]{font = small, labelfont = {sc, red}, singlelinecheck = off}
\usepackage[boxed, section]{algorithm}
\usepackage{algorithmic}

\begin{document}

\setcounter{section}{3}
\begin{algorithm}
\begin{algorithmic}[1]
\PRINT \texttt{‘‘Hello, World!’’}
\end{algorithmic}
`