在 Beamer 中使用 Algorithm2e 包

在 Beamer 中使用 Algorithm2e 包

我的 MWE 是

\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}

这会导致以下错误

! LaTeX Error: Not in outer par mode.

答案1

浮动环境可能会导致投影仪出现问题,因此您需要强制算法保持在适当的位置并遵循以下[H]规范:

\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}

我还将其缩小了一点\scalebox,因为它不适合框架。

在此处输入图片描述

相关内容