Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

During the IEEEtrans.tex, there is a comment below package algorithmic saying that

Do NOT use the algorithm floating environment provided by algorithm.sty (by the same authors) or algorithm2e.sty (by Christophe Fiorio) as IEEE does not use dedicated algorithm float types and packages that provide these will not provide correct IEEE style captions.

How can I make a beautiful box to show the pseudo code while using only the algorithmic package?

share|improve this question
    
You can download the algorithmic package from here: ctan.org/tex-archive/macros/latex/contrib/algorithms It contains a .pdf with instructions on how to do what you are looking for. –  latex fan Jun 22 '12 at 0:03
add comment

2 Answers

up vote 10 down vote accepted

You can try something like this:

\documentclass{IEEEtran}
\pagestyle{empty}
\usepackage{algorithmic}

\newsavebox{\ieeealgbox}
\newenvironment{boxedalgorithmic}
  {\begin{lrbox}{\ieeealgbox}
   \begin{minipage}{\dimexpr\columnwidth-2\fboxsep-2\fboxrule}
   \begin{algorithmic}}
  {\end{algorithmic}
   \end{minipage}
   \end{lrbox}\noindent\fbox{\usebox{\ieeealgbox}}}

\begin{document}

\begin{boxedalgorithmic}
\REQUIRE $n \geq 0 \vee x \neq 0$
\ENSURE $y = x^n$
...
\ENDIF
\ENDWHILE
\end{boxedalgorithmic}

\end{document}

enter image description here

share|improve this answer
    
Many thanks for sharing! I still have a problem about the caption. Which part should I modify if I want to reference this box? Could I write the box part into a Table? –  datcn Jan 6 '12 at 13:05
    
Update: I find the way of adding the title and caption. Move the box part into a '\begin{table}' and '\end{table}' is OK. Thanks again for your help. –  datcn Jan 6 '12 at 13:20
add comment

Instead of Table, you can use a Figure, in which you can add caption and label.

\begin{figure}
\begin{boxedalgorithmic}
\REQUIRE $n \geq 0 \vee x \neq 0$
\ENSURE $y = x^n$
\STATE $y \leftarrow 1$
\IF{$n < 0$}
\STATE $X \leftarrow 1 / x$
\STATE $N \leftarrow -n$
\ELSE
\STATE $X \leftarrow x$
\STATE $N \leftarrow n$
\ENDIF
\WHILE{$N \neq 0$}
\IF{$N$ is even}
\STATE $X \leftarrow X \times X$
\STATE $N \leftarrow N / 2$
\ELSE[$N$ is odd]
\STATE $y \leftarrow y \times X$
\STATE $N \leftarrow N - 1$
\ENDIF
\ENDWHILE
\end{boxedalgorithmic}
\caption{My algorithm}
\label{myalgo}
\end{figure}
share|improve this answer
    
Welcome to TeX.sx! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). –  doncherry Jun 22 '12 at 15:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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