2

I have two algorithms wrote by the algorithm2e package. One of them is large and the other is small. My try is here:

%\documentclass[runningheads,a4paper]{llncs}
\documentclass{article}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{amsmath}
\usepackage[noline]{algorithm2e}

\begin{document}

    %%my first try by using minipage
    \begin{minipage}{\linewidth}
        \begin{center}
            \begin{minipage}{0.65\linewidth}
                \begin{algorithm}[H]
                    \DontPrintSemicolon
                    \SetKwProg{Fn}{Function}{}{end}
                    \Fn{$u^h \leftarrow $V-cycle($A_h, u_0^h, f_h$)}{
                        \uIf{$h==h_0$}{\KwRet{$u_h \leftarrow A_h^{-1}f_h $}\tcc*[r]{bottom solve}}
                        $u^h \leftarrow \text{smooth}(A_h,u_0^h,f_h)$\tcc*[r]{pre-smooth}
                        $r^h \leftarrow f_h-A_hu^h$\tcc*[r]{residual}
                        $r^{2h} \leftarrow I_h^{2h}r^h$\tcc*[r]{restriction}
                        $\delta^{2h} \leftarrow \text{V-cycle}(A_{2h},0,r^{2h})$\tcc*[r]{recursive call}
                        $u^h \leftarrow u^h + I_{2h}^h\delta^{2h}$\tcc*[r]{interpolation}
                        $u^h \leftarrow \text{smooth}(A_{2h},u^h,f_h)$\tcc*[r]{post-smooth}
                        \KwRet{$u^h$}\;
                    }   
                    \caption{V-cycle}   
                \end{algorithm}
            \end{minipage}%
            \begin{minipage}{0.35\linewidth}
                \begin{algorithm}[H]
                    \DontPrintSemicolon     
                    $h \leftarrow h_0$\;
                    $u^h \leftarrow A_h^{-1}f_h$\;
                    \While{!done}{
                        $u^{h/2} \leftarrow \overline{I}_h^{h/2}u^h$\;
                        $h \leftarrow \frac{h}{2}$\;
                        $u^h \leftarrow \text{V-cycle}(A_h,u^h,f_h)$\;
                    }
                    \caption{F-cycle}
                \end{algorithm}
            \end{minipage}
        \end{center}
    \end{minipage}


%%my second try by using subfigure and minipage
    \begin{figure}
        \begin{center}
            \subfigure[V-cycle]{
                \begin{minipage}{0.7\linewidth}
                    \begin{algorithm}[H]
                        \DontPrintSemicolon
                        \SetKwProg{Fn}{Function}{}{end}
                        \Fn{$u^h \leftarrow $V-cycle($A_h, u_0^h, f_h$)}{
                            \uIf{$h==h_0$}{\KwRet{$u_h \leftarrow A_h^{-1}f_h $}\tcc*[r]{bottom solve}}
                            $u^h \leftarrow \text{smooth}(A_h,u_0^h,f_h)$\tcc*[r]{pre-smooth}
                            $r^h \leftarrow f_h-A_hu^h$\tcc*[r]{residual}
                            $r^{2h} \leftarrow I_h^{2h}r^h$\tcc*[r]{restriction}
                            $\delta^{2h} \leftarrow \text{V-cycle}(A_{2h},0,r^{2h})$\tcc*[r]{recursive call}
                            $u^h \leftarrow u^h + I_{2h}^h\delta^{2h}$\tcc*[r]{interpolation}
                            $u^h \leftarrow \text{smooth}(A_{2h},u^h,f_h)$\tcc*[r]{post-smooth}
                            \KwRet{$u^h$}\;
                        }       
                    \end{algorithm}
                \end{minipage}
            }%
            \subfigure[F-cycle]{
                \begin{minipage}{0.3\linewidth}
                    \begin{algorithm}[H]
                        \DontPrintSemicolon     
                        $h \leftarrow h_0$\;
                        $u^h \leftarrow A_h^{-1}f_h$\;
                        \While{!done}{
                            $u^{h/2} \leftarrow \overline{I}_h^{h/2}u^h$\;
                            $h \leftarrow \frac{h}{2}$\;
                            $u^h \leftarrow \text{V-cycle}(A_h,u^h,f_h)$\;
                        }
                    \end{algorithm}
                \end{minipage}
            }
        \end{center}
        \caption{V-cycle and F-cycle}
    \end{figure}

\end{document}

The result of the above is: enter image description here Both of them doesn't look very well for me. I want

  1. The code line will not be broke into two lines.
  2. Comments will be on the same line with the code.
  3. Captions of these algorithms are aligned.

Here is my want: enter image description here

1 Answer 1

1

Below I've used two minipages, one for each algorithm, followed by two more minipages, one for each algorithm caption. The double-usage is required to achieve the appropriate alignment.

enter image description here

\documentclass{article}
\usepackage{amsmath}
\usepackage[noline]{algorithm2e}

\begin{document}

\begin{figure}
  \begin{minipage}{0.65\linewidth}
    \begin{algorithm}[H]
      \DontPrintSemicolon
      \SetKwProg{Fn}{Function}{}{end}
      \Fn{$u^h \leftarrow $V-cycle($A_h, u_0^h, f_h$)}{
        \uIf{$h==h_0$}{\KwRet{$u_h \leftarrow A_h^{-1}f_h $}\tcc*[r]{bottom solve}}
        $u^h \leftarrow \text{smooth}(A_h,u_0^h,f_h)$\tcc*[r]{pre-smooth}
        $r^h \leftarrow f_h-A_hu^h$\tcc*[r]{residual}
        $r^{2h} \leftarrow I_h^{2h}r^h$\tcc*[r]{restriction}
        $\delta^{2h} \leftarrow \text{V-cycle}(A_{2h},0,r^{2h})$\tcc*[r]{recursive call}
        $u^h \leftarrow u^h + I_{2h}^h\delta^{2h}$\tcc*[r]{interpolation}
        $u^h \leftarrow \text{smooth}(A_{2h},u^h,f_h)$\tcc*[r]{post-smooth}
        \KwRet{$u^h$}\;
      }
    \end{algorithm}
  \end{minipage}%
  \begin{minipage}{0.35\linewidth}
    \begin{algorithm}[H]
      \DontPrintSemicolon     
      $h \leftarrow h_0$\;
      $u^h \leftarrow A_h^{-1}f_h$\;
      \While{!done}{
        $u^{h/2} \leftarrow \overline{I}_h^{h/2}u^h$\;
        $h \leftarrow \frac{h}{2}$\;
        $u^h \leftarrow \text{V-cycle}(A_h,u^h,f_h)$\;
      }
    \end{algorithm}
  \end{minipage}

  \begin{minipage}{0.65\linewidth}
    \begin{algorithm}[H]
      \caption{V-cycle}
    \end{algorithm}
  \end{minipage}%
  \begin{minipage}{0.35\linewidth}
    \begin{algorithm}[H]
      \caption{F-cycle}
    \end{algorithm}
  \end{minipage}

  \caption{V-cycle and F-cycle}
\end{figure}


\end{document}

You must log in to answer this question.

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