[Tex/LaTex] How to put algorithm and figure(s) side by side

algorithmsgraphicspositioning

How can I put an algorithm and figure(s) side by side?

Say,

XXXXXXXXXXX FIG ONE
XXXXXXXXXXX FIG ONE
XXXXXXXXXXX FIG ONE
XALGORITHMX FIG TWO 
XXXXXXXXXXX FIG TWO

Please, only post working solution – I am new in LaTeX. Also inform what package to include.

Best Answer

For correct referencing of of figures you could use the caption package which provides \captionof{figure}{<caption>}. This typesets the caption <caption> as that of a figure environment even though you have not declared the contents within a figure environment. This is of great help since typesetting a figure (or float) inside a minipage (a non-float) is not allowed. Additionally, the listings package typesets a large body of languages verbatim and can be expanded to incorporate your own touch of formatting.

Here is a small example that shows the construction, as well as the referencing capability, if needed.

enter image description here

\documentclass{article}
\usepackage[margin=1in]{geometry}% http://ctan.org/pkg/geometry
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{caption}% http://ctan.org/pkg/caption
%\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\begin{document}
\lipsum[1]

\medskip

\noindent\begin{minipage}{.5\textwidth}
\begin{lstlisting}[language=C,caption={My first program},label=mylisting]
#include <stdio.h>

main()
{
  printf ("Hello World!\n");
}
\end{lstlisting}
\end{minipage}%
\begin{minipage}[t]{.5\textwidth}
  \centering
  \rule{0.3\textwidth}{50pt}
  \captionof{figure}{This is a figure caption.} \label{myfig1}
  \bigskip
  \rule{0.25\textwidth}{70pt}
  \captionof{figure}{This is another figure.} \label{myfig2}
\end{minipage}

\medskip

On the left is Listing~\ref{mylisting}. On the right is Figures~\ref{myfig1} and~\ref{myfig2}.

\lipsum[2]

\end{document}

In order to use a more pseudo-code listing, you may be interested in the [algorithmicx package][algorithmicx-pkg]. However, for this to function fully as above, you also need the [algorithm package][algorithm-pkg] which defines the algorithm float (and associated float counter). Now you can use \captionof{algorithm}{<caption>}. Here is a similar example:

enter image description here

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

\medskip

\noindent\begin{minipage}{.5\textwidth}
\captionof{algorithm}{Euclid’s algorithm}\label{myalg}
\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\label{euclidendwhile}
    \State \textbf{return} $b$\Comment{The gcd is b}
  \EndProcedure
\end{algorithmic}
\end{minipage}%
\begin{minipage}[t]{.5\textwidth}
  \centering
  \rule{0.3\textwidth}{50pt}
  \captionof{figure}{This is a figure caption.} \label{myfig1}
  \bigskip
  \rule{0.25\textwidth}{70pt}
  \captionof{figure}{This is another figure.} \label{myfig2}
\end{minipage}

\medskip

On the left is Algorithm~\ref{myalg}. On the right is Figures~\ref{myfig1} and~\ref{myfig2}.

\lipsum[2]

\end{document}

lipsum provided some dummy text, while geometry was merely used to increase the text block size (via margin=1in). Dummy figures are represented by black rectangular boxes, although you would be interested in the graphicx package for inserting your images.

Adjustment of the vertical alignment is also possible, depending on the size of algorithm or figures.

Related Question