1

I am migrating to algorithm2e package. I would like to align the algorithm title as justified according to the text column width. But I also would like to know how to align left, to align right and to center the title.

My MWE attempt:

\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}

The execution results of aforementioned code:

Fig. 1

1

I redefined \algocf@makecaption@ruled to use the whole column, minus \AlgoCapHskip on both sides. This will have no effect on boxed or other format captions.

I also added \thealgorithm to enable one to access the caption number, and \SetAlgoCaptionFormat to facilitate other formatting styles (like \centering, \raggedleft and \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}

demo

|improve this answer|||||
  • That is great solution! It is exactly what I was looking for. I tested your solution and it worked fine in my context. Thank you by help. – Adriano May 11 '19 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.