[Tex/LaTex] Space between algorithm and text

algorithm2espacing

I'm using algorithm2e to write algorithms. I can't figure out how to place some space (a line would be enough) between my text and the algorithm environment above and below after the caption. here's my code:

\documentclass[a4paper]{article}
\usepackage{algorithm2e}
\usepackage{float}
%\usepackage[font={small,it}]{caption}
\usepackage[font=small,labelfont=bf,
   justification=justified,
   format=plain]{caption}
\begin{document}
We can finally state the algorithm:
%space needed
\begin{algorithm}[H]
\While{$(k<niter)$}{
 do something
}
\caption{Topology optimization algorithm}
\end{algorithm}
%space needed
\noindent Moreover some text:
\end{document}

I need to use [H] instead of [h] otherwise it place it after the "Moreover".

Best Answer

Just add a center environment:

\documentclass[a4paper]{article}

\usepackage{algorithm2e}
\usepackage{float}
%\usepackage[font={small,it}]{caption}
\usepackage[font=small,labelfont=bf,
   justification=justified,
   format=plain]{caption}

\begin{document}
We can finally state the algorithm:
\begin{center}
\begin{algorithm}[H]
\While{$(k<\mathit{niter})$}{
 do something
}
\caption{Topology optimization algorithm}
\end{algorithm}
\end{center}
Moreover some text:

\end{document}

Or “abuse” a math display (this ensures no page break can happen before the algorithm)

\documentclass[a4paper]{article}

\usepackage{algorithm2e}
\usepackage{float}
%\usepackage[font={small,it}]{caption}
\usepackage[font=small,labelfont=bf,
   justification=justified,
   format=plain]{caption}

\begin{document}
We can finally state the algorithm:
\[
\begin{minipage}{\displaywidth}
\begin{algorithm}[H]
\While{$(k<\mathit{niter})$}{
 do something
}
\caption{Topology optimization algorithm}
\end{algorithm}
\end{minipage}
\]
Moreover some text:

\end{document}

enter image description here

On the other hand, if the algorithm needs to have a caption, it can be made a floating object. Using a cross reference makes the text independent of the final position of the float.

\documentclass[a4paper]{article}

\usepackage{algorithm2e}
%\usepackage[font={small,it}]{caption}
\usepackage[font=small,labelfont=bf,
   justification=justified,
   format=plain]{caption}

\begin{document}

We can finally state the algorithm, displayed as Algorithm~\ref{algo:toa}.

\begin{algorithm}[htp]
\While{$(k<\mathit{niter})$}{
 do something
}
\caption{Topology optimization algorithm\label{algo:toa}}
\end{algorithm}

Moreover some text:

\end{document}