# Splitting an algorithm across two columns

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:

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:

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.)

-
– Jubobs Jan 18 '14 at 15:28
– M.S. Dousti Feb 10 '14 at 8:07

## algorithm & algorithmic

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

\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.

\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}

-