TransWikia.com

Beamer and pseudocode

TeX - LaTeX Asked by Open the way on February 14, 2021

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

4 Answers

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}

Correct answer by Gonzalo Medina on February 14, 2021

  • 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

Answered by Stefan Kottwitz on February 14, 2021

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/

Answered by Beeren Sahu on February 14, 2021

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}

Answered by izmirlig on February 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP