# Removing the space of caption when using algorithm

I have something like this:

\usepackage{algorithm}
\usepackage{algpseudocode}

...
\begin{document}
\begin{algorithm}[H]
\begin{algorithmic}[1]
\Function {function}{a}
\State a = a + 1
\EndFunction
\end{algorithmic}
\end{algorithm}
\end{document}


I'm using [H] because I want to set the position of my code. But after doing that a space for caption appears and I don't want it and now there are to horizontal lines beafore my code. You can see what I mean in this picture:

How can I remove one of those lines?

Thanks!

• Welcome to TeX.SX! Why do you use the float if you force it in bad style to be where it is in code and you do not want to caption it? Just remove your algorithm environment. – TeXnician Apr 1 '17 at 16:32
• Because I want it to be between lines. Sorry if this is a bad practice but it's my first time using these packages. @TeXnician – Néstor Apr 1 '17 at 16:38

Define your own version:

\documentclass{article}
\usepackage{algpseudocode}

\usepackage{lipsum} % for the example

\newenvironment{algo}
\centering
\begin{minipage}{\linewidth}
\hrule\kern2pt}
{\par\kern2pt\hrule
\end{minipage}

\begin{document}

\lipsum[2]

\begin{algo}
\begin{algorithmic}[1]
\Function {function}{a}
\State a = a + 1
\EndFunction
\end{algorithmic}
\end{algo}

\lipsum[3]

\end{document}


In order to achieve this, you need to define a new \floatstyle. Here's one such option (I called it nocaptionruled):

\documentclass{article}

\usepackage{algorithm,algpseudocode}

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

\begin{document}

\begin{algorithm}[H]
\begin{algorithmic}[1]
\Function{function}{a}
\State a = a + 1
\EndFunction
\end{algorithmic}
\end{algorithm}

\floatstyle{nocaptionruled}
\restylefloat{algorithm}

\begin{algorithm}[H]
\begin{algorithmic}[1]
\Function{function}{a}
\State a = a + 1
\EndFunction
\end{algorithmic}
\end{algorithm}

\end{document}

• This works, but if after my code i use another code without using [H] the only one line is missed, any solution? – Néstor Apr 1 '17 at 16:44
• @n23: So you have two algorithmic environments in a single algorithm? – Werner Apr 1 '17 at 17:18
• No, nevermind, @egreg solve it! – Néstor Apr 1 '17 at 17:24