5

My desired output is similar to the example given in this answer, in that I want the caption below the algorithm and outside the box, except I want the algorithm included as a figure (so I use \usepackage[figure,boxed]{algorithm2e}).

Currently I have something like this:

enter image description here

The MWE:

\documentclass[a4paper]{article}

\usepackage{blindtext}
\usepackage[linesnumbered,boxed,figure]{algorithm2e}

\SetAlCapSkip{1em}

\SetKwInput{KwInput}{Input}
\SetKwInput{KwOutput}{Output}

\begin{document}

  \begin{algorithm}[h]
    \SetAlgoLined
    \KwInput{Parameters...}

    \KwOutput{$\emptyset$}

    \If{Condition}
    {
      Do something \;
    }
    \caption{Algoritm test...}
  \end{algorithm}


\end{document} 
2

1 Answer 1

6

One way to go is to set the algorithm as a non-floating object (use H instead of h) and use a normal figure environment instead of the option figure when loading algorithm2e.

MWE:

\documentclass[a4paper]{article}

\usepackage{blindtext}
\usepackage[linesnumbered,boxed]{algorithm2e}

%\SetAlCapSkip{1em}

\SetKwInput{KwInput}{Input}
\SetKwInput{KwOutput}{Output}

\begin{document}

\begin{figure}[h]
  \begin{algorithm}[H]
    \SetAlgoLined
    \KwInput{Parameters...}

    \KwOutput{$\emptyset$}

    \If{Condition}
    {
      Do something \;
    }
  \end{algorithm}
  \caption{Algoritm test...}
\end{figure}

\end{document} 

Output:

enter image description here

5
  • does it need !H or just H? Nov 15, 2013 at 6:30
  • @adifferentben just H Nov 15, 2013 at 6:33
  • As a supplement: the !H in \begin{algorithm}[!H] in my case (using algorithmicx) raises an error Not in outer par mode. H works well.
    – hengxin
    Oct 30, 2014 at 13:23
  • This does not work for a twocolumn environment.
    – valentin
    Nov 13, 2018 at 13:48
  • Maybe this should be a new question. But I find placing the figure correctly (with the caption nicely spaced as you do here) much easier when I put the algorithm in a figure like you do here. But I want the caption to read Algorithm 1 and use the algorithm counter. Is this possible?
    – Kvothe
    Mar 24, 2021 at 11:00

You must log in to answer this question.

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