# Squeezing space around algorithm construct

I am using algorithm2e for developing algorithm constructs in my LaTeX document. However, if I want to use [!t] or [!b] to place the algorithm construct in an appropriate place, sometimes the construct is going down to the last page instead of the current page. Therefore, I wanted to wrap it with a figure construct as follows to squeeze space after the algorithm construct by using squeezing functions such as \vpsace{-0.5cm} just between \end{algorithm} and \end{figure}. But it is giving the error: ! LaTeX Error: Not in outer par mode. Please help me to solve this.

\begin{figure}[!t]
\begin{algorithm}
....
...
\end{algorithm}
\end{figure}

-
You cannot wrap an algorithm within a figure environment, since both are floats. Floats inside floats is not allowed, producing your error. It's not the use of \vspace{-0.5cm} that produces the error. – Werner Aug 18 '11 at 19:46

@kkp: The algorithm environment provided by the algorithm2e package is a "floating" environment, just like table and figure floating environments are. Hence, it can't be wrapped inside another floating group. What you are encountering -- the fact that you can't get LaTeX to place the floats anywhere close to where you want them to go -- is a commonly shared frustration of many LaTeX users. My main suggestion is to check if the algorithm floats in question occupy well more than half a page. If that's the case, you may want to change some or all of the parameters \topfraction, \bottomfraction, \textfraction, and \floatpagefraction. In many of my documents, I have the following commands in the preamble:

\renewcommand\topfraction{0.85}
\renewcommand\bottomfraction{0.85}
\renewcommand\textfraction{0.1}
\renewcommand\floatpagefraction{0.85}


With these commands, you would instruct LaTeX to allow a float -- really, a group of floats -- to occupy up to 85% of a page that also contains some text. (If a float is larger than that, it'll end up on a page by itself.)

Another suggestion I'd make is not to specify the placement options [t!] and [h!], for if LaTeX cannot satisfy this demand immediately, somewhat perversely (and counter-intuitively) the float, and all subsequent floats of the same type (figure, table, or algorithm, will be pushed back all the way to the end of the document rather than just to the next suitable page.

Addendum: Discussion of how to reduce the space between text and floats:

To change these amounts of space, you could add the following instructions (or something similar) to your document's preamble:

\setlength\floatsep{1.25\baselineskip plus 3pt minus 2pt}
\setlength\textfloatsep{1.25\baselineskip plus 3pt minus 2pt}
\setlength\intextsep{1.25\baselineskip plus 3pt minus 2 pt}


The first length governs the separation of two adjacent floats, the second sets the separation between a float that's at the top (or bottom) of a page and the text below (above) it, and the third sets the separation between a float that's in the middle of a page and the text above and below it. As you can see from this example, I've set all three lengths to the same ("rubber") value. Unless you're really really pressed for space, I wouldn't reduce the lengths even further.

-

LaTeX places floats on pages based on the availability for that specific page. These availabilities are defined in terms of lengths or ratios.

In the documentation of the layouts package it displays the page-related quantities associated with float placement (see section 6 Float layouts):

Therefore, float placement is influenced by

• \topfraction (default is 0.699)
• \topnumber (default is 2)
• \textfraction (default is 0.199)
• \bottomfraction (default is 0.300)
• \bottomnumber (default is 1)
• \totalnumber (default is 3)

Changing these should motivate LaTeX to increase the number of floats in a specific region (like t or b), for example. Redefinition of \...number is done via \setlength, while \...fraction is modified using \renewcommand. See section 6.3 Changing the float layout in your document for more details on how this can be done. Modifying these settings may be very document-specific.

More layout options regarding the space between page components can also be modified. Here's a graphic from the same documentation displaying the important ones:

Modifying these lengths via \setlength adjusts the layout according to your preference.

-