收缩算法

收缩算法

我正在写一篇 2 栏论文。我的\begin{multicols} ... \end{multicols}环境中各有 2 个算法。我想将它们每个缩放到一半大小,以便可以并排放置。我该怎么做?我无法使用 scalebox 或 resizebox,因为它们会抛出错误LaTeX Error: Not in outer par mode.

现在我拥有的 MWE 如下

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

我无法使用,\begin{multicols}{4} ... \end{multicols}因为算法中的文本比页面的 1/4 大得多。因此我必须使用 resizebox 或类似的东西。

答案1

不少肮脏的伎俩!

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

在此处输入图片描述

相关内容