7

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".

3 Answers 3

4

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

Replace the lines with comments between the text and the algorithm by blank lines and use \SetAlgoSkip{bigskip} before the algorithm or in preamble.


Explanation

algorithm2e provides \SetAlgoSkip to add spaces before and after the algorithm.

There are four possibilities:

  • \SetAlgoSkip{} for no extra vertical skip
  • \SetAlgoSkip{smallskip} to act as the default behaviour
  • \SetAlgoSkip{medskip} to have a bigger skip
  • \SetAlgoSkip{bigskip} to have the bigger skip

You can also define a command with a custom skip and pass its name (without \) to \SetAlgoSkip.

Examples

Using \SetAlgoSkip{bigskip}:

\documentclass[a4paper]{article}

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

\begin{document}
We can finally state the algorithm:

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

\noindent Moreover some text:
\end{document}

With bigskip


Using custom spacing (defining new command):

\documentclass[a4paper]{article}

\usepackage{algorithm2e}
\usepackage{float}
%\usepackage[font={small,it}]{caption}
\usepackage[font=small,labelfont=bf,
  justification=justified,
  format=plain]{caption}
   
\newcommand{\SkipBeforeAndAfter}{\vspace{1cm}}  % <------ added

\begin{document}
We can finally state the algorithm:

\SetAlgoSkip{SkipBeforeAndAfter} % <------ added (without \)
\begin{algorithm}[H]
\While{$(k<niter)$}{
 do something
 }
\caption{Topology optimization algorithm}
\end{algorithm}

\noindent Moreover some text:
\end{document}

Custom spacing

Extra

If you want to define different spaces for before and after the algorithm, one possibility is to do the following:

\documentclass[a4paper]{article}

\usepackage{algorithm2e}
\usepackage{float}
%\usepackage[font={small,it}]{caption}
\usepackage[font=small,labelfont=bf,
  justification=justified,
  format=plain]{caption}
   
\usepackage{etoolbox}% <---- needed for this solution
\providetoggle{beginFlag}
\settoggle{beginFlag}{true}
\newcommand{\SkipBeforeAndAfter}{%
    \iftoggle{beginFlag}{%
        \settoggle{beginFlag}{false}%
        \vspace{\baselineskip}% <---- initial space (before)
    }{%
        \settoggle{beginFlag}{true}%
        \vspace{3\baselineskip}% <---- final space (after)
    }%
}
    
\SetAlgoSkip{SkipBeforeAndAfter}

\begin{document}
We can finally state the algorithm:

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

\noindent Moreover some text:
\end{document}

extra

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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .