2

I am trying to left-align the caption of algorithms of algorithm2e to match the captions of the figures.

Taking a look at Align Caption to the left , here is my code so far:

\documentclass[12pt,a4paper,oldfontcommands]{memoir}
\usepackage{caption}
\usepackage[boxed,vlined,resetcount,algochapter]{algorithm2e}
\SetAlgoCaptionSeparator{:}
\renewcommand\AlCapFnt{\normalfont}
\SetAlCapSkip{\abovecaptionskip\relax}
\captionsetup[algorithm]{justification=raggedright,singlelinecheck=off}   
\begin{document}
\chapter{ }

\begin{figure}[h]

\caption{Here is a long caption for an algorithm.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.}

\end{figure}

\begin{algorithm}
Some algorithm step \;
\caption{Here is a long caption for an algorithm.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.}
\end{algorithm}

\end{document}

This results in enter image description here

One solution candidate was to use the figure parameter as explained in Adjusting the caption of an algorithm2e code by considering

\usepackage[figure,boxed,vlined,resetcount,algochapter]{algorithm2e}

This results in: enter image description here

However, I would like the algorithms to be boxed (like in the first figure without boxing the caption), numbered and indexed (create a list of algorithms) independently.

Does anybody have ideas?

Edit:

After trying John Kormylo's solution I got:

enter image description here

The text seems to exceed the margin in this case.

2
  • Algoritm2e implements its own \caption at the \@makecaption level. It has to in order to move the caption outside the box. It also totally ignores the caption package. Jan 3, 2019 at 19:47
  • 1
    ...and furthermore the algorithm2e package does not offer any appropriate (internal) interfaces usable by the caption package, so unfortunately the caption package could not be adapted to the algorithm2e package either. Adapting caption to float was a nightmare but adapting it to algorithm2e (which contains modified code from the float package) is impossible.
    – user2574
    Jan 3, 2019 at 21:36

1 Answer 1

2

Note: this will only work for boxed algorithms.

\documentclass[12pt,a4paper,oldfontcommands]{memoir}
\usepackage[boxed,vlined,resetcount,algochapter]{algorithm2e}
\SetAlgoCaptionSeparator{:}
\renewcommand\AlCapFnt{\normalfont}
\SetAlgoCaptionLayout{raggedright}
\SetAlCapSkip{\abovecaptionskip}

\makeatletter
\renewcommand{\algocf@makecaption}[2]{% no singleline check
  \addtolength{\hsize}{\algomargin}%
  \parbox[t]{\hsize}{\algocf@captiontext{#1}{#2}}%
  \addtolength{\hsize}{-\algomargin}%
}%
\renewcommand{\algocf@makecaption@boxed}[2]{%
  \addtolength{\hsize}{\algomargin}% again???
  \global\sbox\algocf@capbox{\algocf@makecaption{#1}{#2}}%
  \addtolength{\hsize}{-\algomargin}%
 }%
\renewcommand{\algocf@caption@boxed}{\vskip\AlCapSkip
  \leavevmode\hskip-\leftskip\box\algocf@capbox\hskip-\rightskip}
\makeatother

\usepackage{showframe}% alignment tool

\begin{document}
\chapter{ }

\begin{figure}[h]

\caption{Here is a long caption for an algorithm.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.}

\end{figure}

\begin{algorithm}
Some algorithm step \;

\rule{\hsize}{1ex}

\caption{Here is a long caption for an algorithm.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
\rule{\hsize}{1ex}}
\end{algorithm}

\end{document}

demo


Alternate code fragment:

\makeatletter
\renewcommand{\algocf@makecaption}[2]{% no singleline check
  \parbox[t]{\columnwidth}{\algocf@captiontext{#1}{#2}}%
}%
\renewcommand{\algocf@makecaption@boxed}[2]{%
  \global\sbox\algocf@capbox{\algocf@makecaption{#1}{#2}}%
 }%
\renewcommand{\algocf@caption@boxed}{\vskip\AlCapSkip
  \leavevmode\hskip-\leftskip\box\algocf@capbox\hskip-\rightskip}
\makeatother
3
  • Hey John, your solution worked, yet, the algorithm caption exceeds the margin. I added a pic to the question. What do you think might have caused this? Thanks
    – J-Cash
    Jan 4, 2019 at 9:55
  • That is not what I get. Try updating your packages. If that doesn't work, try the alternate version. Jan 4, 2019 at 16:59
  • Perfect! Thank you for your edits! Worked like a charm.
    – J-Cash
    Jan 6, 2019 at 15:34

You must log in to answer this question.

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