# Using Algorithm2e package in Beamer

My MWE is

\documentclass[pdf]{beamer}
\usepackage[lined]{algorithm2e}

\begin{document}

\begin{frame}
\begin{algorithm}
\DontPrintSemicolon
\KwIn{Labels: set of all labels}
\KwIn{F: combinatorial logic function}
\KwIn{Lat: lattice representation of all labels}
\KwIn{X: set of inputs to F with values in 0,1}
\KwOut{$Sh_F$: truth table for shadow F}
\For{each XRow $\in$ XSet}{
\For{each LRow $\in$ LSet}{
CandidateSet $\gets$ $\phi$\;
\For{each $label \in Labels$} {
$C \gets \phi$\;
\For{each $l_i \in LRow$} {
\If{\underline{Lat.conflictsWith}($l_i$,label)}{
$C \gets C \cup \{x_i\}$ \;
}
}
\If{!\underline{isAffectedBy}(F,$X_{row}$,C)}{
CandidateSet $\gets$ CandidateSet $\cap$ \{label\} \;
}
}
$SHF_{row} \gets$ Lat.ChooseMin(CandidateSet) \;
Output $X_{row}$, $T_{row}$, $SHF_{row}$ \;
}
}
\end{algorithm}
\end{frame}

\end{document}


this results in following error

! LaTeX Error: Not in outer par mode.


Floating environments can cause problems in beamer, so you need to force the algorithm to stay in place with a [H] specification:

\documentclass[pdf]{beamer}
\usepackage[lined]{algorithm2e}

\begin{document}
\begin{frame}
\scalebox{.7}{                        %new code
\begin{algorithm}[H]              %new code
\DontPrintSemicolon
\KwIn{Labels: set of all labels}
\KwIn{F: combinatorial logic function}
\KwIn{Lat: lattice representation of all labels}
\KwIn{X: set of inputs to F with values in 0,1}
\KwOut{$Sh_F$: truth table for shadow F}
\For{each XRow $\in$ XSet}{
\For{each LRow $\in$ LSet}{
CandidateSet $\gets$ $\phi$\;
\For{each $label \in Labels$} {
$C \gets \phi$\;
\For{each $l_i \in LRow$} {
\If{\underline{Lat.conflictsWith}($l_i$,label)}{
$C \gets C \cup \{x_i\}$ \;
}
}
\If{!\underline{isAffectedBy}(F,$X_{row}$,C)}{
CandidateSet $\gets$ CandidateSet $\cap$ \{label\} \;
}
}
$SHF_{row} \gets$ Lat.ChooseMin(CandidateSet) \;
Output $X_{row}$, $T_{row}$, $SHF_{row}$ \;
}
}
\end{algorithm}
}
\end{frame}
\end{document}


I've also scaled down a bit with \scalebox, since it didn't fit the frame.

• What does the [H] specifier do? – gsamaras Mar 1 '17 at 17:24
• @gsamaras forces the algorithm environment to stay in place and not float. The algorithm env works like the figure env, it floats by default. In beamer floating makes no sense, so you need to force the env to stay where the code is. – Nico Vecchio Mar 1 '17 at 18:15
• Why do we need this for algorithm environment but not for figures? – Cyriac Antony Oct 11 '19 at 10:13