2

When I add a caption to my algorithms, it appears on the top, just as I want, however, it is always preceded by a text like "Algorithm 1:". For example, this code

\documentclass{beamer}
\usepackage[linesnumbered,boxed,ruled,vlined]{algorithm2e}

\begin{document}

\begin{frame}
    \begin{center}
        \begin{algorithm}[H]
            \DontPrintSemicolon
            \KwIn{
                array $v[0, ..., n-1]$,
                $n \ge 1$
            }
            \KwOut{sum of odd entries of $v$}
        
            $s \gets 0$
            
            \For{$(i \gets 0; i < n; i++)$}{
                \If{$v[i] \bmod 2 = 1$}{
                    $s \gets s + v[i]$
                }
            }
            \Return $s$
            \caption{$\mathtt{Add}(v, n)$}
        \end{algorithm}
    \end{center}
\end{frame}
\end{document}

generates this

algorithm

How can I change it so that the caption is just "Add(v, n)" instead of "Algorithm 1: Add(v, n)"?

1 Answer 1

2

Adjust the way the caption text is set. Since algorithm2e overrides the way \captions are handled (to have absolute control over placement and embellishments (like rules above/below), add the following to your preamble after loading algorithm2e:

\makeatletter
\renewcommand{\algocf@captiontext}[2]{\AlCapNameSty{\AlCapNameFnt{}#2\endgraf}}%
\makeatother

The above snippet was modified from the source - algorithm2e.sty - by removing the formatting of the first argument #1 (the Algorithm < num >: part). You'll see there's no reference to it, and only the second argument #2 is used.

enter image description here

\documentclass{beamer}

\usepackage[linesnumbered,boxed,ruled,vlined]{algorithm2e}

\makeatletter
\renewcommand{\algocf@captiontext}[2]{\AlCapNameSty{\AlCapNameFnt{}#2\endgraf}}%
\makeatother

\begin{document}

\begin{frame}
  \begin{center}
    \begin{algorithm}[H]
      \DontPrintSemicolon
      \KwIn{
        array $v[0, \dots, n-1]$,
          $n \geq 1$
      }
      \KwOut{sum of odd entries of $v$}

      $s \gets 0$\;

      \For{$(i \gets 0; i < n; i{+}{+})$}{
        \If{$v[i] \bmod 2 = 1$}{
          $s \gets s + v[i]$\;
        }
      }
      \Return $s$\;
      \caption{$\mathtt{Add}(v, n)$}
    \end{algorithm}
  \end{center}
\end{frame}

\end{document}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.