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}
4
  • Algorithm is a float, isn't it? Try using minipages instead. Oct 6, 2014 at 2:55
  • Never mind. That didn't work either. Oct 6, 2014 at 2:58
  • After much experimentation, I have concluded that the output of algorithm must entirely fit in one column. It will not break, period. Oct 6, 2014 at 4:12
  • related: tex.stackexchange.com/questions/154725/…
    – jub0bs
    Oct 6, 2014 at 6:54

1 Answer 1

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}
3
  • 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 6, 2014 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. Oct 6, 2014 at 19:22
  • Okay, I modified the solution to separate the caption from the \savebox. Oct 6, 2014 at 19:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .