# Algorithm caption?

I am writing my final thesis and I am not too practical working with LaTeX. In my document, I have to write some algorithms; my professor gave me the following code to put in the preamble:

\newcounter{algo}
\counterwithin{algo}{chapter}
\newcommand{\rs}{\textbf }
\newcommand{\ie}{i.e.\xspace}
\newcommand{\sfrac}[2]{{\textstyle\frac{#1}{#2}}}
\newcommand{\note}[1]{\marginpar{\footnotesize #1}}
\newcommand{\ignore}[1]{}
\newcommand{\diag}{\mathrm{diag}}
\newcommand{\algo}[3]{\refstepcounter{algo}
\begin{figure}[H]
\begin{center}
\framebox[\textwidth]{
\parbox{0.95\textwidth} {\vspace{\topsep}
{\bf Algorithm \thealgo : #2}\label{#1}\\
\vspace*{-\topsep} \mbox{ }\\
{#3} \vspace{\topsep} }}
\end{center}
\end{figure}}


I cannot really understand all the lines: in practice, this allows me to write an algorithm inside a box. Now, I have a few questions to modify some elements (I have checked on the Internet but couldn't find anything):

1. how can I add a label to an algorithm defined by \algo? I would like to cite it during my discussion and also create a list of algorithms at the beginning of the thesis. I don't need a caption, cause the first line of the algorithm already states the algorithm number and the title, I just need the label to refer to it in my document.
2. How can I center \algo? I mean, if I have a short \algo and want to put it on a blank page, it doesn't appear centered both orizontally and vertically. I understand this "box" is treated like a figure, but even though in the command there is written \begin{center} ... \end{center} it doesn't manage to do it. I tried to replace it by \centering but it still doesn't work.

If you know how to fix this and want to help me, I would really appreciate it. Regards to all.

To refer to the algorithm simply use the first argument of the \algo command which stores the label. \centering is useless since the box width is set to \textwidth, so it is always horizontally centered. If you change [H] to [p] You will notice that the algorithm will also be vertically centered. Anyway you can manually move the algorithm vertically with \vspace*{<dimen>} inside figure.

MWE

\documentclass{book}
\usepackage{chngcntr}
\usepackage{float}
\usepackage{graphicx}
\usepackage{hyperref}

\newcounter{algo}
\counterwithin{algo}{chapter}
\newcommand{\rs}{\textbf }
\newcommand{\ie}{i.e.\xspace}
\newcommand{\sfrac}[2]{{\textstyle\frac{#1}{#2}}}
\newcommand{\note}[1]{\marginpar{\footnotesize #1}}
\newcommand{\ignore}[1]{}
\newcommand{\diag}{\mathrm{diag}}
\newcommand{\algo}[3]{\refstepcounter{algo}
\begin{figure}[H]
\centering
\framebox[\textwidth]{
\parbox{0.95\textwidth} {\vspace{\topsep}
{\bf Algorithm \thealgo : #2}\label{#1}\\
\vspace*{-\topsep} \mbox{ }\\
{#3} \vspace{\topsep} }}
\end{figure}}

\begin{document}

\chapter{The algorithm}

See algorithm \ref{algo:label}

\algo{algo:label}{The basic algorithm}{Do this and then this}

\end{document}


which gives:

\documentclass{book}
\newcounter{algo}
\counterwithin{algo}{chapter}
\newcommand{\algo}[3]{\refstepcounter{algo}
{
\centering
\framebox[\textwidth]{
\parbox{0.95\textwidth} {\vspace{\topsep}
{\bf Algorithm \thealgo : #2}\label{#1}\\
\vspace*{-\topsep} \mbox{ }\\
{#3} \vspace{\topsep} }}
}}
\begin{document}
\vspace*{\fill}
\algo{label}{title}{algorithm}
\vspace*{\fill}
\newpage
\ref{test}
\end{document}


Given the mention of chapters I'm assuming a book class although this should work with other classes too. I've also removed the definitions that don't pertain to \algo.

I've removed the figure environment since you want manual placement rather than the automatic placement of figures. Both the center environment and the \centering command only centre horizontally as this is usually what is wanted. To start a new page simply use \newpage. So in your document you would have

\newpage
\vspace*{\fill}
\algo{alg:label}{title}{algorithm}
\vspace*{\fill}
\newpage


This produces the algorithm horizontally and vertically centred (centred on the text block which is may not be centred on the page). At another point in the document you can the reference this as Algorithm~\ref{alg:label} note that the alg prefix is not necessary but it is typically good practice to organise references like this.