算法环境中的 Multicols

算法环境中的 Multicols

我正在尝试编写一个算法,但由于它无法放在一页纸中,所以我尝试将其放在两列中。

我正在尝试编写的代码如下

\makebox[\linewidth]{
    \begin{minipage}[H]{.85\textwidth}
        \begin{algorithm}[H]
            \caption{Point addition in projective coordinates}
            \label{alg:projective_add}
            \begin{algorithmic}[1]
                \Require $P = (X_1 \colon Y_1 \colon Z_1), Q = (X_2 \colon Y_2 \colon Z_2), E\colon Y^2Z = X^3 -3XZ^2 + bZ^3$
                \Ensure $P + Q = (X_3 \colon Y_3 \colon Z_3)$

                \begin{multicols}{2}
                    \State $t_0 \gets X_1 \cdot X_2$
                    \State $t_1 \gets Y_1 \cdot Y_2$
                    \State $t_2 \gets Z_1 \cdot Z_2$
                    
                    \State $t_3 \gets X_1 + Y_1$
                    \State $t_4 \gets X_2 + Y_2$
                    \State $t_3 \gets t_3 \cdot t_4$
                    
                    \State $t_4 \gets t_0 + t_1$
                    \State $t_3 \gets t_3 - t_4$
                    \State $t_4 \gets Y_1 + Z_1$
                    
                    \State $X3 \gets Y_2 + Z_2$
                    \State $t4 \gets t_4 \cdot X_3$
                    \State $X3 \gets t_1 + t_2$
                    
                    \State $t4 \gets t_4 - X_3$
                    \State $X3 \gets X_1 + Z_1$
                    \State $Y3 \gets X_2 + Z_2$
                    
                    \State $X_3 \gets X_3 \cdot Y_3$
                    \State $Y_3 \gets t_0 + t_2$
                    \State $Y_3 \gets X_3 - Y_3$
                    
                    \State $Z_3 \gets b \cdot t_2$
                    \State $X_3 \gets Y_3 - Z_3$
                    \State $Z_3 \gets X_3 + X_3$
                    
                    \State $X_3 \gets X_3 + Z_3$
                    \State $Z_3 \gets t_1 - X_3$
                    \State $X_3 \gets t_1 + X_3$
                    
                    \State $Y_3 \gets b \cdot Y_3$
                    \State $t_1 \gets t_2 + t_2$
                    \State $t_2 \gets t_1 + t_2$
                    
                    \State $Y_3 \gets Y_3 - t_2$
                    \State $Y_3 \gets Y_3 - t_0$
                    \State $t_1 \gets Y_3 + Y_3$
                    
                    \State $Y_3 \gets t_1 + Y_3$
                    \State $t_1 \gets t_0 + t_0$
                    \State $t_0 \gets t_1 + t_0$
                    
                    \State $t_0 \gets t_0 - t_2$
                    \State $t_1 \gets t_4 \cdot Y_3$
                    \State $t_2 \gets t_0 \cdot Y_3$
                    
                    \State $Y_3 \gets X_3 \cdot Z_3$
                    \State $Y_3 \gets Y_3 + t_2$
                    \State $X_3 \gets t_3 \cdot X_3$
                    
                    \State $X_3 \gets X_3 - t_1$
                    \State $Z_3 \gets t_4 \cdot Z_3$
                    \State $t_1 \gets t_3 \cdot t_0$
                    
                    \State $Z_3 \gets Z_3 + t_1$    
                \end{multicols}
                
            \end{algorithmic}
        \end{algorithm}
    \end{minipage}
}

但是当我尝试这个时,我得到了一个错误。如果我在 \Require 之前启动 multicols,它就可以工作,但我不想这样做。我已经导入了 algpseudocode 和 algorithm。有人知道我该如何让它工作吗?

答案1

\Require使用两个单独的环境将和\Ensure组件与其余语句分开algorithmic。然后将第二个组件包装在里面,multicols并进行一些垂直调整。

在此处输入图片描述

\documentclass{article}

\usepackage{algorithm,algpseudocode}
\usepackage{multicol}

\begin{document}

\noindent
\begin{minipage}{.85\textwidth}
  \begin{algorithm}[H]
    \caption{Point addition in projective coordinates}
    \begin{algorithmic}
      \Require $P = (X_1 \colon Y_1 \colon Z_1), Q = (X_2 \colon Y_2 \colon Z_2), E\colon Y^2Z = X^3 -3XZ^2 + bZ^3$
      \Ensure $P + Q = (X_3 \colon Y_3 \colon Z_3)$
    \end{algorithmic}
    \vspace{-.5\baselineskip}% Adjust to suit your needs
    \begin{multicols}{2}
      \begin{algorithmic}[1]
        \State $t_0 \gets X_1 \cdot X_2$
        \State $t_1 \gets Y_1 \cdot Y_2$
        \State $t_2 \gets Z_1 \cdot Z_2$

        \State $t_3 \gets X_1 + Y_1$
        \State $t_4 \gets X_2 + Y_2$
        \State $t_3 \gets t_3 \cdot t_4$

        \State $t_4 \gets t_0 + t_1$
        \State $t_3 \gets t_3 - t_4$
        \State $t_4 \gets Y_1 + Z_1$

        \State $X3 \gets Y_2 + Z_2$
        \State $t4 \gets t_4 \cdot X_3$
        \State $X3 \gets t_1 + t_2$

        \State $t4 \gets t_4 - X_3$
        \State $X3 \gets X_1 + Z_1$
        \State $Y3 \gets X_2 + Z_2$

        \State $X_3 \gets X_3 \cdot Y_3$
        \State $Y_3 \gets t_0 + t_2$
        \State $Y_3 \gets X_3 - Y_3$

        \State $Z_3 \gets b \cdot t_2$
        \State $X_3 \gets Y_3 - Z_3$
        \State $Z_3 \gets X_3 + X_3$

        \State $X_3 \gets X_3 + Z_3$
        \State $Z_3 \gets t_1 - X_3$
        \State $X_3 \gets t_1 + X_3$

        \State $Y_3 \gets b \cdot Y_3$
        \State $t_1 \gets t_2 + t_2$
        \State $t_2 \gets t_1 + t_2$

        \State $Y_3 \gets Y_3 - t_2$
        \State $Y_3 \gets Y_3 - t_0$
        \State $t_1 \gets Y_3 + Y_3$

        \State $Y_3 \gets t_1 + Y_3$
        \State $t_1 \gets t_0 + t_0$
        \State $t_0 \gets t_1 + t_0$

        \State $t_0 \gets t_0 - t_2$
        \State $t_1 \gets t_4 \cdot Y_3$
        \State $t_2 \gets t_0 \cdot Y_3$

        \State $Y_3 \gets X_3 \cdot Z_3$
        \State $Y_3 \gets Y_3 + t_2$
        \State $X_3 \gets t_3 \cdot X_3$

        \State $X_3 \gets X_3 - t_1$
        \State $Z_3 \gets t_4 \cdot Z_3$
        \State $t_1 \gets t_3 \cdot t_0$

        \State $Z_3 \gets Z_3 + t_1$    
      \end{algorithmic}
    \end{multicols}
    \vspace{-.5\baselineskip}% Adjust to suit your needs
  \end{algorithm}
\end{minipage}

\end{document}

相关内容