[Tex/LaTex] Squeezing space around algorithm construct

algorithm2espacing

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}

Best Answer

@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.