38

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

2
  • 1
    Could you include your preamble? Also, when you say it "does not compile", what error do you get? Aug 25, 2011 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$}. Aug 25, 2011 at 18:55

4 Answers 4

66

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
  • 1
    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, 2013 at 7:55
14
  • 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

1
  • You can use the H specifier in beamer without explicitly loading the float package (see my answer). Aug 25, 2011 at 19:21
1

enter image description hereYeah. None of this works for me. I have been scouring for answers for two or three days and seen yet other solutions that don't work for me. I'm betting that if you're using latex 2020 by han the tanh then you're here reading this. Go ahead and downvote me all you want, but you can always make it look good enough with tabbing inside of a block environment. You can even enclose each printing line within an uncover for extra dramatic effect.

\begin{frame}
  \frametitle{My Procedure}
\begin{block}{}
\begin{tabbing}
  \uncover<2- | alert @2>{My\=Fun($x,y$)\=\\}
  \uncover<3- | alert @3>{\>Let $\ell=1,$ \>~~$\epsilon_0=1,w_0=x,$ and $z_0=y$\\}
  \uncover<4- | alert @4>{\>While \>$(\epsilon_{\ell-1} >$ tol $)$\\}
  \uncover<5- | alert @5>{\>\>Let $w_{\ell}$ be the solution to $w = G(w z_{\ell-1})$\\}
  \uncover<6- | alert @6>{\>\>Let $z_{\ell}$ be the solution to $z = G(w_{\ell-1} z) + H(z)$\\}
  \uncover<7- | alert @7>{\>\>Let $\epsilon_{\ell}= |w_{\ell} - w_{\ell-1}| + |z_{\ell} - z_{\ell-1}|$\\}
  \uncover<8- | alert @8>{\>End While\>\\}
  \uncover<9- | alert @9>{End MyFun\>\\}
\end{tabbing}
\end{block}
\end{frame}
-2

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/

2
  • 2
    It appears that you are the author of the linked article. Please disclose your affiliation with your articles and videos in the answer for your post not to be considered spam.
    – Ollie
    Oct 8, 2021 at 17:43
  • Scaling is very rarely a good idea: here, scaling to 1 won't make anything fit, and scaling to an arbitrary amount will give an inconsistent appearance
    – Joseph Wright
    Oct 11, 2021 at 7:20

You must log in to answer this question.

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