6

I am writing a 2-column paper. I have 2 algorithms each within \begin{multicols} ... \end{multicols} environment. I want to scale each of them to half the size so that I can fit it side-by-side. How do I do it? I cannot use scalebox or resizebox as they are throwing LaTeX Error: Not in outer par mode. error.

Right now the MWE I have is as follows

\documentclass[twocolumn]{article}
\usepackage[linesnumbered]{algorithm2e}
\usepackage{multicol}
\usepackage{relsize}

\begin{document}
\begin{algorithm*}
\begin{multicols}{2}
\SetStartEndCondition{ }{}{}%
\SetKwProg{Fn}{def}{\string:}{}
\SetKwFunction{Range}{range}%%
\SetKwProg{Proc}{Proc}{\string:}{}
\SetKwProg{Call}{Call}{\string;}{}
\SetKwProg{Algo}{Algo}{\string:}{}
\SetKwInOut{Input}{Input}
\SetKw{KwTo}{in}\SetKwFor{For}{for}{\string:}{}%
\SetKw{KwTo}{in}\SetKwFor{Foreach}{for each}{\string:}{}%
\SetKwIF{If}{ElseIf}{Else}{if}{:}{elif}{else:}{}%
\SetKwFor{While}{while}{:}{fintq}%
\newcommand{\forcond}{$i=0$ \KwTo $n$}
\renewcommand{\forcond}{$i$ \KwTo\Range{$n$}}
\AlgoDontDisplayBlockMarkers\SetAlgoNoEnd\SetAlgoNoLine%

\Input{some $i$} 
%(reward function assume given)}

\Algo{Algorithm 1}

\Call{Function 1}

\While {something} {
    \Foreach{other things}{
        \eIf {at all anything} {
            do something
             } { %else part
            do something else }

    \Call{Function 2}

    do more stuff\;

    \Foreach{many things}{
        do x, y, z
    }
}
}

\Proc{Function 1}

$x = y +z $\;

\Foreach{$x$}{
    do something to each $x$
    \Foreach{$y$}{
    do something to $y$
    }
}

\vspace*{0.25cm}

\Proc{Function 2}

\Foreach{$z$}{
        initialize something 

        \Foreach{$z \times z$} {
            \If{$z^2 > 4$} {
                it is greater than 4\;
                add 9}
        }
    }
\end{multicols}
\vspace*{0.25cm}
\caption{Sample Algorithm}
\label{samplealgo}
\end{algorithm*}

\begin{algorithm*}
\begin{multicols}{2}
\SetStartEndCondition{ }{}{}%
\SetKwProg{Fn}{def}{\string:}{}
\SetKwFunction{Range}{range}%%
\SetKwProg{Proc}{Proc}{\string:}{}
\SetKwProg{Call}{Call}{\string;}{}
\SetKwProg{Algo}{Algo}{\string:}{}
\SetKwInOut{Input}{Input}
\SetKw{KwTo}{in}\SetKwFor{For}{for}{\string:}{}%
\SetKw{KwTo}{in}\SetKwFor{Foreach}{for each}{\string:}{}%
\SetKwIF{If}{ElseIf}{Else}{if}{:}{elif}{else:}{}%
\SetKwFor{While}{while}{:}{fintq}%
\newcommand{\forcond}{$i=0$ \KwTo $n$}
\renewcommand{\forcond}{$i$ \KwTo\Range{$n$}}
\AlgoDontDisplayBlockMarkers\SetAlgoNoEnd\SetAlgoNoLine%

\Input{some more inputs} 
%(reward function assume given)}

\Algo{Algorithm 2}

\Call{Function 1}

\While {something} {
    \Foreach{other things}{
        \eIf {at all anything} {
            do something
             } { %else part
            do something else }

    \Call{Function 2}

    do more stuff\;

    \Foreach{many things}{
        do x, y, z
    }
}
}

\Proc{Function 1}

$x = y +z $\;

\Foreach{$x$}{
    do something to each $x$
    \Foreach{$y$}{
    do something to $y$
    }
}

\vspace*{0.25cm}

\Proc{Function 2}

\Foreach{$z$}{
        initialize something 

        \Foreach{$z \times z$} {
            \If{$z^2 > 4$} {
                it is greater than 4\;
                add 9}
        }
    }
\end{multicols}
\vspace*{0.25cm}
\caption{Sample 2 Algorithm}
\label{samplealgo}
\end{algorithm*}

\end{document}

I cannot use \begin{multicols}{4} ... \end{multicols} since the text in the algorithm is much larger than 1/4 of the page. Hence I HAVE to use resizebox or something equivalent.

  • 1
    Perhaps a minipage inside each column Will do the trick. (You can put a scaled box in there.) – JPi Feb 2 '16 at 12:24
  • With multicols package, I cannot specify the column explicitly. So, can you help me how? – okkhoy Feb 2 '16 at 14:19
5

A good deal of dirty tricks!

