2

Consider the following minimal example:

\documentclass[usenames,dvipsnames]{beamer}
\usepackage[linesnumbered,boxed,ruled,vlined,resetcount]{algorithm2e}
\begin{document}
    \begin{frame}[fragile]
        With caption:
    \begin{algorithm}[H]

        \KwIn{ $x, y$ }
        \KwOut{$z$}
                
        $z = x\cdot y + 1$
                
        \Return $z$
        \caption{$FindZ$}
    \end{algorithm}
    
    \vspace*{0.5cm}
    Without caption:
    \begin{algorithm}[H]
        \TitleOfAlgo{$FindZ$}
        
        \KwIn{ $x, y$ }
        \KwOut{$z$}
        
        $z = x\cdot y + 1$
        
        \Return $z$
    \end{algorithm}
    \end{frame}     
\end{document}

It generates the following slide with two algorithms:

enter image description here

In the first one, I used the command \caption and the nice thing is that the title appears between the two horizontal lines just before describing the input. But I don't like the number.

Following some answers I found here in tex.stackexchange, instead of \caption, I used \TitleOfAlgo in the second algorithm. Now I don't have the number in front of the word "Algorithm", but the title counts as a line of the algorithm (it is numbered as line 1). Also, it doesn't appear between the two lines (we see the two lines above it).

Is there a way of obtaining the same thing I obtain with Caption, but showing no number in front of "Algorithm" nor making it a numbered line?

1 Answer 1

1

Using \RemoveAlgoNumber from algorithm title, which is unnumbered, is not in place allows you to switch algorithm numbering on/off. If done within a frame, it'll be temporary:

enter image description here

\documentclass{beamer}

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

\makeatletter
\newcommand{\RemoveAlgoNumber}{\renewcommand{\fnum@algocf}{\AlCapSty{\AlCapFnt\algorithmcfname}}}
\newcommand{\RevertAlgoNumber}{\algocf@resetfnum}
\makeatother

\begin{document}

\begin{frame}
  With caption:
  \begin{algorithm}[H]

    \KwIn{ $x, y$ }
    \KwOut{$z$}
                
    $z = x\cdot y + 1$
                
    \Return $z$
    \caption{$FindZ$}
  \end{algorithm}
    
  \vspace*{0.5cm}
  \RemoveAlgoNumber
  Without caption:
  \begin{algorithm}[H]
    \caption{$FindZ$}
        
    \KwIn{ $x, y$ }
    \KwOut{$z$}
        
    $z = x\cdot y + 1$
        
    \Return $z$
  \end{algorithm}
\end{frame}

\end{document}

Note that the algorithm number is still incremented. However, since this forms part of a (beamer) presentation, it's best to avoid numbering content and rather refer to them by name anyway. That is, use \RemoveAlgoNumber in the preamble to turn all algorithm numbering off.

1
  • Crazy how complicated such a simple thing can be... But thanks for figuring it out! Commented Aug 30, 2023 at 23:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .