10

Consider the following short example, where a two column page is selected, and the algorithm is split across the two columns using a manual \pagebreak within the algorithm's code:

\documentclass[twocolumn]{article}
\usepackage[english]{babel}
\usepackage{algpseudocode}
\begin{document}

\begin{algorithmic} [1]
    \State Let $a \gets b + c$
    \State Let $a \gets b + c$
    \pagebreak
    \State Let $a \gets b + c$
    \State Let $a \gets b + c$
\end{algorithmic}

\end{document}

Here's the result:

enter image description here

Now, by putting the code inside a float, I no longer can split the algorithm by \pagebreak:

\documentclass[twocolumn]{article}
\usepackage[english]{babel}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm*}[t]
\caption{Test}

\begin{algorithmic} [1]
    \State Let $a \gets b + c$
    \State Let $a \gets b + c$
    \pagebreak
    \State Let $a \gets b + c$
    \State Let $a \gets b + c$
\end{algorithmic}

\end{algorithm*}
\end{document}

and the result:

enter image description here

How can I get the same effect within a float? Could you please suggest a method which is not specific to algpseudocode, and applies to algorithm2e as well? (The latter does not support \pagebreak within the code.)

Sadeq Dousti
  • 4,380

1 Answers1

8

algorithm & algorithmic

Wrap your algorithmic environment into a multicols one. No need to issue a \pagebreak. Reference: Placing a single algorithm in two columns

enter image description here

\documentclass{article}

\usepackage{lipsum}
\usepackage{algorithm}
\usepackage{algpseudocode}

\usepackage{multicol}

\begin{document}

\lipsum[1]

\begin{algorithm}
    \caption{Calculate $y = x^n$}
    \label{alg1}
    \begin{multicols}{2}
        \begin{algorithmic}[1]
            \State Let $a \gets b + c$
            \State Let $a \gets b + c$
            \State Let $a \gets b + c$
            \State Let $a \gets b + c$
        \end{algorithmic}
    \end{multicols}
\end{algorithm}

\lipsum[2]

\end{document}

algorithm2e

Same strategy. Note, however, that, in the example below, no pagebreak can occur within the body of the while loop. In general, because of the way algorithm2e control-flow macros (\e.g. \While{...}) are defined, two-column algorithms typeset with algorithm2e are not likely to look as good as those typeset with algorithm and algorithmic. If I were you, I'd stick with the latter.

enter image description here

\documentclass{article}

\usepackage{lipsum}
\usepackage[algo2e]{algorithm2e}
\usepackage{multicol}

\begin{document}

\lipsum[1]

\begin{algorithm2e}
    \caption{Bisection}
    \label{bisect}
    \begin{multicols}{2}
            \KwIn{function $f$, reals $a,b \in D_f$ s.t. $f(a)f(b)<0$, tolerance $tol$}
            \KwOut{an approximate root of function $f$}
            \While{$(b-a)/2>tol$}
                        {$c \leftarrow (a+b)/2$\;
                        \If{$f(c)=0$}{break}
                        \eIf{$f(a)f(c)<0$}
                                {$b \leftarrow c$}
                                {$a \leftarrow c$}
                        }
            \KwRet{$(a+b)/2$}
    \end{multicols}
\end{algorithm2e}

\lipsum[2]

\end{document}
jub0bs
  • 58,916