3

I put two algorithms side by side using the following code, however, there is no borders for them and the it is hard to identify the two algorithms.

The codes:

\documentclass{llncs}% http://www.springer.com/computer/lncs/lncs+authors?SGWID=0-40209-0-0-0
%\usepackage[margin=1in]{geometry}% http://ctan.org/pkg/geometry
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage[compatibility=false]{caption}% http://ctan.org/pkg/caption
\begin{document}

\medskip

\noindent\begin{minipage}{.5\textwidth}
\captionof{algorithm}{Euclid’s algorithm}\label{algo1}
\begin{algorithmic}[1]
  \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
    \EndWhile
    \State \textbf{return} $b$\Comment{The gcd is b}
  \EndProcedure
\end{algorithmic}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\captionof{algorithm}{Euclid’s algorithm}\label{algo2}
\begin{algorithmic}[1]
  \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
      \State $d$
    \EndWhile
    \State \textbf{return} $b$\Comment{The gcd is b}
  \EndProcedure
\end{algorithmic}
\end{minipage}

\medskip

On the left is Algorithm~\ref{algo1}. On the right is Algorithm~\ref{algo2}.

\end{document} 

and it looks like:

[![enter image description here][1]][1]

I wish it look like this 1. with borders and 2. top aligned

Below is the result with border drawn.enter image description here

Torbjørn T.
  • 206,688
sweetyBaby
  • 3,029
  • Partial duplicate: http://tex.stackexchange.com/a/34200/89417. Key points: \usepackage{adjustbox} in preamble, \begin{adjustbox}{valign=t,fbox} and \end{adjustbox} around the minipages. – Marijn Oct 16 '15 at 10:11

1 Answers1

2

Choose slightly less wide minipages and add the optional [t]. As the algorithm environment must be dropped, I defined a ruled caption format to mimick the ‘ruled’ caption formatting of algorithm. Also, I added the inputenc and [T1]{fontenc} packages: the documents lacked apostrophs, and probably accented letters if you needed them:

\documentclass{llncs}% http://www.springer.com/computer/lncs/lncs+authors?SGWID=0-40209-0-0-0
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage[compatibility=false]{caption}% http://ctan.org/pkg/caption
\DeclareCaptionFormat{ruled}{\leavevmode\leaders\hrule height 0.8pt depth0pt\hfill\mbox{}\endgraf#1#2 #3 \vspace{-0.4\baselineskip}\leavevmode\leaders\hrule height 0.6pt\hfill\null\vspace*{-0.6\baselineskip}}
 \algrenewcommand\algorithmiccomment[1]{\hfill\({}\triangleright{}\){\footnotesize#1}}%

\begin{document}

\noindent
\captionsetup{format=ruled, labelfont=sc}
   \begin{minipage}[t]{.45\textwidth}
    \captionof{algorithm}{Euclid’s algorithm}\label{algo1}
    \begin{algorithmic}[1]
      \Procedure{Euclid}{$a,b$}

      \Comment{The g.c.d. of a and b}
        \State $r\gets a\bmod b$
        \While{$r\not=0$}\Comment{We have the answer if r is 0}
          \State $a\gets b$
          \State $b\gets r$
          \State $r\gets a\bmod b$
        \EndWhile
        \State \textbf{return} $b$\Comment{The gcd is b}
      \EndProcedure
    \end{algorithmic}
\kern2pt\hrule height.8pt\relax
    \end{minipage}%
    \hfill
    \begin{minipage}[t]{.45\textwidth}
    \captionof{algorithm}{Euclid’s algorithm}\label{algo2}
    \begin{algorithmic}[1]
      \Procedure{Euclid}{$a,b$}

      \Comment{The g.c.d. of a and b}
        \State $r\gets a\bmod b$
        \While{$r\not=0$}

        \Comment{We have the answer if r is 0}
          \State $a\gets b$
          \State $b\gets r$
          \State $r\gets a\bmod b$
          \State $d$
        \EndWhile
        \State \textbf{return} $b$\Comment{The gcd is b}
      \EndProcedure
    \end{algorithmic}
\kern2pt\hrule height.8pt\relax
\end{minipage}
\medskip

On the left is Algorithm~\ref{algo1}. On the right is Algorithm~\ref{algo2}.

\end{document} 

enter image description here

Bernard
  • 271,350
  • May I know how to add borders? – sweetyBaby Oct 16 '15 at 10:20
  • I'll test something and come back. Do you mean some padding on the left and on the right? Or a frame around each algorithm? – Bernard Oct 16 '15 at 10:22
  • I mean the frame around each algorithm, thanks for the help! – sweetyBaby Oct 16 '15 at 10:34
  • Untested suggestion: Use the package algorithm2e as show here. Perhaps this might work. – crixstox Oct 16 '15 at 11:17
  • 1
    @sweetyBaby To get the frames, you can use \fbox around the minipages; schematically: \noindent\fbox{\begin{minipage}[t]{\dimexpr.5\textwidth-2\fboxsep-2\fboxrule\relax} ...\end{minipage}}% \fbox{\begin{minipage}[t]{\dimexpr.5\textwidth-2\fboxsep-2\fboxrule\relax} ... \end{minipage}} – Gonzalo Medina Oct 16 '15 at 13:46
  • @sweetyBaby: I updated my answer to a solution with horizontal rules. – Bernard Oct 16 '15 at 19:38