带有 beamer 包的算法导致噩梦

带有 beamer 包的算法导致噩梦

我有一个与 配合良好的算法\documentclass[twocolumn]{svjour3}。现在我必须展示这篇论文,并且我希望一张幻灯片是该算法,所以我这样做了:

\documentclass{beamer}
\usepackage{graphicx}

% definitions 
\newcommand{\kd}{\emph{k}-d\space}
\newcommand{\kgeraf}{\kd GeRaF}
\newcommand{\geraf}{{\tt GeRaF}\xspace}
% \newcommand{\att}[1]{\alert{#1}}
\newcommand{\att}[1]{{#1}}

% shortcuts 
\newcommand{\tab}{\hspace*{2em}}
\newcommand{\head}[1]{{\medskip\noindent\emph{#1}}}
\newtheorem{defn}{Definition}
\usepackage[top=2.5cm,bottom=2.3cm,hmargin=1.5cm]{geometry} % it was hmargin=2.5cm
\usepackage{amssymb,graphicx,url}
\setcounter{tocdepth}{3}
\usepackage[english]{babel}
\usepackage{multirow}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}
\usepackage{xspace}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.90} % ize got latex error here
\usepackage{pgfplotstable}
\def\real{{\mathbb R}}

%--------------------------------------------------------------------------------
% Add a period to the end of an abbreviation unless there's one
% already, then \xspace.
\makeatletter
\DeclareRobustCommand\onedot{\futurelet\@let@token\@onedot}
\def\@onedot{\ifx\@let@token.\else.\null\fi\xspace}
%
\def\eg{\emph{e.g}\onedot} \def\Eg{\emph{E.g}\onedot}
\def\ie{\emph{i.e}\onedot} \def\Ie{\emph{I.e}\onedot}
\def\cf{\emph{c.f}\onedot} \def\Cf{\emph{C.f}\onedot}
\def\etc{\emph{etc}\onedot} %\def\vs{\emph{vs}\onedot}
\def\wrt{w.r.t\onedot} \def\dof{d.o.f\onedot}
\def\etal{\emph{et al}\onedot}
\makeatother

%--------------------------------------------------------------------
\begin{document}
\begin{algorithm}[t]
\SetFuncSty{textsc}
\SetDataSty{emph}
\newcommand{\expr}[1]{$\langle${#1}$\rangle$}
\newcommand{\commentsty}[1]{{\color{DarkGreen}#1}}
\SetCommentSty{commentsty}
\SetKwComment{Comment}{$\triangleright$ }{}
\SetKwBlock{Block}{function}{}
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}
\SetKw{Return}{return}
\SetKwData{Node}{node}
\SetKwData{Leaf}{leaf}
\SetKwFunction{Build}{build}
\SetKwFunction{Variance}{variance}
\SetKwFunction{Median}{median}
\SetKwFunction{Split}{split}

\Input
{pointset $X$, \#trees $m$, \#split-dimensions $t$, max \#points per leaf $p$
}
\Output{randomized \kd forest $F$}
\Begin
{
    $V \gets$ \expr{\Variance of $X$ in every dimension} \\
    $D \gets$ \expr{$t$ dimensions of maximum variance $V$} \\
    $F \gets \emptyset$  \Comment*[f]{forest} \\
    \For{$i \gets 1$ \KwTo $m$}
    {
        $f \gets$ \expr{random transformation}  \Comment*[f]{isometry, shuffling} \\
        $F \gets F \cup (f,\Build(f(X))$) \Comment*[f]{build on transformed X, store $f$}
    }
    \Return $F$
}

\BlankLine

\Block({$\Build(X)$} \Comment*[f]{recursively build tree (node/leaf)})
{
    \If(\Comment*[f]{termination reached}){$|X| \le p$}{\Return $\Leaf(X)$}
    \Else(\Comment*[f]{split points and recurse})
    {
        $s \gets$ \expr{one of dimensions $D$ at random} \\
        $v \gets $ \expr{\Median of $X$ in dimension $s$} \\
        $(L,R) \gets$ \expr{\Split of $X$ in dimension $s$ at value $v$} \\
        \Return $\Node(c,v,\Build(L),\Build(R))$ \Comment*[f]{build children on $L,R$}\\
    }
}
\caption{\kgeraf: building\label{alg:build}}
\end{algorithm}
\end{document}

它开始产生以下性质的错误:

LaTeX 错误:包几何的选项冲突。

我已经在网站上搜索了 3 个小时,并应用了大量答案,但我就是无法让它编译!你能吗?

答案1

您有几个错误,我修复了最重要的错误。所有错误都\def应该是\newcommand

  1. 不加载geometry,这没有意义,因为beamer已经用自己的选项加载了它

  2. 不要加载xcolor,而是将选项传递给类,如图所示

  3. 不加载graphicx

  4. 不要加载url,它已经附带了hyperref

  5. 既然需要框架,那一定是fragile因为你在里面进行了宏定义

  6. 使用\footnotesize否则算法将不适合

  7. 在标题中使用\protect\kgeraf;可能的定义\kd应该是$k$\mbox{-}d

这是一个工作版本。

\documentclass[xcolor={dvipsnames,svgnames}]{beamer}

% definitions 
\newcommand{\kd}{\emph{k}-d\space}
\newcommand{\kgeraf}{\kd GeRaF}
\newcommand{\geraf}{{\tt GeRaF}\xspace}
% \newcommand{\att}[1]{\alert{#1}}
\newcommand{\att}[1]{{#1}}

% shortcuts 
\newcommand{\tab}{\hspace*{2em}}
\newcommand{\head}[1]{{\medskip\noindent\emph{#1}}}
\newtheorem{defn}{Definition}
%\usepackage[top=2.5cm,bottom=2.3cm,hmargin=1.5cm]{geometry} % it was hmargin=2.5cm
\usepackage{amssymb,url}
\setcounter{tocdepth}{3}
\usepackage[english]{babel}
\usepackage{multirow}
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}
\usepackage{xspace}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.90} % ize got latex error here
\usepackage{pgfplotstable}
\def\real{{\mathbb R}}

%--------------------------------------------------------------------------------
% Add a period to the end of an abbreviation unless there's one
% already, then \xspace.
\makeatletter
\DeclareRobustCommand\onedot{\futurelet\@let@token\@onedot}
\def\@onedot{\ifx\@let@token.\else.\null\fi\xspace}
%
\def\eg{\emph{e.g}\onedot} \def\Eg{\emph{E.g}\onedot}
\def\ie{\emph{i.e}\onedot} \def\Ie{\emph{I.e}\onedot}
\def\cf{\emph{c.f}\onedot} \def\Cf{\emph{C.f}\onedot}
\def\etc{\emph{etc}\onedot} %\def\vs{\emph{vs}\onedot}
\def\wrt{w.r.t\onedot} \def\dof{d.o.f\onedot}
\def\etal{\emph{et al}\onedot}
\makeatother

%--------------------------------------------------------------------
\begin{document}

\begin{frame}[fragile]
\footnotesize
\begin{algorithm}[H]
\SetFuncSty{textsc}
\SetDataSty{emph}
\newcommand{\expr}[1]{$\langle${#1}$\rangle$}
\newcommand{\commentsty}[1]{{\color{DarkGreen}#1}}
\SetCommentSty{commentsty}
\SetKwComment{Comment}{$\triangleright$ }{}
\SetKwBlock{Block}{function}{}
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}
\SetKw{Return}{return}
\SetKwData{Node}{node}
\SetKwData{Leaf}{leaf}
\SetKwFunction{Build}{build}
\SetKwFunction{Variance}{variance}
\SetKwFunction{Median}{median}
\SetKwFunction{Split}{split}

\Input
{pointset $X$, \#trees $m$, \#split-dimensions $t$, max \#points per leaf $p$
}
\Output{randomized \kd forest $F$}
\Begin
{
    $V \gets$ \expr{\Variance of $X$ in every dimension} \\
    $D \gets$ \expr{$t$ dimensions of maximum variance $V$} \\
    $F \gets \emptyset$  \Comment*[f]{forest} \\
    \For{$i \gets 1$ \KwTo $m$}
    {
        $f \gets$ \expr{random transformation}  \Comment*[f]{isometry, shuffling} \\
        $F \gets F \cup (f,\Build(f(X))$) \Comment*[f]{build on transformed X, store $f$}
    }
    \Return $F$
}

\BlankLine

\Block({$\Build(X)$} \Comment*[f]{recursively build tree (node/leaf)})
{
    \If(\Comment*[f]{termination reached}){$|X| \le p$}{\Return $\Leaf(X)$}
    \Else(\Comment*[f]{split points and recurse})
    {
        $s \gets$ \expr{one of dimensions $D$ at random} \\
        $v \gets $ \expr{\Median of $X$ in dimension $s$} \\
        $(L,R) \gets$ \expr{\Split of $X$ in dimension $s$ at value $v$} \\
        \Return $\Node(c,v,\Build(L),\Build(R))$ \Comment*[f]{build children on $L,R$}\\
    }
}
\caption{\protect\kgeraf: building}\label{alg:build}
\end{algorithm}

\end{frame}
\end{document}

在此处输入图片描述

答案2

您需要将 放在algorithm里面frame。即使这样,内容也太多了,无法放在普通幻灯片上。因此,我将其放在一个框内 - 使用浮动[H]说明符 - 并调整其大小以适合框架高度。

在此处输入图片描述

\documentclass[dvipsnames,svgnames]{beamer}

\usepackage[ruled,vlined,linesnumbered]{algorithm2e}

\begin{document}

\begin{frame}[fragile]
  \resizebox{!}{45mm}{\begin{algorithm}[H]
    \caption{$k$-d GeRaF: building}
    \SetFuncSty{textsc}
    \SetDataSty{emph}
    \newcommand{\expr}[1]{$\langle${#1}$\rangle$}
    \newcommand{\commentsty}[1]{{\color{DarkGreen}#1}}
    \SetCommentSty{commentsty}
    \SetKwComment{Comment}{$\triangleright$ }{}
    \SetKwBlock{Block}{function}{}
    \SetKwInOut{Input}{input}
    \SetKwInOut{Output}{output}
    \SetKw{Return}{return}
    \SetKwData{Node}{node}
    \SetKwData{Leaf}{leaf}
    \SetKwFunction{Build}{build}
    \SetKwFunction{Variance}{variance}
    \SetKwFunction{Median}{median}
    \SetKwFunction{Split}{split}
    \Input
    {pointset $X$, \#trees $m$, \#split-dimensions $t$, max \#points per leaf $p$
    }
    \Output{randomized $k$-d forest $F$}
    \Begin
    {
        $V \gets$ \expr{\Variance of $X$ in every dimension} \\
        $D \gets$ \expr{$t$ dimensions of maximum variance $V$} \\
        $F \gets \emptyset$  \Comment*[f]{forest} \\
        \For{$i \gets 1$ \KwTo $m$}
        {
            $f \gets$ \expr{random transformation}  \Comment*[f]{isometry, shuffling} \\
            $F \gets F \cup (f,\Build(f(X))$) \Comment*[f]{build on transformed X, store $f$}
        }
        \Return $F$
    }
    \BlankLine
    \Block({$\Build(X)$} \Comment*[f]{recursively build tree (node/leaf)})
    {
        \If(\Comment*[f]{termination reached}){$|X| \le p$}{\Return $\Leaf(X)$}
        \Else(\Comment*[f]{split points and recurse})
        {
            $s \gets$ \expr{one of dimensions $D$ at random} \\
            $v \gets $ \expr{\Median of $X$ in dimension $s$} \\
            $(L,R) \gets$ \expr{\Split of $X$ in dimension $s$ at value $v$} \\
            \Return $\Node(c,v,\Build(L),\Build(R))$ \Comment*[f]{build children on $L,R$} \\
        }
    }
  \end{algorithm}}
\end{frame}

\end{document}

我还删除了与本示例无关的所有多余内容。此外,由于这是一个演示,因此在代码中插入了一些通用定义,以便可以编译,尤其是框架的使用[fragile]

注意algorithm2e通常用来\;表示行结束。

如果您使用不同的主题,那么您可能需要调整<height>中的参数\resizebox{!}{<height>}{...}

相关内容