如何将算法标题更改为框外的图形标题?

如何将算法标题更改为框外的图形标题?

我正在使用IEEEtran\usepackage[boxed,linesnumbered]{algorithm2e}我有一个伪代码/算法。环境内的标题algorithm显示为“算法 1”。由于我只有一个算法,我认为我应该使用标题figure。您觉得如何?我如何才能强制我的标题显示为标题figure

为了提供评论所需的信息,这里是我的代码片段:

\IncMargin{1em}
\begin{algorithm}
\DontPrintSemicolon
\Indm
\KwInputs{\hspace*{0.3em}sth}
\KwOutput{sth}
\Indp
this is the body of my algo.
\caption{this is my caption.}
\end{algorithm}
\DecMargin{1em}

答案1

最简单的解决方案是使用 选项figurealgorithm2e这样,算法就被放入figure环境中,并被编号为图形并包含在 中\listoffigures

\documentclass{IEEEtran}
\usepackage[figure]{algorithm2e} 

\begin{document}

\begin{algorithm}
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e}
\While{not at end of this document}{read current\;
  \eIf{understand}{
  go to next section\;
  current section becomes this one\;
  }{
    go back to the beginning of current section\;
  }
}
\caption{How to write algorithms}
\end{algorithm}

\end{document}

结果图像:

在此处输入图片描述

更新

根据对原始答案的评论,algorithm2e加载了选项boxedlinesnumbered;在这种情况下,添加选项figure,如

\usepackage[boxed,linesnumbered,figure]{algorithm2e}

会产生不良影响,即字幕会移到算法的框架内。为了防止这种情况发生,需要做一些额外的工作,但由于这只是针对一种算法,我认为这种努力是值得的。

这个想法是加载包仅有的选择figure

\usepackage[figure]{algorithm2e}

然后手动框住算法,也用来\LinesNumbered得到数字:

\documentclass{IEEEtran}
\usepackage[figure]{algorithm2e} 

\newsavebox\mybox

\begin{document}

Algo de texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba  texto de prueba
\begin{algorithm}
\LinesNumbered
\DontPrintSemicolon
\begin{lrbox}{\mybox}
\begin{minipage}{\hsize}
% Write your algorithm starting here
\Indentp{-1em}
\KwIn{something}
\KwOut{something}
\Indentp{1.2em}
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e}
\While{not at end of this document}{read current\;
  \eIf{understand}{
  go to next section\;
  current section becomes this one\;
  }{
    go back to the beginning of current section\;
  }
}
% End your algorithm here
\end{minipage}%
\end{lrbox}
\hspace*{-10pt}\framebox[\columnwidth]{\hspace*{15pt}\usebox\mybox\par}
\caption{How to make algorithms}
\end{algorithm}

\end{document}

结果:

在此处输入图片描述

相关内容