我有两段伪代码,我想将它们并排放在图中,分别封装在各自的 fbox 中,并且这些 fbox 的高度应相同。有什么建议吗?我将我当前的代码(不起作用)粘贴在下面,并附上其屏幕截图。
\begin{figure}[!h]
\centering
\begin{subfigure}{.45\textwidth}
\centering
\fbox{\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
{\footnotesize
\parbox{2.2in} {
\underline{\texttt{MJRTY}}:\\
\textbf{initialize$()$.}
\vspace{-.1in}\begin{itemize}
\addtolength{\itemsep}{-3mm}
\item[1.] $i \leftarrow \bot$
\item[2.] $C\leftarrow 0$
\end{itemize}
\textbf{update$(j)$.} \texttt{// process item $j$ in stream}
\vspace{-.4in}\begin{itemize}
\addtolength{\itemsep}{-3mm}
\medskip
\smallskip
\item[1.] \textbf{if} $i=j$:
\item[] \qquad $C\leftarrow C+1$
\item[2.] \textbf{else}:
\item[] \qquad $C\leftarrow \max\{0, C-1\}$
\item[3.] \qquad \textbf{if} $C=0$:
\item[] \qquad \qquad $i\leftarrow j$
\item[] \qquad \qquad $C\leftarrow 1$
\end{itemize}
\textbf{query$()$.}
\vspace{-.2in}\begin{itemize}
\addtolength{\itemsep}{-2mm}
\smallskip
\item[1.] \textbf{return} $i$
% \item[] \phantom{ }
\end{itemize}
}}\end{minipage}}
\caption{Pseudocode for \textsf{MJRTY}.}\label{fig:mjrty}
\end{subfigure}
\begin{subfigure}{.45\textwidth}
\centering
\fbox{\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
{\footnotesize
\parbox{2.2in} {
\underline{\texttt{Frequent}}:\\
\textbf{initialize$(k)$.}
\vspace{-.1in}\begin{itemize}
\addtolength{\itemsep}{-3mm}
\item[1.] $i_1,\ldots,i_{k-1}\leftarrow \bot$
\item[2.] $C_1,\ldots,C_{k-1}\leftarrow 0$
\end{itemize}
\textbf{update$(j)$.} \texttt{// process item $j$ in stream}
\vspace{-.4in}\begin{itemize}
\addtolength{\itemsep}{-3mm}
\medskip
\smallskip
\item[1.] \textbf{if} $\exists r$ such that $i_r=j$:
\item[] \qquad $C_r\leftarrow C_r+1$
\item[2.] \textbf{else}:
\item[] \qquad \textbf{for} $r=1,\ldots,k-1$:
\item[] \qquad\qquad $C_r\leftarrow \max\{0,C_r-1\}$
\item[3.] \qquad \textbf{if} $\exists\ r$ such that $C_r=0$:
\item[] \qquad\qquad pick such $r$ arbitrarily
\item[] \qquad \qquad $i_r\leftarrow j$
\item[] \qquad \qquad $C_r\leftarrow 1$
\end{itemize}
\textbf{query$()$.}
\vspace{-.2in}\begin{itemize}
\addtolength{\itemsep}{-2mm}
\smallskip
\item[1.] \textbf{return} $\{i_1,\ldots,i_{k-1}\}\backslash\{\bot\}$
\end{itemize}
}}\end{minipage}}
\caption{Pseudocode for \textsf{Frequent}.}\label{fig:frequent}
\end{subfigure}
\caption{Pseudocode for \textsf{MJRTY} and \textsf{Frequent}.}
\end{figure}
答案1
您可以使用可选参数(此处为 5cm)指定 minipage 的高度,如图所示,代码全部叠印,因为您已减小项目分离度,并且从默认值开始,这会使项目分离度为负数,但据推测这是您未显示的更正代码。我保留了它,因为它与框高度问题无关。
我猜你正在使用subcaption
来定义subfigure
,你没有说,并且subfigure
包在你的测试片段上给出了语法错误。
\documentclass[a4paper]{article}
\addtolength\textwidth{1cm}
\usepackage{subcaption}
\begin{document}
\begin{figure}[!h]
\centering
\begin{subfigure}{.45\textwidth}
\centering
\fbox{\begin{minipage}[t][5cm]{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
{\footnotesize
\parbox{2.2in} {
\underline{\texttt{MJRTY}}:\\
\textbf{initialize$()$.}
\vspace{-.1in}\begin{itemize}
\addtolength{\itemsep}{-3mm}
\item[1.] $i \leftarrow \bot$
\item[2.] $C\leftarrow 0$
\end{itemize}
\textbf{update$(j)$.} \texttt{// process item $j$ in stream}
\vspace{-.4in}\begin{itemize}
\addtolength{\itemsep}{-3mm}
\medskip
\smallskip
\item[1.] \textbf{if} $i=j$:
\item[] \qquad $C\leftarrow C+1$
\item[2.] \textbf{else}:
\item[] \qquad $C\leftarrow \max\{0, C-1\}$
\item[3.] \qquad \textbf{if} $C=0$:
\item[] \qquad \qquad $i\leftarrow j$
\item[] \qquad \qquad $C\leftarrow 1$
\end{itemize}
\textbf{query$()$.}
\vspace{-.2in}\begin{itemize}
\addtolength{\itemsep}{-2mm}
\smallskip
\item[1.] \textbf{return} $i$
% \item[] \phantom{ }
\end{itemize}
}}\end{minipage}}
\caption{Pseudocode for \textsf{MJRTY}.}\label{fig:mjrty}
\end{subfigure}
\begin{subfigure}{.45\textwidth}
\centering
\fbox{\begin{minipage}[t][5cm]{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
{\footnotesize
\parbox{2.2in} {
\underline{\texttt{Frequent}}:\\
\textbf{initialize$(k)$.}
\vspace{-.1in}\begin{itemize}
\addtolength{\itemsep}{-3mm}
\item[1.] $i_1,\ldots,i_{k-1}\leftarrow \bot$
\item[2.] $C_1,\ldots,C_{k-1}\leftarrow 0$
\end{itemize}
\textbf{update$(j)$.} \texttt{// process item $j$ in stream}
\vspace{-.4in}\begin{itemize}
\addtolength{\itemsep}{-3mm}
\medskip
\smallskip
\item[1.] \textbf{if} $\exists r$ such that $i_r=j$:
\item[] \qquad $C_r\leftarrow C_r+1$
\item[2.] \textbf{else}:
\item[] \qquad \textbf{for} $r=1,\ldots,k-1$:
\item[] \qquad\qquad $C_r\leftarrow \max\{0,C_r-1\}$
\item[3.] \qquad \textbf{if} $\exists\ r$ such that $C_r=0$:
\item[] \qquad\qquad pick such $r$ arbitrarily
\item[] \qquad \qquad $i_r\leftarrow j$
\item[] \qquad \qquad $C_r\leftarrow 1$
\end{itemize}
\textbf{query$()$.}
\vspace{-.2in}\begin{itemize}
\addtolength{\itemsep}{-2mm}
\smallskip
\item[1.] \textbf{return} $\{i_1,\ldots,i_{k-1}\}\backslash\{\bot\}$
\end{itemize}
}}\end{minipage}}
\caption{Pseudocode for \textsf{Frequent}.}\label{fig:frequent}
\end{subfigure}
\caption{Pseudocode for \textsf{MJRTY} and \textsf{Frequent}.}
\end{figure}
\end{document}