如何在盒装算法中将标题置于中心?

如何在盒装算法中将标题置于中心?

我想使用以下算法使用该algorithm2e包将标题居中。但我不明白。

文档告诉我这\SetAlgoCaptionLayout{centerline}应该可以工作,但事实并非如此。

你有什么想法? :)

多谢!

\documentclass{scrreprt}

\usepackage{lmodern}
\usepackage{xcolor}
\usepackage[boxed]{algorithm2e}
  \DontPrintSemicolon
  \SetAlCapSkip{.5\baselineskip}
  \SetAlgoCaptionLayout{centerline} % Does not work.
  \SetCommentSty{commentFont}
    \newcommand\commentFont[1]{\textcolor{gray}{#1}}

\begin{document}

\begin{algorithm}[H]
  \KwIn{Some Data\tcp*[r]{This is some input data.}}
  \While(\tcp*[f]{A while-loop}){\(j<\dots\)}{
    Do some stuff.\tcp*[r]{Comment}
    \(j=j+1\)\tcp*[r]{Increment}}
  \KwOut{Output data\tcp*[r]{Some fancy results}}
  \caption{This is a long long very loooooooooooooooooooong caption.}
\end{algorithm}

\end{document}

答案1

这看起来像是 的一个错误。从中algorithm2e的定义注释来看,如果 caption 短于,则它居中。因此,我从您的示例中删除了 。\algocf@makecaptionalgorithm2e.sty\hsize\SetAlgoCaptionLayout{centerline}

\documentclass{scrreprt}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage[boxed]{algorithm2e}
  \DontPrintSemicolon
  \SetAlCapSkip{.5\baselineskip}
  \SetCommentSty{commentFont}
    \newcommand\commentFont[1]{\textcolor{gray}{#1}}

\usepackage{xpatch}
\makeatletter
\xpatchcmd\algocf@makecaption
  {\hbox}
  {\hskip\algomargin\hbox}
  {}{\fail}
\makeatother

\begin{document}

\begin{algorithm}[H]
  \KwIn{Some Data\tcp*[r]{This is some input data.}}
  \While(\tcp*[f]{A while-loop}){\(j<\dots\)}{
    Do some stuff.\tcp*[r]{Comment}
    \(j=j+1\)\tcp*[r]{Increment}}
  \KwOut{Output data\tcp*[r]{Some fancy results}}
  \caption{This is a long long very loooooooooooooooooooong caption.}
\end{algorithm}

\end{document}

在此处输入图片描述

相关内容