6

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.

3
  • 2
    Have a look at the algorithm2e, or the listings package. With the old algorithm stuff this is not easily possible.
    – mafp
    Commented Apr 23, 2013 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
    Commented Apr 23, 2013 at 10:31
  • Is there any need for using \fbox and \parbox instead of algorithm2e box features ? see more primitive TeX boxes Commented Apr 24, 2013 at 8:42

3 Answers 3

16

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

6
  • Yes but the first approach is not surrounded by a box
    – curious
    Commented Apr 24, 2013 at 0:21
  • No it's not. In my original i have the fbox and pbox commands.
    – curious
    Commented Apr 24, 2013 at 7:48
  • @curious Yeah I removed the nonworking part to show something.
    – mafp
    Commented Apr 24, 2013 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
    Commented Apr 24, 2013 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
    Commented Apr 24, 2013 at 8:09
5

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.

0

It seems there is yet another quite recent algorithm-like package called algpseudocodex, which indeed allows for drawing boxes around pieces of code. I guess the LaTeX code you are looking for is as simple as

\begin{algorithm}
\caption{My algorithm}
\begin{algorithmic}[1]
    \scriptsize
    \begin{algorithmic}
    \BeginBox
        \State first line
        \State ...
    \EndBox
\end{algorithmic}
\end{algorithm}

after specifying \usepackage{algpseudocodex} in the file header.

You must log in to answer this question.

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