如何使用 algorithm2e latex 包使算法标题居中、左对齐、右对齐和两端对齐?

如何使用 algorithm2e latex 包使算法标题居中、左对齐、右对齐和两端对齐?

我正在迁移到 algorithm2e 包。我想根据文本列宽将算法标题对齐。但我还想知道如何左对齐、右对齐和居中标题。

我的 MWE 尝试:

\documentclass[brazil,twocolumn]{article}



\usepackage{babel}
\usepackage{lmodern}                        
\usepackage[T1]{fontenc}        
\usepackage[utf8]{inputenc} 

\usepackage{lipsum}

\usepackage[portuguese,portuguesekw,linesnumbered,vlined,ruled,commentsnumbered,algo2e]{algorithm2e}



\begin{document}

\twocolumn[]

\lipsum[1-3]

The algorithm with \verb|algorithm2e| package:

\begin{algorithm2e}[h]

    \SetAlgoLined
    \SetKwFunction{ProcedureTitle}{Teste}
    \SetKwProg{KwPcdr}{Procedure}{}{end}
    \DontPrintSemicolon

    \BlankLine

    \KwIn{$a$, $b$, $c$.}
    \KwOut{$d$, $e$, $f$.}

    \BlankLine

    \KwPcdr{\ProcedureTitle{$a$, $b$, $c$}}{
        \BlankLine  
        \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\;
            }
        }       
        \KwRet{$d$, $e$, $f$}   }

    \caption{A long caption is being placed here. I would like to typeset breaklines and full caption box size (i.e., height and width for the ``Algorithm #1: title string'' set).}
    \label{alg:attempt}

\end{algorithm2e}



\end{document}

上述代码的执行结果:

图。1

答案1

我重新定义\algocf@makecaption@ruled为使用整列,\AlgoCapHskip两边都减去。这对方框或其他格式的标题没有影响。

我还添加了\thealgorithm使人们能够访问标题编号的功能,并\SetAlgoCaptionFormat促进其他格式样式(如\centering\raggedleft\raggedright)。

\documentclass[brazil,twocolumn]{article}
\usepackage{babel}
\usepackage{lmodern}                        
\usepackage[T1]{fontenc}        
\usepackage[utf8]{inputenc} 
\usepackage{lipsum}
\usepackage[portuguese,portuguesekw,linesnumbered,vlined,ruled,commentsnumbered,algo2e]{algorithm2e}

\SetAlCapHSkip{0pt}% set caption margins

\makeatletter
\newcommand{\thealgorithm}{\arabic\algocf@float}

\newcommand{\AlgoCaptionFormat}{}
\newcommand{\SetAlgoCaptionFormat}[1]{\def\AlgoCaptionFormat{#1}}

\renewcommand{\algocf@makecaption@ruled}[2]{%
  \global\sbox\algocf@capbox{\hskip\AlCapHSkip%
    \setlength{\hsize}{\columnwidth}% restored on exit of sbox
    \addtolength{\hsize}{-2\AlCapHSkip}% add equal margin to both sides
    \vtop{\AlgoCaptionFormat\algocf@captiontext{#1}{#2}}}% then caption is not centered
}%
\makeatother

%\SetAlgoCaptionFormat{\raggedright}

\begin{document}

\twocolumn[]

\lipsum[1-3]

The algorithm with \verb|algorithm2e| package:

\begin{algorithm2e}[h]

    \SetAlgoLined
    \SetKwFunction{ProcedureTitle}{Teste}
    \SetKwProg{KwPcdr}{Procedure}{}{end}
    \DontPrintSemicolon

    \BlankLine

    \KwIn{$a$, $b$, $c$.}
    \KwOut{$d$, $e$, $f$.}

    \BlankLine

    \KwPcdr{\ProcedureTitle{$a$, $b$, $c$}}{
        \BlankLine  
        \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\;
            }
        }       
        \KwRet{$d$, $e$, $f$}   }

    \caption{A long caption is being placed here. I would like to typeset breaklines and full caption box size (i.e., height and width for the ``Algorithm \thealgorithm: title string'' set).}
    \label{alg:attempt}
\end{algorithm2e}

\end{document}

演示

相关内容