# How to center, to align left, to align right and to justify the algorithm title using the algorithm2e latex package?

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}

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

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}

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


• 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