0

I managed to remove the caption label and number, but the new caption is not perfectly centered as shown below. So, how to literally center the new caption style?

\documentclass{article}

\usepackage{xcolor,hyperref}

\usepackage[
ruled,
vlined,
commentsnumbered,
shortend,
nofillcomment,
]{algorithm2e}

\SetVlineSkip{10pt}
\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{green!60!black}{#1}}
\SetCommentSty{mycommfont}
\SetKwComment{tcc}{\% }{ \%}

\newcommand{\mycapsty}[1]{\centering\normalfont#1}
\SetAlCapNameSty{mycapsty}

\makeatletter
\renewcommand{\fnum@algocf}{\AlCapSty{\AlCapFnt}}
\makeatother

\SetAlgoCaptionSeparator{}
\setlength{\algomargin}{5mm}

\begin{document}
    \begin{algorithm}
        \DontPrintSemicolon
        \BlankLine
        Algorithm Body\;\BlankLine

        \tcc{Loop}      
        \caption{long long long long long long  long long long long caption to center}
    \end{algorithm}
\end{document}

enter image description here

1

algorithm2e inserts some spacing which interferes with the centering. You can adapt by modifying your \rightskip:

Instead of

\newcommand{\mycapsty}[1]{\centering\normalfont#1}

write

\newcommand{\mycapsty}[1]{\centering\normalfont%
  \rightskip=\glueexpr\rightskip-2\AlCapHSkip\relax%
  #1}

enter image description here

  • I am afraid it doesn't work in addition to unwanted vertical skip as shown here. – Diaa Mar 12 '18 at 11:29
  • @DiaaAbidou Sorry, I adapted the answer. The problems should be fixed now. – Marcel Krüger Mar 12 '18 at 12:53

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.