4

I am trying to insert the caption on an algorithm in beamer presentation. The algorithm syntax is correct and it is working while using \documentclass{article} but it shows following error when \documentclass[11pt]{beamer} is used:

! LaTeX Error: Not in outer par mode.
See the LaTeX manual or LaTeX Companion for explanation.

Below is the sample code:

%\documentclass{article}
\documentclass[11pt]{beamer}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}

\algdef{SE}[DOWHILE]{Do}{doWhile}{\algorithmicdo}[1]{\algorithmicwhile\ #1}%

\begin{document}
\begin{frame} %comment \begin{frame} and \end{frame} while using article
    \begin{algorithm}
    \caption{This is comment}
    \begin{algorithmic}[1]
        \ForAll{$point$ ~in $trajectory$}
        \State {$i\gets maxval$}
        \Do
        \State $success \gets dist(a,b)$
        \doWhile{$success \le 0$}
        \EndFor
    \end{algorithmic}
    \end{algorithm}
\end{frame}
\end{document}

Below are the TeX compiler details:

pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015) (preloaded format=pdflatex 2016.5.18) 
ravi
  • 1,618

1 Answers1

5

The typical behaviour of floats are naturally suppressed within a presentation, as it doesn't have the same flow as an article. So, under beamer, figures and tables may be structured in a similar way they're done under article, but beamer just puts them in the frame you use them, rather than finding "a suitable location" through floating.

When you use an algorithm, it hasn't been formatted to not float, which is the root of the problem. Luckily, algorithm defines its floating algorithm environment using float which, in turn, defines the non-floating specifier [H]. Use this to keep beamer happy:

enter image description here

\documentclass{beamer}

\usepackage{algorithm,algpseudocode}

\algdef{SE}[DOWHILE]{Do}{doWhile}{\algorithmicdo}[1]{\algorithmicwhile\ #1}%
\algnewcommand{\var}{\textit}

\begin{document}

\begin{frame}
  \begin{algorithm}[H]% <-------- do not float, stay right [H]ERE!
    \caption{This is comment}
    \begin{algorithmic}[1]
      \ForAll{\var{point} in \var{trajectory}}
        \State {$i \gets \var{maxval}$}
        \Do
          \State $\var{success} \gets dist(a,b)$
        \doWhile{$\var{success} \leq 0$}
      \EndFor
    \end{algorithmic}
  \end{algorithm}
\end{frame}

\end{document}

Note that [H] is completely different from [h] or [h!]. The former actually sets a minipage while making \caption think its in a float (since \captions are usually associated with a float), while the latter still sets a float that could move. As an FAQ in this regard, see How to influence the position of float environments like figure and table in LaTeX?

Werner
  • 603,163