How to remove/change the vertical spacing before and after an 'algorithm' environment?
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,
adipiscing vitae, felis.}
\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.