2

I'm wrtting in algorithm that has the length equal to one page under latex. the algorithm float to the next page , and a blank space is left after the paragraph that preceds that algorithm .

enter image description here

The used code is similar to the following :

\documentclass{article}

\usepackage{amsmath, amssymb}
\usepackage{pdflscape}
\usepackage[margin=0.5in]{geometry}

\usepackage{colortbl}
\usepackage[margin=1cm]{caption}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{float}
\usepackage{graphicx}
\graphicspath{{figures/}}

\usepackage{longtable}

\usepackage{algorithm}
\usepackage{algorithmic}

\usepackage{pdfpages}

\begin{document}


------Some paragraph --------


\begin{algorithm}
    \caption{ algorithm_title}
    \begin{algorithmic}
-----Some instructions ----
\end{algorithmic}
\end{algorithm}

\end{document}

Another view of document ( there is a picture before the paragraph ) :

enter image description here

I wish the question is clear. Thank you in advance for help !

11
  • 1
    tex.stackexchange.com/a/33869/134144 might be worth having a look at.
    – leandriis
    Jul 10, 2020 at 19:55
  • 1
    The algorithm environment is a "floating" environment; two other examples of floating environments are figure and table.
    – Mico
    Jul 10, 2020 at 20:05
  • 1
    If it takes more than 0.7\textheight, use the [p] option. Otherwise it has to wait for the next \clearpage. You could add it a page/column early. Jul 10, 2020 at 20:59
  • 1
    Can't you shrink your algorithm a bit? Or use the \small font?
    – pluton
    Jul 10, 2020 at 21:41
  • 1
    It does not seem to really change the problem: there is a figure, then a bit of text, then the algorithm (and no more text if I understand well). When the algo is called, there insufficient space and it flies to the next page: this is the expected behavior. How else could it be?
    – pluton
    Jul 10, 2020 at 21:54

2 Answers 2

5

A simpler MWE of the problem:

\documentclass{article}
\usepackage{lipsum}
%\renewcommand{\topfraction}{0.9}
\begin{document} 
\begin{figure}
\rule{10cm}{14cm}\end{figure}
\lipsum[1-50] 
\end{document}

The float is in the second page, even if there are only one word in the page, and apparently there are enough space. The problem is that there are not enough space for the float, according to some LaTeX directives. By default, a float should be placed at top, but only if the float height is <70% of the text height. Since this float is bigger, it is moved to a "page of floats" with different directives.

One way to change this behavior is relax one of these directives:

\renewcommand{\topfraction}{0.9}

Now the room for top floats could up to the 90% of the text height, so the float will appear at top of the first page.

Or you can just ignore the float directives:

\begin{figure}[!]

Another solution is change the default ([tp]) to [htp]:

\begin{figure}[htp]  % or just ...   \begin{figure}[h] 

Amusingly, with [t] the float cannot be at top, but it is possible only with [h] because \topfraction directive is not applicable to this option (even when "here" is really the top of the page).

For more information of how floats are placed in the document, see How to influence the position of float environments like figure and table in LaTeX?

5
  • @Fan , thank you for your help ! I'm sorry to say that none of the suggested solutions worked for me but at least i succeded in resolving the problem by resizing the picture before the paragraph. Also i dropped some returns in line and i used `\smal to scale some text and comments within the algorithm. I think that using the package algorithm , there is no way to split the created algo in two pages so \newpage will not work. I searched in previous similar question and i had found an example of splitting an algo . As a consequence , i will no more use the package \algorithm in future ! Thanks!
    – Tou Mou
    Jul 11, 2020 at 0:31
  • You could consider the question as solved ! Thank you all for your precious/valuable help and remarks !
    – Tou Mou
    Jul 11, 2020 at 0:32
  • 2
    The floats cannot be splited in any way. For your comment, I guess that you might be interested in the tcolorbox package, than can produce breakable boxes without floating.
    – Fran
    Jul 11, 2020 at 1:02
  • 2
    @TouMou If you consider the problem resolved, please accept it by clicking on the chek mark. What should I do when someone answers my question?
    – AndréC
    Jul 11, 2020 at 5:07
  • 1
    @AndréC , done !
    – Tou Mou
    Jul 11, 2020 at 12:06
0

I had found another possible solution , use the specified packages in the code and embeed the part you want to split in a new algorithm. In that case , just don't use a caption with this embeeded part and it will not be counted :

\documentclass{article}

\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{ifoddpage}

%% plz drop linesnumbered and vlined options if you don't like the vertical line and the numbering %% of lines 

\begin{document}


\begin{algorithm}
%% Algorithm 1: caption
\caption{algorithm\_1 : caption }
\LinesNumbered
 
a=0
a=a+1 

 
\end{algorithm}


 


\begin{algorithm}
 \State remaining algorithm 1 instructions here
  %% This algo will not be counted because it completes the algorithm 1 
\end{algorithm}


\begin{algorithm}
%% Algorithm 2 : caption
\caption{ Algo 2 : The algo number will be displayed as 2}
  \LinesNumbered
    \State algorithm instructions here
\end{algorithm}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .