调整 algorithm2e 代码的标题

调整 algorithm2e 代码的标题

我想将算法环境的标题排版为粗体,填满纸张的可用宽度,并且标题和行号都不会侵占边距。

在示例中,标题缩进并侵占了页边距。我知道我可以设置标题的宽度,但设置标题的宽度不会消除标题缩进。我也知道如何减少标题的缩进,但是,消除标题缩进会使行号侵占页边距。我怎样才能使标题填满整个宽度(没有任何缩进)并防止行号侵占页边距?

我想将标题保留为图形标题,但我愿意配置“algorithm2e”或“caption”包。

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

在此处输入图片描述

答案1

您可以通过以下方式获得与图片相同的标题:

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

然而每一个字幕algorithm都会发出一个虚假的

Overfull \hbox (7.5pt too wide)

信息。

在此处输入图片描述

相关内容