11

I am trying to add some spacing above/below captions of algorithms using the algorithm2e package. Consider the following example:

\documentclass[a4paper]{article}

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

\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}

  \blindtext{}

  \begin{figure}[!h]
    \caption{Figure test...}
  \end{figure}

  \blindtext{}

\end{document}

You can see that the caption is squeezed in below the box containing the algorithm. There is no space separating the box from the caption and not enough space separating the caption from the following text. I would like to add the same amount of space above and below the caption, but I have not yet figured out how to do that: There seems to be a working solution for the algorithms package (see Algorithms package ignores caption package) , but I can't figure out how to make a \captionsetup apply to an algorithm2e caption. Any ideas about this?

10

To use the command \captionsetup you must have loaded the caption package...

Anyway, this can be done without it.

First of all, the spacing between the caption and the rest of the text is null because you are preventing the algorithm to float using H as a position specifier. Use h instead, which means "place it here" but keeps the algorithm a floating object (the same as h for figures and tables).

Then you can adjust the spacing between the body of the algorithm and its caption through the command \SetAlCapSkip.

In other words, substituting the line

\begin{algorithm}[H]

with

\begin{algorithm}[h]

and inserting

\SetAlCapSkip{1em}

in the preamble (adjust 1em to your needs) should be what you want.


Complete MWE

\documentclass[a4paper]{article}

\usepackage{blindtext}
\usepackage[linesnumbered,boxed]{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}

  \blindtext{}

  \begin{figure}[!h]
    \caption{Figure test...}
  \end{figure}

  \blindtext{}

\end{document} 

Output

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.