1

In another post, @Jubobs suggested to use the multicol package around an algorithmic environment, to typeset the algorithm in two columns.

Recently, I faced a rather aesthetic issue: It might be the case that one column has a long line, while all lines in the other column are short. Example:

An algorithm with a long line

Here's the code for the above picture:

\documentclass[twocolumn]{article}
\usepackage[width=11cm]{geometry}   % page width is reduced to show the effect
\usepackage{multicol}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}
\begin{algorithm*}[t]
    \caption{An algorithm with a long line.}
    \label{alg1}
    \begin{multicols}{2}
        \begin{algorithmic}[1]
            \If{$(x = y^2+1$ and $z=x^3+4y -12)$ }
                \State $a \gets b + c$
            \EndIf

            \columnbreak

            \State $x \gets 0$
        \end{algorithmic}
    \end{multicols}
\end{algorithm*}

\end{document} 

Is it possible to typeset an Algorithmicx environment in two columns, but with unequal widths? I tried the vwcol package as suggested in this post, but I wasn't able to make it work for my case.

2

Would this be what you seek? The proposed solution uses varwidth package. Since algorithmic environment is wrapped inside the varwidth, algostore and algorestore is used to continue the numbering.

Note the xx\linewidth can be used to change the width of varwidth

enter image description here

Code:

\documentclass[twocolumn]{article}
\usepackage[width=11cm]{geometry}   % page width is reduced to show the effect
\usepackage{varwidth}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}
%
\begin{algorithm*}[t]
    \caption{An algorithm with a long line.}
    \label{alg1}
    \begin{varwidth}[t]{0.45\textwidth}        % change 0.45 to suit your need
       \begin{algorithmic}[1]
            \If{$(x = y^2+1$ and $z=x^3+4y -12)$ }
                \State $a \gets b + c$
            \EndIf
           \algstore{myalg}
        \end{algorithmic}
    \end{varwidth}\quad\quad\quad              % the gap between columns
 \begin{varwidth}[t]{0.4\textwidth}            % change 0.4 to suit your need
  \begin{algorithmic}[1]
             \algrestore{myalg}
             \State $x \gets 0$
  \end{algorithmic}
  \end{varwidth}
\end{algorithm*}

\end{document} 
  • You can set simply \textwidth in both varwidth. The width is determined by its contents. I think this is what the OP wants. – karlkoeller Feb 9 '14 at 8:33
  • Yes, you are right, Karl. But for the first one, I used it to mimic the OP's image, showing that one has some options. For automation, yes, the contents determine the width. Thanks for bringing that up. – Jesse Feb 9 '14 at 8:49

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.