# How to remove/change the vertical spacing before and after an 'algorithm' environment?

I saw a question that tells me how to change the spacing in an algorithm environment, but I don't know how to do remove/change the space before and after an algorithm environment like:

% remove the space here
\begin{algorithm}
\begin{algorithmic}
[...]
\end{algorithmic}
\end{algorithm}
% remove the space here


LaTeX has three length variables to control (i) the distance between two adjacent floating objects (such as figure, table, or algorithm objects), (ii) the distance between a float at the top (bottom) of a page and the text below (above) it, and (iii) the distance between an in-text float and the text above and below it; they are called \floatsep, \textfloatsep, and \intextsep, respectively. (LaTeX also has three more such variables to control the spacing above and/or below floats on floats-only pages; these are \@fptop, \@fpbot, and \@fpsep, respectively.)

To completely suppress the in-text separation of a float (not recommended, by the way!!), you'd type (in the preamble)

\setlength{\intextsep}{0pt}


i.e., you'd set \intextsep to a fixed length of 0 points. A better solution, if you're pressed for space (pun intended), would be to set

\setlength{\intextsep}{1\baselineskip}


Here's a MWE that uses the algorithm2e package:

\documentclass{article}
\usepackage{algorithm2e}
\newcommand{\lipsone}{Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
\newcommand{\lipstwo}{Donec vehicula augue eu neque. Pellentesque
habitant morbi tristique senectus et netus et malesuada fames ac
turpis egestas.}
\begin{document}

\subsubsection*{With default setting of \texttt{\textbackslash intextsep}}
\lipsone

\begin{algorithm}
\caption{A random example}
\SetAlgoLined
\KwData{Some input}[h]
\KwResult{Some output}
initialization\;
\While{not at end of this document}{read}
{go back to beginning\;}
\end{algorithm}

\lipstwo

\subsubsection*{After setting \texttt{\textbackslash intextsep} to 0pt}
\setlength{\intextsep}{0pt}
\lipsone

\begin{algorithm}
\caption{Another random example}
\SetAlgoLined
\KwData{Some input}
\KwResult{Some output}
initialization\;
\While{not at end of this document}{read}
{go back to beginning\;}
\end{algorithm}

\lipstwo

\end{document}


Okay, I solved it in an "ugly" way by adding a

\vspace*{-.4cm}


at the respective places. For my case, this is fine, since I only have one algorithm environment in my tex file, however if you want it automatically be done in all your algorithm environments, there should be another way.

I'm not allowed to comment as I don't have enough reputation points. To solve these issues, just add

\setlength{\AlCapSkip}{1em}


To the preamble and the caption to all Algorithm figures will move down without affecting other captions.

• Also, you can change 1em to other amounts in case you want a bigger or smaller gap – abdnChap Jan 31 '18 at 21:31
• No. What you're suggesting is specific to algorithm2e; this is different from the interface supplied by the algorithms bundle (which provides algorithm and algorithmic). It is also different from what is provided by algorithmicx. – Werner Jan 31 '18 at 22:22
• ...moreover, the request in this question is for spacing between the algorithm float and the surrounding text, not within the environment between the algorithm and the \caption. – Werner Jan 31 '18 at 22:29
• Ah, cool, thanks for clarifying – abdnChap Mar 4 '19 at 14:14
\begin{figure}[tb]


or

\begin{algorithm}[tb]


or

\begin{table}[tb]


This [tb] parameter works like charm with almost anything.

• Are you sure this answers the question? The [tb] parameter changes the placement of the float, but not directly the vertical spacing around it. – Vincent Mar 1 at 18:29