Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
Related: tex.stackexchange.com/questions/119205/… –  Jubobs Jan 18 at 15:28
1  
Related: tex.stackexchange.com/q/159428/1347 –  Sadeq Dousti Feb 10 at 8:07

1 Answer 1

up vote 5 down vote accepted

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.