16

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?

13

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.

  • 1
    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 '16 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 '16 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.