演算法環境內的多值

演算法環境內的多值

我正在嘗試編寫一個演算法,但由於它不適合一頁,所以我嘗試將其放在兩列中。

我想寫的程式碼是這樣的

\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和演算法。有誰知道我怎麼能讓它發揮作用?

答案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}

相關內容