[Tex/LaTex] Self-defined Width of the Pseudocode Box with algorithmicx

algorithmicxalgorithmsframedpseudocodetwo-column

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?

Best Answer

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}