5

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.

  • 2
    Have a look at the algorithm2e, or the listings package. With the old algorithm stuff this is not easily possible. – mafp Apr 23 '13 at 9:36
  • algorithm is a float environment. If you put the box only in algorithmic it should work. \begin{algorithm}\fbox{\begin{minipage}[b]{\textwidth} \begin{algorithmic} ... \end{agorithmic} \end{minipage}} end{algorithm} – cacamailg Apr 23 '13 at 10:31
  • Is there any need for using \fbox and \parbox instead of algorithm2e box features ? see more primitive TeX boxes – texenthusiast Apr 24 '13 at 8:42
11

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

|improve this answer|||||
  • Yes but the first approach is not surrounded by a box – curious Apr 24 '13 at 0:21
  • No it's not. In my original i have the fbox and pbox commands. – curious Apr 24 '13 at 7:48
  • @curious Yeah I removed the nonworking part to show something. – mafp Apr 24 '13 at 7:53
  • 1
    If you remove it this is not an answer to my question.As it is not why i want to do. – curious Apr 24 '13 at 8:00
  • @curious Now you even downvoted me? What the hell ... I just showed how a working example of algorithmic would look like, and then how a boxed version using algorithm2e looks like. – mafp Apr 24 '13 at 8:09
4

You can not have any kind of float in a box, you would get the same from figure or table, The most direct solution is not to use a float and use the (one line) capt-of package to allow use of a caption

\usepackage{capt-of}

then

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

}}

However it is probably better to use an algorithm package that allows a boxed float style (or use the float package to style such floats) then the box is inside the float so the construct may still be floated by LaTeX.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.