Text FormattingPutting two Algorithms Side by Side

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
pablochacin
Posts: 2
Joined: Mon Nov 12, 2007 12:14 pm

Putting two Algorithms Side by Side

Post by pablochacin »

Hi

I'm trying to put two algorithm listings side by side within an figure environment, to refer them as sub-figures. I tried many, many options, but none has worked so far.

The "obvious" approach, putting subfloats in the figure, as shown below, throws the infamous error "! LaTeX Error: Something's wrong--perhaps a missing \item."

Code: Select all

\begin{figure}[h]
\centering

\subfloat[Algorithms 1]{
\begin{algorithmic}
% algorithm text
\end{algorithmic}
}

\subfloat[algorithm 2]{
\begin{algorithmic}
% algorithm text
\end{algorithmic}
}

\caption{Algorithms}
\label{figure
\end{figure}
Is there any simple way to do this? Looks like a common problem, bu googling has been mostly useless.

Thanks in advance

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Putting two Algorithms Side by Side

Post by gmedina »

Hi,

one possibility would be to use minipages and the \captionof command provided by the caption package (see Remarks below); the following example illustrates this approach:

Code: Select all

\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{caption}

\begin{document}

\noindent\begin{minipage}{\textwidth}
  \centering
  \begin{minipage}{.45\textwidth}
    \centering
    \captionof{algorithm}{test algorithm 1}
    \label{alg:alg1}
    \begin{algorithmic}
    \WHILE{$N \neq 0$}
    \IF{$N$ is even}
    \STATE $X \Leftarrow X \times X$
    \STATE $N \Leftarrow N / 2$
    \ENDIF
    \ENDWHILE
    \end{algorithmic}
  \end{minipage}
  \begin{minipage}{.45\textwidth}
    \centering
    \captionof{algorithm}{test algorithm 2}
    \label{alg:alg2}
    \begin{algorithmic}
    \WHILE{$N \neq 0$}
    \IF{$N$ is even}
    \STATE $X \Leftarrow X \times X$
    \STATE $N \Leftarrow N / 2$
    \ENDIF
    \ENDWHILE
    \end{algorithmic}
  \end{minipage}
  \captionof{figure}{Two algorithms side by side}
  \label{fig:twoalg}
\end{minipage}

Figure~\ref{fig:twoalg} shows two algorithms side by side: they can be referenced as algorithm~\ref{alg:alg1} and algorithm~\ref{alg:alg2}...

\end{document}
Remarks: 1) Make sure that you have a recent version (3.1x) of the package installed in your system.
2) Please take into account that since minipages were used this is not a floating object.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
steenreem
Posts: 6
Joined: Tue Apr 05, 2011 1:28 pm

Re: Putting two Algorithms Side by Side

Post by steenreem »

For anyone searching this. I noticed that wrapping the algorithmic environment in a minipage removes the infamous error.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Putting two Algorithms Side by Side

Post by localghost »

Can be done much easier with »subcaption« (bundled with caption).

Code: Select all

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[font=small,labelfont=bf,tableposition=top]{caption}
\usepackage[font=footnotesize]{subcaption}
\usepackage{algorithmic}

\begin{document}
  \begin{figure}[!ht]
    \centering
    \begin{subfigure}{.45\textwidth}
      \centering
      \begin{algorithmic}
        \WHILE{$N \neq 0$}
          \IF{$N$ is even}
            \STATE $X \Leftarrow X \times X$
            \STATE $N \Leftarrow N / 2$
          \ENDIF
        \ENDWHILE
      \end{algorithmic}
      \caption{Test Algorithm No.1}\label{alg:alg-1}
    \end{subfigure}
		\hfill
    \begin{subfigure}{.45\textwidth}
      \centering
      \begin{algorithmic}
        \WHILE{$N \neq 0$}
          \IF{$N$ is even}
            \STATE $X \Leftarrow X \times X$
            \STATE $N \Leftarrow N / 2$
          \ENDIF
        \ENDWHILE
      \end{algorithmic}
      \caption{Test Algorithm No.2}\label{alg:alg-2}
    \end{subfigure}
    \caption{Two algorithms side by side}\label{fig:twoalg}
  \end{figure}

  Figure~\ref{fig:twoalg} shows two algorithms side by side: they can be referenced as algorithm~\ref{alg:alg-1} and algorithm~\ref{alg:alg-2}.
\end{document}
The package manual has more detailed explanations.


Thorsten
Post Reply