Home > Blockchain >  side by side algorithms in latex
side by side algorithms in latex

Time:01-01

I am using "elsarticle" class and need to place two algorithms side by side. I am using minipage fucntion to do this but algoirthms are not geenrated exactly side by side (a picture is attched for your reference). A working latex code is given as:

documentclass[3p]{elsarticle}
\usepackage{hyperref}
%\modulolinenumbers[5]
\usepackage[ruled,linesnumbered]{algorithm2e}
\usepackage{amsfonts}
\usepackage{threeparttable}
\usepackage{tabularx}
%\usepackage{cite}
\usepackage{mathtools}
\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{multirow}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{minipage}{0.5\textwidth}
\begin{algorithm}[H]             
%   \caption{(X,Y)-only  co-Z addition with update     ($ZADDU_{(X,Y)}$)}  
    \caption{ $ZADDU_{(X,Y)}$}          
    
    \label{alg1}   
    %\begin{multicols}{2}                       % and a label for \ref{} commands 
    \begin{algorithmic}[1] 
    \Require{ $R_1 = (X_1, Y_1, Z )$ and $R_2 = (X_2, Y_2, Z)$ }
    \Ensure {$(R_3, R_1)\mspace{5mu} = \mspace{5mu}ZADDU_{(X,Y)}(R_1, R_2)$ $\mspace{15mu}$where $R_3= R_1   R_2=
(X_3, Y_3, Z_3)$ and $R_1 = (\lambda^2{X_1}, \lambda^3 {Y_1}, Z_3)$  with $Z_3=\lambda Z$ for some $\lambda\neq0$}\\
%   \tcc{\textbf{Phase A:}}
%   $z := 0, R_1 := x$\;
%   $R_2 := u_2$\; 
%   
%   \tcc{\textbf{Phase B:}}
$B=(X_1-X_2)^2$; 
    $E_1 =X_1 U; E_2 = X_2 U$; $C =(Y_1 - Y_2)^2$\\
      $D =Y_1 (E_1 - E_2)$; $X_3=C - E_1 - E_2$; 
      $Y_3=(Y_1 - Y_2)(E_1 - X_3 ) - D$;\\
       ${X_1}=  E_1$; ${Y_1} = D$; 
     $R_3=(X_3, Y_3)$, $R_3=(X_1, Y_1)$\\
    \Return{($R_3,R_1$)}\;
    \end{algorithmic}
\end{algorithm}
\end{minipage}
\hfill
\begin{minipage}{0.5\textwidth}
\begin{algorithm}[H]
%        \caption{(X,Y)-only  conjugate co-Z addition ($ZADDC_{(X,Y)}$)}              
    \caption{$ZADDC_{(X,Y)}$}          
    % give the algorithm a caption
    \label{alg1}   
    %\begin{multicols}{2}                       % and a label for \ref{} commands 
    \begin{algorithmic}[1] 
    \Require{ $R_1 = (X_1, Y_1, Z )$ and $R_2 = (X_2, Y_2, Z)$ }
    \Ensure {$(R_3, \overline R_3)\mspace{5mu} = \mspace{5mu}ZADDC_{(X,Y)}(R_1, R_2)$$\mspace{10mu}$where $R_3= R_1   R_2=
(X_3, Y_3, Z_3)$ and $\overline R_3 =R_1 - R_2= (\overline{X_3},\overline {Y_3}, Z_3)$ }\\
%   \tcc{\textbf{Phase A:}}
%   $z := 0, R_1 := x$\;
%   $R_2 := u_2$\; 
%   
%   \tcc{\textbf{Phase B:}}
$B=(X_1-X_2)^2$;  
    $E_1 =X_1 U; E_2 = X_2 U$; $C =(Y_1 - Y_2)^2$;\\
      $D =Y_1 (E_1 - E_2)$; $X_3=C - V_1 - V_2$;
      $Y_3=(Y_1 - Y_2)(E_1 - X_3 ) - D$; \\
     $\overline{C} = (Y1   Y2)^2$;  $\overline{X_3}=  \overline{C} - E_1 - E_2$; $\overline{Y_3} = (Y_1   Y_2)(E_1 - \overline{X_3} ) - D$;\\
    
    \Return{($R_3,\overline R_3$)}\;
    \end{algorithmic}
 \end{algorithm}
 \end{minipage}

\end{document}

enter image description here

An output picture is attached, any help to allign these algorithms side by side would be appreciated alot.

CodePudding user response:

