Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
You MWE, verbatim as it is, errors out with !LaTeX Error: Command \listofalgorithms already defined. Or name of \end... illegal. What am I doing wrong? Thanks! –  Jan Benes Apr 27 at 15:35
    
@JanBenes I had forgotten to also un-define the original list. I've fixed the code now. –  Gonzalo Medina Apr 27 at 15:38
    
Thanks, works like a charm. I'll just remark that the position of the \caption statement within the algorithm matters, as I just found out. Great answer, much appreciated! –  Jan Benes Apr 27 at 15:49
    
@JanBenes Ah, you're right. I forgot to mention this in my answer. Will do it right now. –  Gonzalo Medina Apr 27 at 15:50
    
@JanBenes I updated my answer providing now the proper settings for an eventual "List of Algorithms"; the new information might be of interest for you. –  Gonzalo Medina Apr 27 at 16:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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