[Tex/LaTex] Algorithm2e and hanging captions

algorithm2ecaptions

I'm having issues trying to make the captions in Algorithn2e hang, as in the table caption pictured below. I plan on using them together with arsclassica/classicthesis, but it seems neither of these packages affect the issue.

enter image description here

I have tried using \usepackage[format=hang]{caption} and \captionsetup{format=hang}, but algorithm2e seems not to be affected by them. The classicthesis style file seems to be achieving this effect exactly this way. The algorithm2e documentation doesn't mention hanging captions and I couldn't find anything (that I would decypher as caption code) in the style file either. Finally, google also didn't yield anything and I am finding myself a little bit out of depth with my latex skills here.

Here's a MWE:

\documentclass{scrreprt}

\usepackage[american]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[algoruled]{algorithm2e}
%\usepackage{classicthesis}
%\usepackage{arsclassica}

% these two lines don't make a difference   
\usepackage[format=hang]{caption}   
\captionsetup{format=hang}

\begin{document}

\begin{algorithm}[htb]
    \While{true}
    {   
    }
    \caption{Morbi blandit vehicula leo a consectetur. Aliquam a lacus posuere, consectetur tortor at, mollis erat.}
\end{algorithm}

\end{document}

I have been able to format the rest of the algorithm2e caption with some trial and error and hacking. I'm only interested in the hanging part of the problem here.

Best Answer

Here's one possible solution:

\documentclass{scrreprt}
\usepackage[american]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[algoruled]{algorithm2e}
\usepackage[format=hang]{caption}   
\usepackage{etoolbox}
\usepackage{newfloat}

\usepackage{classicthesis}
\usepackage{arsclassica}

% Un-define ''algorithm'' as defined by the algorithm package
\let\algorithm\relax
\let\endalgorithm\relax
\expandafter\let\csname algorithm*\endcsname\relax
\expandafter\let\csname endalgorithm*\endcsname\relax
\let\listofalgorithms\relax

% Define the new algorithm environment
\DeclareFloatingEnvironment{algorithm}

\makeatletter
% Definition for the style ruled
% First add the rules enclosing the captiom
\DeclareCaptionFormat{algrule}{%
  \rule{\textwidth}{0.8pt}\par
  {\@hangfrom{#1#2}%
     \advance\caption@parindent\hangindent
     \advance\caption@hangindent\hangindent
     \caption@@par#3\par}%
  \vspace*{-8pt}\rule{\textwidth}{0.4pt}\vspace*{-5pt}     
}
% Now add the rules after the algorithm
\AtEndEnvironment{algorithm}{\vspace*{-9pt}\rule{\textwidth}{0.4pt}}

% Apply the new format to the algorithm environment
\captionsetup[algorithm]{format=algrule}

% Settings for the list of algorithms
\let\oldlistofalgorithms\listofalgorithms
\renewcommand\listofalgorithms{
\begingroup
\renewcommand{\cftfigpresnum}{\algorithmname~}
\oldlistofalgorithms
\clearpage
\endgroup
}
\makeatletter

\begin{document}

\listofalgorithms

\begin{algorithm}[htb]
\caption{Morbi blandit vehicula leo a consectetur. Aliquam a lacus posuere, consectetur tortor at, mollis erat.}
    \While{true}
    {   
    }
\end{algorithm}

\begin{figure}
\centering
\Huge
A
\caption{Morbi blandit vehicula leo a consectetur. Aliquam a lacus posuere, consectetur tortor at, mollis erat.}
\end{figure}

\begin{table}
\centering
\Huge
A
\caption{Morbi blandit vehicula leo a consectetur. Aliquam a lacus posuere, consectetur tortor at, mollis erat.}
\end{table}

\end{document}

An image of the list of algorithms:

enter image description here

An image of the document showing the new environment in action:

enter image description here

The algorithm environment defined by algorithm2e is defined internally using the float package, so it's behaviour is not exactly the same as the one for standard floats and the environment is not 100% compatible with some of the features of the caption package.

One possible solution is then to un-define the algorithm environment defined by the algorithm2e package and define a new algorithm float with the help of the newfloat package. This new environment is fully compatible with the caption package, so one can use all the features offered by the package; in particular, settings like the hanging indentation will now apply directly (notice how nicely the new object inherits the settings from classicthesis/arsclassica).

An algrule format was used to imitate the look and feel of the ruled style of the original environment. The \caption command inside the new algorithm environment has to be used before the actual algorithm.

The solution also takes care of producing a "List of Algorithms" following the settings of the other standard lists.

The procedure used is, in fact, recommended in the documentation of the caption package:

Note: Only one single caption can be typeset inside environments defined with \newfloat or \restylefloat, furthermore these environments are not behaving exactly like the pre-defined floats figure and table. As a consequence many packages do not cooperate well with these.

Furthermore the float package has some caveats & limitations, so if you just want to define a new simple floating environment—behaving like figure or table—I recommend using \DeclareFloatingEnvironment offered by the newfloat package instead.