需要在单个图形中使用多种算法,并且每种算法都有子标题

需要在单个图形中使用多种算法,并且每种算法都有子标题
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\algtext*{EndIf}% Remove \EndIf
\renewcommand{\thealgorithm}{}
\usepackage{amsmath,amssymb}
\usepackage{subcaption}
\usepackage{comment}
\begin{document}

\begin{algorithm}
\caption{$Game PrvInd_{G,\phi}$}
\label{pseudoPSO}
\begin{algorithmic}
\Procedure{Garble}{$f_0, f_1, x_0, x_1$} 

    \If{$f(\phi(f0) \neq\phi(f1)$} 
    \Return $\perp$ \EndIf
    \If {$ev(f_0, x_0) \neq ev(f_1, x_1)$}
    \Return $\perp$ \EndIf
    \If{$\{x_0,x_1\} \nsubseteq \{0,1\}^{{f_0}.n}$} \Return $\perp$ \EndIf
    $(F, e, d) \leftarrow Gb(1^k, f_b)$; 
    $X \leftarrow En(e, x_b)$
    \Return (F,X, d) 
\EndProcedure
\end{algorithmic}
\end{algorithm}

\begin{algorithm}
\caption{$Game PrvInd_{G,\phi,S}$}
\label{pseudoPSO}
\begin{algorithmic}
\Procedure{Garble}{f,x,} 

    \If{$x\notin\{0,1\}^{{f}.n}$}
    \Return $\perp$ \EndIf
    \If {b=1}
    $(F, e, d) \leftarrow Gb(1^k, f)$; 
     $X \leftarrow En(e, x)$
    \Else $ y \leftarrow ev(f, x); (F,X, d) \leftarrow S(1^k, y,\phi(f))$
    \EndIf
    \Return (F,X, d) 
\EndProcedure
\end{algorithmic}
\end{algorithm

\end{document}

我想在单个图中显示多个算法,并在右侧为每个算法添加子标题,如下图所示: 期望输出

但我得到的是这个(仅显示前两种算法:)

输出

还有其他库吗?

答案1

以下实现了您的目标 - 在内部设置每个算法tabular(应用适当的规则(顶部和底部),然后您可以仅使用环境algorithmic来设置伪代码:

在此处输入图片描述

\documentclass{article}

\usepackage[margin=0.5in]{geometry}

\usepackage{algorithm,algpseudocode,amsmath,amssymb}
\algtext*{EndIf}% Remove \EndIf
\renewcommand{\thealgorithm}{}

\begin{document}

\begin{figure}
  \begin{tabular}[t]{@{} p{.49\linewidth} @{}}
    \hline
    \begin{algorithmic}
      \Procedure{Garble}{$f_0, f_1, x_0, x_1$}\hfill $\text{Game PrvInd}_{G,\phi}$
        \If{$f(\phi(f0) \neq \phi(f1))$} \Return $\perp$
        \EndIf
        \If{$\text{ev}(f_0, x_0) \neq \text{ev}(f_1, x_1)$} \Return $\perp$
        \EndIf
        \If{$\{x_0,x_1\} \nsubseteq \{0,1\}^{{f_0} \cdot n}$} \Return $\perp$
        \EndIf
        \State $(F, e, d) \leftarrow \text{Gb}(1^k, f_b)$;
        $X \leftarrow \text{En}(e, x_b)$
        \Return $(F, X, d)$
      \EndProcedure
    \end{algorithmic} \\
    \hline
  \end{tabular}\hfill
  \begin{tabular}[t]{@{} p{.49\linewidth} @{}}
    \hline
    \begin{algorithmic}
      \Procedure{Garble}{$f,x$}\hfill $\text{Game PrvInd}_{G,\phi,S}$
        \If{$x \notin \{0,1\}^{{f} \cdot n}$} \Return $\perp$
        \EndIf
        \If{$b = 1$} $(F, e, d) \leftarrow \text{Gb}(1^k, f)$;
          $X \leftarrow \text{En}(e, x)$
        \Else{} $y \leftarrow \text{ev}(f, x)$; $(F, X, d) \leftarrow S(1^k, y, \phi(f))$
        \EndIf
        \State \Return $(F, X, d)$
      \EndProcedure
    \end{algorithmic} \\
    \hline
  \end{tabular}
  \caption{Some games and their definitions.}
\end{figure}

\end{document}

相关内容