6

I'm having a problem with the package multicol. I'm trying to split an algorithm into two columns. In particular, this algorithm is made up by two procedures. Unfortunately, in this particular case, one procedure is definitely longer than the other.

I'd like to know if there is a way to force multicol to split this algorithm evenly, and better if I can chose whether to split evenly or split by function on a case-by-case basis.

I've tried using \columnbreak in the middle but it did not work.

\documentclass[10pt,twoside]{report}

\usepackage[utf8]{inputenc}

\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm,bindingoffset=6mm]{geometry}

\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage[numbers]{natbib}
\usepackage{color}

\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage[ruled]{algorithm2e}
\usepackage{multicol}

\begin{document}

\begin{algorithm}
    \caption{My lovely procedure}
\begin{multicols}{2}
    \SetKwFunction{funone}{MyFunction}
    \SetKwFunction{funtwo}{OtherFun}

    \SetKwProg{main}{Algorithm}{}{}
    \main{\funone{b}}{
        \KwData{MyData b}
\nl     \While{this is true}{
\nl         Do X\;
\nl         Do X\;
        }
\nl     Do X\;
    }

    \setcounter{AlgoLine}{0}
    \SetKwProg{foo}{Procedure}{}{}
    \foo{\funtwo{h, s, d}}{
        \KwData{MyData h, s, d}
\nl     Do X\;
\nl     Do X\;
\nl     \If{Is his true?}{
\nl         \eIf{Is this true?}{
\nl             Do X\;
            }{
\nl             Do X\;
\nl             Do X\;
\nl             Do X\;
            }
        }
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
    }
\end{multicols}
\end{algorithm}

\end{document}
Svalorzen
  • 296

1 Answers1

7

If you really want to break it evenly, you can put it into a single column and use \clipbox from the adjustbox package to literally cut it in half.

clipped

\documentclass[10pt,twoside]{report}

\usepackage[utf8]{inputenc}

\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm,bindingoffset=6mm]{geometry}

\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage[numbers]{natbib}
\usepackage{color}

\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage[ruled]{algorithm2e}
\usepackage{adjustbox}

\newsavebox{\tempbox}

\begin{document}

\savebox{\tempbox}{% create image
\begin{minipage}[c]{0.45\textwidth}%
\begin{algorithm*}[H]
    \SetKwFunction{funone}{MyFunction}
    \SetKwFunction{funtwo}{OtherFun}

    \SetKwProg{main}{Algorithm}{}{}
    \main{\funone{b}}{
        \KwData{MyData b}
\nl     \While{this is true}{
\nl         Do X\;
\nl         Do X\;
    }
\nl     Do X\;
    }
    \setcounter{AlgoLine}{0}
    \SetKwProg{foo}{Procedure}{}{}
    \foo{\funtwo{h, s, d}}{
        \KwData{MyData h, s, d}
\nl     Do X\;
\nl     Do X\;
\nl     \If{Is his true?}{
\nl         \eIf{Is this true?}{
\nl             Do X\;
            }{
\nl             Do X\;
\nl             Do X\;
\nl             Do X\;
            }
        }
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
\nl     Do X\;
    }
\end{algorithm*}%
\end{minipage}}

\begin{algorithm}
\caption{My lovely procedure}
\clipbox{0pt {\depth} 0pt {\baselineskip}}{\usebox{\tempbox}}\hfill
\raisebox{\depth}{\clipbox{0pt 1ex 0pt {\height}}{\usebox{\tempbox}}}
\end{algorithm}

\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • It does look somewhat weird though. Do you think otherwise I could do something like create a procedure with no title, split the code between the two and try to sneak it in like that? – Svalorzen Oct 06 '14 at 14:59
  • Yes, it would probably be better to put the title and the caption outside the \savebox. I'm not sure if hyperref would even be able to locate it otherwise. This would mean two separate expansions of algorithm. – John Kormylo Oct 06 '14 at 19:22
  • Okay, I modified the solution to separate the caption from the \savebox. – John Kormylo Oct 06 '14 at 19:41