Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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)?

share|improve this question
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

2 Answers 2

up vote 15 down vote accepted

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}
share|improve this answer
    
ok, it did the trick! gracias gonzalo –  flow Aug 26 '11 at 8:48
    
how can one regulate algorithm's font size? –  flow 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
  • 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

share|improve this answer
    
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
    
@Gonzalo: thanks, good to know! –  Stefan Kottwitz Aug 25 '11 at 20:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.