3

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

Here is a screenshot:

enter image description here

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

1 Answer 1

3

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

enter image description here

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

Additional spacing parameters can be inserted, depending on your personal taste.

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.