4

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?

2
  • 1
    till to now I never see that someone use geometry packages in beamer slides ... do you can't live without this package?
    – Zarko
    Commented Jun 23, 2016 at 23:12
  • @Zarko I could surely do, if the code manages to compile and the algorithm to be presented in the slides!
    – gsamaras
    Commented Jun 23, 2016 at 23:13

2 Answers 2

7

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

1
  • You should change your name to latex-God. Thanks!
    – gsamaras
    Commented Jun 23, 2016 at 23:24
3

You need to place the algorithm inside a frame. Even then the content is too much to fit on a regular slide. As such, I've placed it inside a box - using an [H] float specifier - and resized it to fit within the frame height.

enter image description here

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

I also removed all the superfluous content that doesn't pertain to this example. Additionally, since this is a presentation, some of the general definitions have been inserted in the code to allow it to compile, especially the use of a [fragile] frame.

Note that algorithm2e typically uses \; to denote line-endings.

If you're using a different theme, then you might want to adjust the <height> parameter in \resizebox{!}{<height>}{...}.

1
  • OK good Werner. However the other answer came first, but I will upvote you, thank you!
    – gsamaras
    Commented Jun 23, 2016 at 23:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .