4

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}
Werner
  • 603,163

1 Answers1

4

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.

Werner
  • 603,163
  • 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'), typeI' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined." – user995434 Jul 01 '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 01 '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 01 '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 01 '12 at 15:38
  • @user995434: In both instances, use \begin{minipage}[t]{.5\linewidth} to align them at the top. – Werner Jul 01 '12 at 17:33
  • 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:38
  • @user995434: Yes, you don't have to use geometry for this answer to still work. The reason why I used it was because I had a complete example of pseudocode, together with comments (which you did not provide). So, just remove geometry from your preamble. – Werner Jul 01 '12 at 17:41