\documentclass[twocolumn]{article}
\usepackage[linesnumbered]{algorithm2e}
\usepackage{multicol}
\usepackage{graphicx}

\usepackage{lipsum}

\begin{document}

\lipsum[1-3]

\begin{figure*}% for getting a float
\begingroup % trick algorithm2e into thinking we're in one column mode
\csname @twocolumnfalse\endcsname
\noindent
\resizebox{.49\textwidth}{!}{%
\begin{minipage}{.7\textwidth}
\begin{algorithm}[H]
\begin{multicols}{2}
\SetStartEndCondition{ }{}{}%
\SetKwProg{Fn}{def}{\string:}{}
\SetKwFunction{Range}{range}%%
\SetKwProg{Proc}{Proc}{\string:}{}
\SetKwProg{Call}{Call}{\string;}{}
\SetKwProg{Algo}{Algo}{\string:}{}
\SetKwInOut{Input}{Input}
\SetKw{KwTo}{in}\SetKwFor{For}{for}{\string:}{}%
\SetKw{KwTo}{in}\SetKwFor{Foreach}{for each}{\string:}{}%
\SetKwIF{If}{ElseIf}{Else}{if}{:}{elif}{else:}{}%
\SetKwFor{While}{while}{:}{fintq}%
\newcommand{\forcond}{$i=0$ \KwTo $n$}
\renewcommand{\forcond}{$i$ \KwTo\Range{$n$}}
\AlgoDontDisplayBlockMarkers\SetAlgoNoEnd\SetAlgoNoLine%

\Input{some $i$} 
%(reward function assume given)}

\Algo{Algorithm 1}

\Call{Function 1}

\While {something} {
    \Foreach{other things}{
        \eIf {at all anything} {
            do something
             } { %else part
            do something else }

    \Call{Function 2}

    do more stuff\;

    \Foreach{many things}{
        do x, y, z
    }
}
}

\Proc{Function 1}

$x = y +z $\;

\Foreach{$x$}{
    do something to each $x$
    \Foreach{$y$}{
    do something to $y$
    }
}

\vspace*{0.25cm}

\Proc{Function 2}

\Foreach{$z$}{
        initialize something 

        \Foreach{$z \times z$} {
            \If{$z^2 > 4$} {
                it is greater than 4\;
                add 9}
        }
    }
\end{multicols}
\vspace*{0.25cm}
\caption{Sample Algorithm}
\label{samplealgo}
\end{algorithm}
\end{minipage}%
}% <------------- end of \resizebox
\hfill
\resizebox{.49\textwidth}{!}{%
\begin{minipage}{.7\textwidth}
\begin{algorithm}[H]
\begin{multicols}{2}
\SetStartEndCondition{ }{}{}%
\SetKwProg{Fn}{def}{\string:}{}
\SetKwFunction{Range}{range}%%
\SetKwProg{Proc}{Proc}{\string:}{}
\SetKwProg{Call}{Call}{\string;}{}
\SetKwProg{Algo}{Algo}{\string:}{}
\SetKwInOut{Input}{Input}
\SetKw{KwTo}{in}\SetKwFor{For}{for}{\string:}{}%
\SetKw{KwTo}{in}\SetKwFor{Foreach}{for each}{\string:}{}%
\SetKwIF{If}{ElseIf}{Else}{if}{:}{elif}{else:}{}%
\SetKwFor{While}{while}{:}{fintq}%
\newcommand{\forcond}{$i=0$ \KwTo $n$}
\renewcommand{\forcond}{$i$ \KwTo\Range{$n$}}
\AlgoDontDisplayBlockMarkers\SetAlgoNoEnd\SetAlgoNoLine%

\Input{some more inputs} 
%(reward function assume given)}

\Algo{Algorithm 2}

\Call{Function 1}

\While {something} {
    \Foreach{other things}{
        \eIf {at all anything} {
            do something
             } { %else part
            do something else }

    \Call{Function 2}

    do more stuff\;

    \Foreach{many things}{
        do x, y, z
    }
}
}

\Proc{Function 1}

$x = y +z $\;

\Foreach{$x$}{
    do something to each $x$
    \Foreach{$y$}{
    do something to $y$
    }
}

\vspace*{0.25cm}

\Proc{Function 2}

\Foreach{$z$}{
        initialize something 

        \Foreach{$z \times z$} {
            \If{$z^2 > 4$} {
                it is greater than 4\;
                add 9}
        }
    }
\end{multicols}
\vspace*{0.25cm}
\caption{Sample 2 Algorithm}
\label{samplealgo}
\end{algorithm}
\end{minipage}%
}% <------------- end of \resizebox
\endgroup
\end{figure*}

\lipsum[4-20]

\end{document}

enter image description here

  • thank you @egreg, surprisingly, following many other threads, and building one top of one another I came up with a very similar solution, using \scalebox instead of \resizebox! – okkhoy Feb 2 '16 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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