[Tex/LaTex] Using algorithm2e in figure environment

algorithm2efloatsieeetran

I am using algorithm2e to explain my algorithm and surrounding it within float environment for IEEETran class as suggested in How to use the algorithm2e package with IEEEtran class?

Below is a sample code:

\documentclass[conference]{IEEEtran}
\usepackage[font=scriptsize]{caption}
\usepackage{subcaption}
\usepackage[plain, vlined, linesnumbered, noresetcount]{algorithm2e}
\makeatletter
\newcommand{\removelatexerror}{\let\@latex@error\@gobble}
\makeatother

\begin{document}
    \begin{figure}
        \removelatexerror
        \begin{algorithm}[H]
            \textbf{struct} Node \{ \\
            \Indp
            \textbf{Int} key;\\
            Node* child[2];\\
            \Indm
            \}; \\      
        \end{algorithm}
        \hrule
        \captionsetup{justification=centering}
        \caption{Node structure}
        \label{alg:NodeStructure}
    \end{figure}

    Figure \ref{alg:NodeStructure} represents a node in our tree
\end{document}

After I compile this code, I see a significant gap between the text and my algorithm. Is there any way to reduce this gap.
enter image description here

Best Answer

This is not particular to algorithm2e, but more so to do with the natural gaps between floats and the surrounding text as laid out by the class. Here is a quick view on the lengths involved in the separation:

enter image description here

The default lengths for IEEEtran are

  • \floatsep: 0.85\baselineskip plus 0.2\baselineskip minus 0.2\baselineskip

  • \textfloatsep: 1.55\baselineskip plus 0.2\baselineskip minus 0.4\baselineskip

  • \intextsep: 0.85\baselineskip plus 0.2\baselineskip minus 0.2\baselineskip

These are all fairly large, but allow for some manoeuvring within a two-column layout (due to the plus and minus). Here's the view with \textfloatsep set to 0pt:

enter image description here

\documentclass[conference]{IEEEtran}

\usepackage[plain, vlined, linesnumbered, noresetcount]{algorithm2e}

\makeatletter
\newcommand{\removelatexerror}{\let\@latex@error\@gobble}
\makeatother

\begin{document}

\begin{figure}
  \removelatexerror
  \begin{algorithm}[H]
    \textbf{struct} Node \{ \\
    \Indp
    \textbf{Int} key;\\
    Node* child[2];\\
    \Indm
    \};
  \end{algorithm}
  \hrule
  \caption{Node structure}
\end{figure}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. In finibus est in pulvinar semper. 
Nullam a mauris nibh. Aliquam egestas quam at vulputate condimentum. Suspendisse quis velit eros.

\newpage

\setlength{\textfloatsep}{0pt}

\begin{figure}
  \removelatexerror
  \begin{algorithm}[H]
    \textbf{struct} Node \{ \\
    \Indp
    \textbf{Int} key;\\
    Node* child[2];\\
    \Indm
    \};
  \end{algorithm}
  \hrule
  \caption{Node structure}
\end{figure}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. In finibus est in pulvinar semper. 
Nullam a mauris nibh. Aliquam egestas quam at vulputate condimentum. Suspendisse quis velit eros.

\end{document}