[Tex/LaTex] How to put \algorithm and \algorithmic environments inside a box

algorithmicboxes

I want to put an algorithm inside a box. Here is the code

\fbox{\parbox{0.8\linewidth}{
\begin{algorithm}
\caption{My algorithm}
\begin{algorithmic}[1]
  \scriptsize
...
\end{algorithmic}
\end{algorithm}

}}

I am getting this error:

 Not in outer par mode

following other error at the line where the two ending curly brackets are. The problem is solved when i remove the algorithm environment and i have only the algorithmic. But i want to have algorithm environment to put the caption.

Best Answer

To make that a real answer: If you want a box around

\documentclass{article}

\usepackage{algorithm,algorithmic}

\begin{document}
\begin{algorithm}
\caption{My algorithm}
\begin{algorithmic}[1]
  \scriptsize
  \STATE Statement
\end{algorithmic}
\end{algorithm}
\end{document}

original output

I suggest to switch to the algorithm2e package. The above can be roughly mimicked with

\documentclass{article}

\usepackage{algorithm2e}

\begin{document}
% \RestyleAlgo{boxed}
\RestyleAlgo{boxruled}
\LinesNumbered
\begin{algorithm}[ht]
  \caption{My algorithm\label{alg}}
  Statement
\end{algorithm}
\end{document}

boxed output