Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

1 Answer 1

up vote 9 down vote accepted

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

share|improve this answer
    
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 1 '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 1 '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 1 '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 1 '12 at 16:11
1  
@user995434 I mean you just add [t] part, sorry for the vaguesness. It's an additional option to the subalgorithm. –  percusse Jul 1 '12 at 16:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.