我有两个用 algorithm2e 包编写的算法。其中一个很大,另一个很小。我的尝试如下:
%\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}
上述结果是: 对我来说,它们两个看起来都不太好。我想要
- 代码行不会分成两行。
- 注释将与代码位于同一行。
- 这些算法的标题是对齐的。
我想要的是:
答案1
下面我使用了两个minipage
s,每个算法一个,后面又使用了两个minipage
s,每个算法标题一个。需要重复使用才能实现适当的对齐。
\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}