A couple of problems:

  • missing \ in front of documentclass

  • incompatible package. Your document will produce the error

    Command \listofalgorithms already defined. ...s}{\listof{algorithm}{\listalgorithmname}}
    

    You should never ignore error messages. After an error latex only recovers enough to syntax check the rest of the document, not necessarily producing sensible output

  • you are placing two minipages with .5\textwidth each besides each other. This will fill the entire line leaving no space for all the additional spaces added by your unescaped line endings. Either make your minipages smaller or add % at the end of lines so they don't act like spaces

  • use unique labels for your algorithms

  • the hyperref package should be loaded as one of the last packages. Don't load it directly at the start of your preamble

  • and just from your picture I would guess that in your real document you are missing an empty line before the minipages

\documentclass[3p]{elsarticle}

%\modulolinenumbers[5]
\usepackage[ruled,linesnumbered]{algorithm2e}
\usepackage{amsfonts}
\usepackage{threeparttable}
\usepackage{tabularx}
%\usepackage{cite}
\usepackage{mathtools}
\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{multirow}
%\usepackage{algorithm}
\usepackage{algpseudocode}

\usepackage{hyperref}
\begin{document}
\noindent\begin{minipage}{0.5\textwidth}
\begin{algorithm}[H]             
%   \caption{(X,Y)-only  co-Z addition with update     ($ZADDU_{(X,Y)}$)}  
    \caption{ $ZADDU_{(X,Y)}$}          
    
    \label{alg1}   
    %\begin{multicols}{2}                       % and a label for \ref{} commands 
    \begin{algorithmic}[1] 
    \Require{ $R_1 = (X_1, Y_1, Z )$ and $R_2 = (X_2, Y_2, Z)$ }
    \Ensure {$(R_3, R_1)\mspace{5mu} = \mspace{5mu}ZADDU_{(X,Y)}(R_1, R_2)$ $\mspace{15mu}$where $R_3= R_1   R_2=
(X_3, Y_3, Z_3)$ and $R_1 = (\lambda^2{X_1}, \lambda^3 {Y_1}, Z_3)$  with $Z_3=\lambda Z$ for some $\lambda\neq0$}\\
%   \tcc{\textbf{Phase A:}}
%   $z := 0, R_1 := x$\;
%   $R_2 := u_2$\; 
%   
%   \tcc{\textbf{Phase B:}}
$B=(X_1-X_2)^2$; 
    $E_1 =X_1 U; E_2 = X_2 U$; $C =(Y_1 - Y_2)^2$\\
      $D =Y_1 (E_1 - E_2)$; $X_3=C - E_1 - E_2$; 
      $Y_3=(Y_1 - Y_2)(E_1 - X_3 ) - D$;\\
       ${X_1}=  E_1$; ${Y_1} = D$; 
     $R_3=(X_3, Y_3)$, $R_3=(X_1, Y_1)$\\
    \Return{($R_3,R_1$)}\;
    \end{algorithmic}
\end{algorithm}
\end{minipage}%
%\hfill
\begin{minipage}{0.5\textwidth}
\begin{algorithm}[H]
%        \caption{(X,Y)-only  conjugate co-Z addition ($ZADDC_{(X,Y)}$)}              
    \caption{$ZADDC_{(X,Y)}$}          
    % give the algorithm a caption
    \label{alg2}   
    %\begin{multicols}{2}                       % and a label for \ref{} commands 
    \begin{algorithmic}[1] 
    \Require{ $R_1 = (X_1, Y_1, Z )$ and $R_2 = (X_2, Y_2, Z)$ }
    \Ensure {$(R_3, \overline R_3)\mspace{5mu} = \mspace{5mu}ZADDC_{(X,Y)}(R_1, R_2)$$\mspace{10mu}$where $R_3= R_1   R_2=
(X_3, Y_3, Z_3)$ and $\overline R_3 =R_1 - R_2= (\overline{X_3},\overline {Y_3}, Z_3)$ }\\
%   \tcc{\textbf{Phase A:}}
%   $z := 0, R_1 := x$\;
%   $R_2 := u_2$\; 
%   
%   \tcc{\textbf{Phase B:}}
$B=(X_1-X_2)^2$;  
    $E_1 =X_1 U; E_2 = X_2 U$; $C =(Y_1 - Y_2)^2$;\\
      $D =Y_1 (E_1 - E_2)$; $X_3=C - V_1 - V_2$;
      $Y_3=(Y_1 - Y_2)(E_1 - X_3 ) - D$; \\
     $\overline{C} = (Y1   Y2)^2$;  $\overline{X_3}=  \overline{C} - E_1 - E_2$; $\overline{Y_3} = (Y_1   Y_2)(E_1 - \overline{X_3} ) - D$;\\
    
    \Return{($R_3,\overline R_3$)}\;
    \end{algorithmic}
 \end{algorithm}
 \end{minipage}%

\end{document}

enter image description here

  • Related