26

I am using the usual algorithm package for a paper. However, LaTeX puts too much vspace below algorithms. I have already reduced \floatsep, \textfloatsep and \intextsep but it does not seem to help. For figures, I can use negative \vspaces after the figure to get less space after it. However, this does not work for algorithms as there is a line displayed below the algorithm. If I use a negative vspace in the algorithm environment, the line gets pulled into the pseudocode.

Thus, both answers which were posted for this question:

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

do not work.

How to fix this?

2

2 Answers 2

22

The layouts documentation (section 6.2 Detailed float layout, p 25) describes the possible lengths influencing the vertical space between floats and text body elements:

  • \textfloatsep - between top and bottom-aligned float and text body (default is 20\p@ \@plus 2\p@ \@minus 4\p@); and
  • \intextsep - between float and text for other floats (default is 12\p@ \@plus 2\p@ \@minus 2\p@).

The defaults for these are set in latex.ltx. Both these lengths have glue (meaning they can stretch/shrink). Here's a graphic from layouts showing the lengths involved.

enter image description here

Here's an incidental view on a change in \textfloatsep from the default to 0pt with an example from the algorithmicx package:

enter image description here

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\begin{document}
\lipsum[1-2]
\begin{algorithm}[t]
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
    \EndWhile\label{euclidendwhile}
    \State \textbf{return} $b$\Comment{The gcd is b}
    \EndProcedure
  \end{algorithmic}
  \caption{Euclid’s algorithm}\label{euclid}
\end{algorithm}
\lipsum[3-6]
\setlength{\textfloatsep}{0pt}% Remove \textfloatsep
\begin{algorithm}[t]
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
    \EndWhile\label{euclidendwhile}
    \State \textbf{return} $b$\Comment{The gcd is b}
    \EndProcedure
  \end{algorithmic}
  \caption{Euclid’s algorithm}\label{euclid}
\end{algorithm}
\end{document}

Note the visible difference in the vertical space below algorithm.

2
  • 2
    The main issue of using \textfloatsep is that it will set for the entire documents. So, if you add a figure after this command, this separation you've set will also affect the figure. What OP wanted, is a command that only affects the current float (such as \vspace). One way would be using \textfloatsep to the desired value before the algorithm, and setting it to the default value right after the algorithm. But, how to do that?
    – rph
    Dec 8, 2016 at 5:13
  • 3
    @rkioji: \newlength{\textfloatsepsave} \setlength{\textfloatsepsave}{\textfloatsep} \setlength{\textfloatsep}{0pt} ... \setlength{\textfloatsep}{\textfloatsepsave}. My suggestion would be to leave such things until the end of your document processing. Then, if you have a single float that you want to adjust, issue the appropriate negative \vspace to correct that particular float's spacing.
    – Werner
    Dec 8, 2016 at 5:37
2

An alternative approach is to place the algorithm inside a figure. Once inside a figure, you can adjust whitespace using \vspace{-1cm} (or similar), just as you would for other figures. One nice aspect of this approach is that it doesn't change the spacing around other figures.


Full example:

\begin{figure}[t]
\vspace{-5em}
\begin{algorithm}[H]
\begin{algorithmic}[1]
  \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
    \EndWhile\label{euclidendwhile}
    \State \textbf{return} $b$\Comment{The gcd is b}
  \EndProcedure
\end{algorithmic}
\end{algorithm}
\vspace{-5em}
\end{figure}

You must log in to answer this question.

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