我正在尝试将 algorithm2e 的算法标题左对齐,以匹配图形的标题。
看看左对齐标题,这是我目前的代码:
\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}
一个解决方案是使用数字参数说明调整 algorithm2e 代码的标题通过考虑
\usepackage[figure,boxed,vlined,resetcount,algochapter]{algorithm2e}
但是,我希望算法能够被装箱(就像第一个图那样,没有装箱标题)、编号和索引(创建算法列表)。
有人有想法吗?
编辑:
尝试了 John Kormylo 的解决方案后,我得到了:
在这种情况下,文本似乎超出了边距。
答案1
注意:这仅适用于盒装算法。
\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}
替代代码片段:
\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