# Reducing space between 2 algorithms using algorithm2e

Is there any way to reduce the space between two algorithms?

Here is a screenshot:

\documentclass{article}

\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\begin{document}

\begin{algorithm}
\label{alg:my-alg}
\DontPrintSemicolon
\caption{My algorithm}
\SetKwInOut{Input}{Input}\SetKwInOut{Output}{Output}
\Input{$a,b,...$}
\Output{$R$}
statment1\;
statment2\;
statment3\;
\tikzmark{start}\lIf{$a>b$}{\KwRet{$a$\tikzmark{stop}}}\;
\KwRet{$b$}
\begin{tikzpicture}[remember picture, overlay]
\draw[red,thick] ([yshift=0.5ex]pic cs:start) -- ([yshift=0.5ex]pic cs:stop);
\end{tikzpicture}%
\end{algorithm}
\begin{algorithm}
\label{alg:my-alg}
\DontPrintSemicolon
\caption{My algorithm}
\SetKwInOut{Input}{Input}\SetKwInOut{Output}{Output}
\Input{$a,b,...$}
\Output{$R$}
statment1\;
statment2\;
statment3\;
\tikzmark{start}\lIf{$a>b$}{\KwRet{$a$\tikzmark{stop}}}\;
\KwRet{$b$}
\begin{tikzpicture}[remember picture, overlay]
\draw[red,thick] ([yshift=0.5ex]pic cs:start) -- ([yshift=0.5ex]pic cs:stop);
\end{tikzpicture}%
\end{algorithm}

\end{document}


Usually when you place an algorithm it is treated as a float and the spacing between floats depend on a number of factors discussed in How to change the spacing between figures/tables and text? However, in some cases it's convenient to have multiple floats move together (always). This is supported in the following way: Please all the algorithms as non-floating blocks (using the [H] float specifier) inside another float (like figure).

\documentclass{article}

\usepackage[ruled]{algorithm2e}

\begin{document}

\begin{figure}
\begin{algorithm}[H]
\caption{An algorithm}
A step in the algorithm
\end{algorithm}

% Additional spacing can be inserted here (like \bigskip, for example)

\begin{algorithm}[H]
\caption{A different algorithm}
Another step in this different algorithm
\end{algorithm}
\end{figure}

\end{document}


• It did work!!! I tried with \begin{figure} but it was throwing an error because [H] was missing. Thank you! – Delan Oct 27 '19 at 6:22