4

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:

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

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

3

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

Depending on what kind of space you're after exactly, you have some options.

  1. Leave a blank line between the text and the algorithm. This will insert a \parskip:

    We can finally state the algorithm:
    
    \begin{algorithm}[H]
      \While{$(k < n)$}{
        do something
      }
      \caption{Topology optimization algorithm}
    \end{algorithm}
    
    \noindent Moreover some text:
    
  2. Manually insert a \vspace{<len>} between the text and the algorithm, ensuring that you are in vertical mode. That is, also leave a blank line between the two or insert an explicit \par.

    \noindent
    We can finally state the algorithm:
    
    \vspace{\baselineskip}% Insert a blank line
    
    \begin{algorithm}[H]
      \While{$(k < n)$}{
        do something
      }
      \caption{Topology optimization algorithm}
    \end{algorithm}
    
    \vspace{\baselineskip}% Insert a blank line
    
    \noindent Moreover some text:
    
  3. Automate (2) using environ:

    \usepackage{environ}
    
    \let\oldalgorithm\algorithm
    \let\endoldalgorithm\endalgorithm
    
    \RenewEnviron{algorithm}[1][htbp]{%
      \par\vspace{\baselineskip}
      \begin{oldalgorithm}[#1]
        \BODY
      \end{oldalgorithm}
      \par\vspace{\baselineskip}
      \ignorespacesafterend
    }
    

Using [H] and a blank line (or a vertical skip) may cause the text to separate from the algorithm around a page/column break.

enter image description here

\documentclass{article}

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

\usepackage{environ}

\let\oldalgorithm\algorithm
\let\endoldalgorithm\endalgorithm

\RenewEnviron{algorithm}[1][htbp]{%
  \par\vspace{\baselineskip}
  \begin{oldalgorithm}[#1]
    \BODY
  \end{oldalgorithm}
  \par\vspace{\baselineskip}
  \ignorespacesafterend
}

\begin{document}

\noindent
We can finally state the algorithm:

\vspace{\baselineskip}% Insert a blank line

\begin{algorithm}[H]
  \While{$(k < n)$}{
    do something
  }
  \caption{Topology optimization algorithm}
\end{algorithm}

\noindent Moreover some text:

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.