@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.
I thought I would show an extreme case.
\documentclass[twoside,open=right,12pt,a4paper,abstraction,BCOR=15mm,bibliography=totoc,toc=indentunnumbered]{scrreprt}
\usepackage{lettrine}
\usepackage{lmodern}
\usepackage{tocloft}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[normalem]{ulem}
\def\topfraction{.95}
\def\textfraction{.05}
\begin{document}
some text
\par\rule{1pt}{.95\textheight}\par
\noindent
some text that goes right till the end of the page
\begin{figure}[tb]
\centering
\captionsetup{labelfont=bf}
\includegraphics[width=\textwidth,height=.85\textheight]{example-image}
\caption{The figure appears on the top of the second page, which ist perfect.}\label{fig:FCC}
\end{figure}
\par\bigskip
\noindent
Some other text that appears on the third page although there is enough space below the figure/caption.
\end{document}
Best Answer
Just add a
center
environment:Or “abuse” a math display (this ensures no page break can happen before the algorithm)
On the other hand, if the algorithm needs to have a caption, it can be made a floating object. Using a cross reference makes the text independent of the final position of the float.