[Tex/LaTex] Algorithm with beamer package causing nightmares

algorithm2ebeamer

I have an algorithm which works well with \documentclass[twocolumn]{svjour3}. Now I have to present the paper and I would like one slide to be that algorithm, so I do:

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

which starts producing errors of that nature:

LaTeX Error: Option clash for package geometry.

I have searched for the past 3 hours in the site and applied the numerous answers, but I just can't make this thing compile! Can you?

Best Answer

You have several errors, I fixed the most important ones. All the \def should better be \newcommand.

  1. Don't load geometry, it doesn't make sense, as beamer already loads it with its own options

  2. Don't load xcolor, rather pass the options to the class as shown

  3. Don't load graphicx

  4. Don't load url, it's already coming with hyperref

  5. Since you need a frame, it must be fragile because you perform macro definitions in it

  6. Use \footnotesize or the algorithm will not fit

  7. Use \protect\kgeraf in captions; probably the definition of \kd should be $k$\mbox{-}d

Here's a working version.

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

enter image description here