3

I'm trying to write an algorithm, but since it doesn't fit in one page, I'm trying to put it in two columns.

The code I'm trying to write it this

\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}
}

But I get an error when I try this. If i start the multicols before the \Require, it works but I dont want to do that. I have already imported algpseudocode and algorithm. Does anyone know how I can get this to work?

1 Answer 1

3

Split your \Require and \Ensure components from the rest of your statements using two separate algorithmic environments. Then wrap the second one inside a multicols with some vertical adjustment.

enter image description here

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .