![调整 algorithm2e 代码的标题](https://linux22.com/image/294243/%E8%B0%83%E6%95%B4%20algorithm2e%20%E4%BB%A3%E7%A0%81%E7%9A%84%E6%A0%87%E9%A2%98.png)
我想将算法环境的标题排版为粗体,填满纸张的可用宽度,并且标题和行号都不会侵占边距。
在示例中,标题缩进并侵占了页边距。我知道我可以设置标题的宽度,但设置标题的宽度不会消除标题缩进。我也知道如何减少标题的缩进,但是,消除标题缩进会使行号侵占页边距。我怎样才能使标题填满整个宽度(没有任何缩进)并防止行号侵占页边距?
我想将标题保留为图形标题,但我愿意配置“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)
信息。