1

I have several algorithms written down in a single file and would like latex to spread them across the document to save spaces. For example an algorithm can be just 3 or 4 lines and another can be 20 to 30 lines. So how do I tell latex to squeeze the smaller algorithms among the text in a document.

\documentclass{llncs}
\usepackage{llncsdoc}
\usepackage{lipsum}
\usepackage[ruled,vlined,linesnumbered,noresetcount]{algorithm2e}
\AlgoDisplayBlockMarkers 
\begin{document}
\lipsum
\begin{algorithm}
\caption{alg1\label{1}} 
...\;
...\;
...\;
...
\end{algorithm}
\begin{algorithm}
\caption{alg2\label{2}} 
...\;
...\;
...
\end{algorithm}
\begin{algorithm}
\caption{alg3\label{3}} 
...\;
...\;
...\;
...
\end{algorithm}
\begin{algorithm}
\caption{alg4\label{4}} 
...\;
...\;
...\;
...\;
...
\end{algorithm}
\lipsum
\end{document}
arunmoezhi
  • 1,270
  • 2
    This happens by default that LaTeX floats the algorithm around to best-fit the page layout. There are some parameters that influence the placement though (see How to influence the position of float environments like figure and table in LaTeX?). – Werner Jul 21 '14 at 23:28
  • ...since this is the default and you must be experiencing something to the contrary, could you explain a little more as to what is going on? – Werner Jul 21 '14 at 23:57
  • @Werner As I read the question, the OP wants LaTeX to reorder the algorithms if doing so will save space but, as I understand it, LaTeX does not do that with floats of a single kind? – cfr Jul 22 '14 at 00:59
  • @Werner: If I specify {algorithm}[h] then it interleaves them with text, but if I just specify {algorithm} it puts all the algorithms together. – arunmoezhi Jul 22 '14 at 17:51
  • Latex preserves the order in which the algorithms are written. Actually I dont care the order in which they are displayed on the paper. So is there a way to tell latex to ignore the order. – arunmoezhi Jul 22 '14 at 17:53
  • @arunmoezhi: Ignore the order and just attempt to optimize their placement? No, not really. LaTeX gathers them in the order you place them in the text, and flushes them as needed (in that order) as it constructs the pages. As such, you can attempt the optimization manually (which isn't really sensible). – Werner Jul 22 '14 at 18:35

1 Answers1

1

This is not really an answer, but I have a feeling no one else is about to propose one soon. This comes as a collection of the previous comments on the question.

The quick answer is no, it is not possible to influence the order of algorithms, to optimize overall whitespace around them.

The long answer is, as pointed out by Werner not limited to algorithms but more generally to floats. This is very well explained by Frank Mittlebach in his extensive answer https://tex.stackexchange.com/a/39020/141947.

I will only reproduce part of his answer here for the sake of eas acces to the precise information I believe is relevant here :

For floats, this means that the algorithm is greedy, i.e., the moment it encounters a float it will immediately try to place it and, if it succeeds, it will never change its decision. This means that it may choose a solution that could be deemed to be inferior in the light of data received later on.

BambOo
  • 8,801
  • 2
  • 20
  • 47