0

I want the floating block used in algorithm2e at the page's top instead of the middle. Here is my sample code with this Tex SX link's help:

\documentclass{article}

\usepackage{lipsum,mathtools}
\usepackage[noend,ruled,lined]{algorithm2e}
\usepackage{hyperref}
\usepackage{booktabs}
\usepackage{float}

\makeatletter
\setlength{\@fptop}{0pt}
\setlength{\@fpbot}{0pt plus 1fil}
\makeatother

\setlength{\intextsep}{13pt}

\newenvironment{SpacingAlgorithm}[2]{
    \begin{algorithm}[ht]
        \caption{#1}\label{alg:#2}
}
{
    \end{algorithm}
}

\begin{document}
\lipsum[1]
\begin{SpacingAlgorithm}{1}{1}
\If{$1<2$}{
test
}
\end{SpacingAlgorithm}

\lipsum[1]\par
\lipsum[1]\par

\begin{itemize}
    \item test
\end{itemize}
% \clearpage

\begin{SpacingAlgorithm}{2}{2}
\If{$1<2$}{
test
}
\end{SpacingAlgorithm}
\begin{SpacingAlgorithm}{3}{3}
\If{$1<2$}{
test
}
\end{SpacingAlgorithm}
\begin{itemize}
    \item test
\end{itemize}

\end{document}

Then the pdf shows (Here I posted one small image to be better viewed for the mobile user):

enter image description here

Then I followed this Tex SX link, but it will make the contents after \begin{SpacingAlgorithm} also in one new page which may be not convenient if the floating block is very big causing one big blank area in the leaving page (See the above commented % \clearpage).

I also read this Tex SX link, but the H option in the \usepackage{float} is not available for algorithm block.

Q: How to make the above two algorithm blocks more condensed at the new page, i.e. not having one large blank area between them?

Edited:

After viewing this comment

  1. I checked the doc. The H option in the \usepackage{float} can't be combined with other parameters. Also, although H will make the above algorithm blocks condensed, I found the above \setlength{\intextsep}{13pt} won't work with it although the doc doesn't say it explicitly, but I need the \setlength{\intextsep}{13pt} feature.
  2. I updated the above codes by removing ! and others, the figure is still same as before.
4
  • 1
    ! means ignore the constraints so it's hard to do force any behaviour while you have !. Also if H is defined it is defined for all float types including algorithms. Nov 17, 2023 at 15:04
  • Thanks for your comment. I edited the question after using your advice. Hope it is more clarified now.
    – An5Drama
    Nov 17, 2023 at 15:34
  • With the above comment's help and the answer of another question asked by me, I found one workaround to implement the \setlength{\intextsep}{13pt} feature with H option. I use \par\addvspace{10pt} \begin{algorithm}[H] and \end{algorithm} \vspace{10pt}, although maybe it is not one good solution for this specific problem. The modified codes can be seen here. Hope that someone can improve my solution. Thanks in advance.
    – An5Drama
    Nov 18, 2023 at 0:35
  • After viewing this reference Tex SX link referred to by many Tex SX links, I viewed the ltoutput.dtx file and found the \makeatletter\setlength{\@fptop}{0pt}\setlength{\@fpsep}{3pt}\makeatother can solve the above problem. See the codes which is a bit different from the above in my 2nd comment. Hope this can help the future readers.
    – An5Drama
    Nov 19, 2023 at 12:32

1 Answer 1

0

I found two solutions to this problem which is shown in my 2nd comment and the 3rd with the David Carlisle's help. If anyone has one better solution or find some problems with my solutions, welcome to help improving this answer.

You must log in to answer this question.

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