[Tex/LaTex] Removing the space of caption when using algorithm

algorithms

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: enter image description here

How can I remove one of those lines?

Thanks!

Best Answer

Define your own version:

\documentclass{article}
\usepackage{algpseudocode}

\usepackage{lipsum} % for the example

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

\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}

enter image description here

Related Question