[Tex/LaTex] Adjusting the caption of an algorithm2e code

algorithm2ecaptionsfloatsindentation

I'd like to typeset the caption of an algorithm environment such that it is bold, fills the available width of the paper, and neither the caption nor the line numbers encroach into the margins.

In the example, the caption is indented and encroaches to the margin. I know that I can set the width of the caption but setting the width of the caption doesn't remove the caption indentation. I also know how to decrease the caption's indentation, but, removing the caption indentation makes the line numbers encroach to the margins. How can I make the caption fill the whole width (without any indentation) and keep the line numbers from encroaching to the margins?

I'd like to keep the caption a figure caption but I'm open to configuring the "algorithm2e" or "caption" packages.

\documentclass[10pt]{article}
\usepackage{showframe}
\usepackage[figure,noend,noline,linesnumbered]{algorithm2e}
\usepackage[font=bf,skip=\baselineskip]{caption}
\begin{document}
\begin{algorithm}[t]
Some algorithm step \;
\caption{Here is a long caption for an algorithm. I'd like to typeset the caption such that it is bold and fills the available width of the page without encroaching into margins.}
\end{algorithm}
\end{document}

enter image description here

Best Answer

You can get captions identical to the ones for figure in the following way:

\documentclass[10pt]{article}
\usepackage{showframe}
\usepackage[figure,noend,noline,linesnumbered]{algorithm2e}
\usepackage[font=bf,skip=\baselineskip]{caption}
\usepackage{etoolbox}
\AtBeginEnvironment{algorithm}{%
  \captionsetup{margin={-\algomargin,\algomargin}}%
}

\begin{document}
\begin{figure}[h]

\caption{Here is a long caption for an algorithm. I'd like to typeset 
  the caption such that it is bold and fills the available width of 
  the page without encroaching into margins.}

\end{figure}
\begin{algorithm}[h]
\raggedright
Some algorithm step \;
\caption{Here is a long caption for an algorithm. I'd like to typeset 
  the caption such that it is bold and fills the available width of 
  the page without encroaching into margins.}
\end{algorithm}
\end{document}

However every caption to algorithm will issue a spurious

Overfull \hbox (7.5pt too wide)

message.

enter image description here