[Tex/LaTex] Export algorithm as pdf

algorithm2e

I use algorithm2e package for my algorithms and want to export my algorithm as pdf and could not find a way to do it. Is there any way to do this?

The reason is that I use a style file provided to me and when I use this style file, the space between the lines gets huge and none of my algorithms fits into a page. I would like to export them as pdf and then include them.

Below is an example of what I want to do, it exports the given tikz picture as pdf. I want to put an algorithm and get a pdf which includes the algorithm as if it is an image or graphics.

\documentclass{article}

\usepackage{tikz,nicefrac,amsmath,pifont}
\usetikzlibrary{arrows,snakes,backgrounds,patterns,matrix,shapes,fit,calc,shadows,plotmarks}
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}
\usepackage[graphics,tightpage,active]{preview}
\PreviewEnvironment{tikzpicture}
\newlength{\imagewidth}
\newlength{\imagescale}

\begin{document}

\begin{tikzpicture}[<->,>=stealth,auto,shorten >=1pt]
    \tikzstyle{agent}=[rectangle,draw,rounded corners,text
    centered,node distance=2cm,text width=1.1cm,scale=0.9];
    \tikzstyle{ont}=[ellipse,draw,node distance=2.4cm,text
    width=1.7cm,scale=0.9,text centered];
    \tikzstyle{edge}=[<->,scale=0.9]

    \node at (0,0.5) [agent] (UA) {UA}; 
    \node at (3,-1.5) [ont] (EO) {Environment\\Ontology}; 
    \node at (3,2.5) [ont] (DO) {Domain\\Ontology}; 
    \node at (7,2) [agent] (A1) {Agent\\1}; 
    \node at (7,0.5) [agent] (A2) {Agent\\2}; 
    \node at (7,-1) [agent] (A3) {Agent\\3};
    \path 
      (A1) edge (UA) 
           edge[->,dashed] (EO) 
           edge[->,dashed] (DO)
      (A2) edge (UA) 
           edge[->,dashed] (EO) 
           edge[->,dashed] (DO)
      (A3) edge (UA) 
           edge[->,dashed] (EO) 
           edge[->,dashed] (DO)
      (UA) edge[->,dashed] (EO) 
    edge[->,dashed] (DO);      
  \end{tikzpicture}
\end{document}

I do not use any packages like setspace in my latex code. The provided style file is here.

Best Answer

You can use the standalone document class; a little example:

\documentclass{standalone}
\usepackage{algorithm2e}

\begin{document}

\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}

\end{document}