8

I want to put two algorithm side by side to gain place in the document, but when I use \documentclass{llncs} instead of \documentclass{article}, the following code does not work (errors):

\documentclass{article}
\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{caption}% http://ctan.org/pkg/caption
\begin{document}
\lipsum[1]

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

\lipsum[2]

\end{document}
Count Zero
  • 17,424

1 Answers1

11

The problem is that the class file defines the caption commands and caption package hates it.

As the package author of caption and subcaption, Alex Sommerfeldt mentions this problem in this answer Subcaption package: compatibility issue with a problem related to (again) another class file.

\captionsetup{compatibility=false}

would make the problem go away but might introduce others if the class file is picky about caption settings.

I have made up another solution for the algorithms though not sure if that's what you want: The first difference is that I have used the subcaption package instead of minipages. And then I cooked up a simple subalgorithm environment. And also placed them in a float (a table but can also be a figure too regarding the counters involved if needed).

\documentclass{llncs}
\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{subcaption}% http://ctan.org/pkg/subcaption
\captionsetup{compatibility=false}
\DeclareCaptionSubType*{algorithm}
\renewcommand\thesubalgorithm{\thetable\alph{subalgorithm}}
\DeclareCaptionLabelFormat{alglabel}{Alg.~#2}



\title{My title}
\author{TeX.SX}
\institute{Online}

\begin{document}
\maketitle
\lipsum[1]
\begin{table}%
\begin{subalgorithm}{.5\textwidth}
\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}
\caption{Euclid’s algorithm}\label{algo1}
\end{subalgorithm}%
\begin{subalgorithm}{.5\textwidth}
\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}
\caption{Euclid’s algorithm}\label{algo2}
\end{subalgorithm}
\captionsetup{labelformat=alglabel}
\caption{Two algorithms}%
\label{tab:1}%
\end{table}


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

\lipsum[2]

\end{document}

enter image description here

percusse
  • 157,807
  • The problem now is that if I use \usepackage[margin=1in]{geometry} I can no more use \abovedisplayskip=-23pt on some places in my document ! – user995434 Jul 01 '12 at 15:22
  • @user995434 Ah I should have known this. Please have a look at subfig vs. subcaption for Axel's response. You might want to update the question (or better ask a new one) to address the problem with all your packages and tweaks included. – percusse Jul 01 '12 at 15:28
  • Well, I have just one last small question. If your Algo1a (at left) is longer than Algo1b (at right), is it possible that Algo1b starts from the same top line as Algo1a (like it is the case in your current picture, however here the algorithms are of the same lenght) – user995434 Jul 01 '12 at 15:38
  • @user995434 You can use the modifier \begin{subalgorithm}[t]{.5\textwidth} for both of them to align at the top. By the way, the caption manual has some interesting remarks about the algorithm and float packages. So maybe you can make a better subalgorithm environment that I have given. – percusse Jul 01 '12 at 16:11
  • Where should I add \begin{subalgorithm}[t]{.5\textwidth} ? it gives errors if I add it under on on top of \begin{algorithmic}[1] .. – user995434 Jul 01 '12 at 16:34
  • 1
    @user995434 I mean you just add [t] part, sorry for the vaguesness. It's an additional option to the subalgorithm. – percusse Jul 01 '12 at 16:35
  • Oh I just noticed that using \usepackage[margin=1in]{geometry} will change the original margin of the llncs class will be changed ! I'm not sure that this will be accepted by the conference for which I want to submit a paper :/ Is there any why to do the same by keeping the margin etc as specified by the llncs class ? – user995434 Jul 01 '12 at 17:20
  • @user995434 I really don't know. I am having similar problems with IEEEtran class and usually these classes are using, understandably, outdated tricks. I usually don't mess with the page settings and try to reduce the size, shape or other properties of things. – percusse Jul 01 '12 at 17:23
  • Do you know if it is possible to change the font size of text of the algorithm ? – user995434 Jul 01 '12 at 17:41
  • @user995434 Not really. I have never used those with detail. But should be possible anyway maybe the manual helps? – percusse Jul 01 '12 at 21:17