[Tex/LaTex] using standalone with algorithmicx or algorithm2e

algorithm2ealgorithmicxstandalone

I am trying to compile a standalone file containing pseudocode created with the algorithmicx package.
According to algorithmicx crashes standalone it should work with the preview option set for standalone, however it does not.
Also the mentioned vbox hack does not work.
What am I doing wrong?

The first compiler error:

! Extra }, or forgotten \endgroup.
\@endfloatbox ...pagefalse \outer@nobreak \egroup 
                                                  \color@endbox
l.13 \end{algorithm}

The (generic) pseudocode file:

\documentclass[preview]{standalone}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}
\vbox{
\begin{algorithm}
  \caption{Euclid’s algorithm}\label{euclid}
  \begin{algorithmic}[1]
    \State $r\gets a\bmod b$
  \end{algorithmic}
\end{algorithm}
}
\end{document}

Thank you for your help.

Sven


UPDATE:

Using the algorithm2e package (as suggested by David Carlisle) and not using the vbox lets the pseudocode file compile without errors.

When I use \KwIn{foo} it breaks again. This in turn is fixed by using \usepackage[varwidth]{standalone}. The pseudocode file compiles without errors.

However now when I try to include the pseudocode file in my main file, I get the following compilation error in the algorithm2e.sty when compiling the main file:

! Undefined control sequence.
<argument> \SetAlgoLined 

l.1177 ...etcount,titlenotnumbered,lined,shortend}
                                                  %

The new pseudocode file (without ~\\ there is no line break after \KwIn{foo}):

\documentclass[varwidth]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{algorithm2e}

\begin{document}
\begin{algorithm}
  ~\\
  \KwIn{foo}
  $r\gets a\bmod b$\;  
\end{algorithm}
\end{document}

The main file where pseudocode is included:

\documentclass[a4paper,10pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[subpreambles]{standalone}

\begin{document}
\include{pseudocode}
\end{document}

Best Answer

I've lost track a bit of the various packages but your example runs without error (with or without the \vbox) if you change

\usepackage{algorithm}

to

\usepackage{algorithm2e}