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 have two algorithms in a latex document using a class document of one column. The first algorithm take almost one page, and the second one is on the next page. Is there ant way to format this to gain some place in the document. Maybe putting the two algorithms on the same pages (I don't know if it is possible).

I use \usepackage{algorithm} and \usepackage{algpseudocode}. This is an example of the two algorithms (actually they are longer than this, this is just an example).

\begin{algorithm}
\caption{My First Algorithm (param)}
\label{algo1}
\begin{algorithmic}[1]
\State $A = A + param$
\State $\alpha = 34$
\State etc ...
\end{algorithmic}
\end{algorithm}

\begin{algorithm}
\caption{My Second Algorithm (A)}
\label{algo1}
\begin{algorithmic}[1]
\State $C = C + A$
\State $\alpha = \beta$
\State etc ...
\end{algorithmic}
\end{algorithm}
share|improve this question
1  
Could you turn your code snippet into a complete MWE? Have you tried using minipages? –  cmhughes Jul 1 '12 at 0:49
    
@cmhughes I don't know about it, how can I use minipage for this case ? –  user995434 Jul 1 '12 at 1:08

1 Answer 1

This a a slight modification of How to put algorithm and figure(s) side by side?

Here is a minimal example showing how to pair algorithms side-by-side:

enter image description here

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

The idea behind this approach is to have two minipages spanning the entire \linewidth. These minipages then house the non-floating algorithmic environments, as well as the non-floating \captionof caption. If need be, you can wrap the entire double minipage inside a floating algorithm environment.

The use of geometry was just to gain some stock real estate (may not be required in your instance), while lipsum provided some dummy text, Lorem ipsum style.

caption provides the means to have a caption outside of a float via \captionof. However, it requires the compatibility=false option to work, since llncs already redefines \caption - detected by caption. algorithm is still required, since it provides the algorithm counter and "List of Algorithms" capability.

share|improve this answer
    
At line "\captionof{algorithm}{Euclids algorithm}\label{algoo1}" I have an error : "! Undefined control sequence. \@tempf ... \@parboxrestore \normalsize \@fs@capt {\@nameuse {fnum@#1}}{\ign... l.178 \captionof{algorithm}{Euclids algorithm} \label{algoo1} The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., \hobx'), type I' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined." –  user995434 Jul 1 '12 at 10:20
    
I use \documentclass{llncs}, and it doesnt work with it ! \documentclass{llncs} \usepackage{llncsdoc} \usepackage{algorithm} \usepackage{algpseudocode} \usepackage{graphicx} \usepackage{amsmath} \usepackage{wrapfig} \usepackage[margin=1in]{geometry}% http://ctan.org/pkg/geometry \usepackage{lipsum}% http://ctan.org/pkg/lipsum \usepackage{caption}% http://ctan.org/pkg/caption –  user995434 Jul 1 '12 at 10:26
    
I've updated my answer to use the llncs document class, just as in Put two algorithms side by side. For future reference, include a minimal, compilable example that includes such detail. –  Werner Jul 1 '12 at 13:17
    
Well, I have just one last small question. If your Algo1 (at left) is longer than Algo2 (at right), is it possible that Algo2 starts from the same top line as Algo1 (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: In both instances, use \begin{minipage}[t]{.5\linewidth} to align them at the top. –  Werner Jul 1 '12 at 17:33

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.