3

This post has beautifully shown how to make the algorithm pseudocode box as wide as one column or two columns.

I am now writing a pseudocode with many if layers. So a one-column box cannot suffice. However, the layers are not that many to span over two columns. So my problem is that if I use the two-column box, the RHS is too empty and the comments are too far away from the code.

One-column, too narrow. Two-column, too wide.

Can I define the width of the algorithm box myself, say 1.5 columns wide?

1 Answer 1

3

You can place the algorithmic environment inside a minipage of desired width (for example, 1.5\columnwidth):

enter image description here

\documentclass{IEEEtran}
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{float}% http://ctan.org/pkg/float
\floatstyle{boxed} % Box...
\restylefloat{figure}% ...figure environment contents.
\begin{document}
\section{A section}
\begin{figure*}
  \centering
  \caption{Euclid’s algorithm}\label{euclid}
  \begin{minipage}{1.5\columnwidth}
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
      \State $r\gets a\bmod b$
      \While{$r\not=0$}\Comment{We have the answer if r is 0}
        \State $a\gets b$
        \State $b\gets r$
        \State $r\gets a\bmod b$
      \EndWhile\label{euclidendwhile}
      \State \textbf{return} $b$\Comment{The gcd is b}
    \EndProcedure
  \end{algorithmic}
  \end{minipage}
\end{figure*}

\lipsum[1-15]% dummy text
\end{document}

To reduce the box size itself, it is far more convenient to remove the boxed float style, and mere wrap the minipaged algorithmic environment inside an \fbox:

enter image description here

\documentclass{IEEEtran}
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{float}% http://ctan.org/pkg/float
%\floatstyle{boxed} % Box...
\restylefloat{figure}% ...figure environment contents.
\begin{document}
\section{A section}
\begin{figure*}
  \centering
  \caption{Euclid’s algorithm}\label{euclid}
  \fbox{\begin{minipage}{1.5\columnwidth}
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
      \State $r\gets a\bmod b$
      \While{$r\not=0$}\Comment{We have the answer if r is 0}
        \State $a\gets b$
        \State $b\gets r$
        \State $r\gets a\bmod b$
      \EndWhile\label{euclidendwhile}
      \State \textbf{return} $b$\Comment{The gcd is b}
    \EndProcedure
  \end{algorithmic}
  \end{minipage}}
\end{figure*}

\lipsum[1-15]% dummy text
\end{document}
1
  • cool it works! only one minor adjsutment to be done. The upper and lower boundaries of the algorithm box remain unchanged, still 2 columns. How to make it also on the minipage? THanks:) Sep 9, 2013 at 15:55

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.