25

I am preparing a presentation with beamer from a previous paper written in latex. I want to insert in a slide the pseudocode

    \begin{algorithm}
\begin{algorithmic}[1]
\FOR{$i=1$ to $N$}
\FOR{$j=1$ to $JJJJ}
\STATE $energy[i*JJJ+j] =$ \\
$ interpolate(AAA[i*JJJ+j], ZZZ)$
\ENDFOR
\ENDFOR
\end{algorithmic}
\caption{pseudocode for the calculation of }
\label{alg:seq}
\end{algorithm}

but I get problems since the beamer document does not compile. I tried to use the same packages that in the latex document. I also tried to find examples through google but nothing. Could you tell me what is wron here or how could I add pseudocode in beamer )examples on the net, etc)?

  • 1
    Could you include your preamble? Also, when you say it "does not compile", what error do you get? – Loop Space Aug 25 '11 at 18:28
  • There's a missing closing $ in your code in the line \FOR{$j=1$ to $JJJJ}; it should be \FOR{$j=1$ to $JJJJ$}. – Gonzalo Medina Aug 25 '11 at 18:55
43

The floating object algorithm doesn't behave well with beamer (which onviously disables floating objects). To prevent problems you can 1) use the H placement specifier for algorithm, or 2) drop the algorithm environment and use the \captionof command from the caption package if a caption is needed. The following example shows the first approach:

\documentclass[12pt]{beamer}
\usepackage{algorithm,algorithmic}

\begin{document}

\begin{frame}

\begin{algorithm}[H]
\begin{algorithmic}[1]
\FOR{$i=1$ to $N$}
\FOR{$j=1$ to $JJJJ$}
\STATE $energy[i*JJJ+j] =$ 
$ interpolate(AAA[i*JJJ+j], ZZZ)$
\ENDFOR
\ENDFOR
\end{algorithmic}
\caption{pseudocode for the calculation of }
\label{alg:seq}
\end{algorithm}
\end{frame}

\end{document}
  • 1
    ok, it did the trick! gracias gonzalo – Open the way Aug 26 '11 at 8:48
  • how can one regulate algorithm's font size? – Open the way Aug 26 '11 at 8:49
  • @flow: your last request does not directly depend on the original question, so it would be better to open a fresh new question. – Gonzalo Medina Aug 26 '11 at 17:17
  • The preamble \usepackage{algorithm,algorithmic} does not work for me. I changed it to usepackage{algpseudocode} and this works. Don't know why. – hengxin Nov 26 '13 at 7:55
10
  • Load the required packages (for example algorithmic and algorithm2e or algorithm)
  • Use the float package with H option for the floating algorithm environment option to get a fixed position
  • Use the fragile option for the frame if you get strange errors, it can fix problems with verbatim text and listings

Compilable example:

\documentclass{beamer}
\usetheme{Singapore}
\usepackage{algorithm2e}
\usepackage{algorithmic}
\usepackage{float}
\begin{document}
\section{Test}
\begin{frame}[fragile]
\begin{algorithm}[H]
\begin{algorithmic}[1]
\FOR{$i=1$ to $N$}
\FOR{$j=1$ to $JJJJ$}
\STATE $energy[i*JJJ+j] =$ \\
$ interpolate(AAA[i*JJJ+j], ZZZ)$
\ENDFOR
\ENDFOR
\end{algorithmic}
\caption{pseudocode for the calculation of }
\label{alg:seq}
\end{algorithm}
\end{frame}
\end{document}

algorithm example

  • You can use the H specifier in beamer without explicitly loading the float package (see my answer). – Gonzalo Medina Aug 25 '11 at 19:21
-1

We add a new slide to Beamer documentation, using this \begin{frame}...\end{frame} and algorithm using \begin{algorithm}...\end{algorithm} If you try to directly embed algorithm in the frame it would not work.

Therefore use \scalebox{}{...}

Syntax:

\begin{frame}
    \scalebox{1}{\begin{algorithm}...\end{algorithm}}
\end{frame}

https://beerensahu.wordpress.com/2014/11/17/documentation-how-to-write-algorithms-in-latex-beamer-slids/

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.