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.

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.

share|improve this question

migrated from stackoverflow.com Nov 15 '11 at 2:06

This question came from our site for professional and enthusiast programmers.

3 Answers 3

up vote 8 down vote accepted

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.

share|improve this answer
    
It is not a script. C is offended by such wording! –  Andrey Vihrov Nov 15 '11 at 9:26
    
@Werner Thank you very much. It worked fine. I wanted to use algorithm environment, so instead of using \begin{lstlisting} .. \end.. I used \begin{algorithmic}[1] {code here} \end{algorithmic} Now, it works, only problem is I can't add caption!! \captionof{XXXX}{test algorithm 1} works if I add, XXXX=figure. How can I use XXXX = algorithm? Possible? Thanks again for your help. –  Droid-Bird Nov 15 '11 at 11:11
1  
@Droid-Bird: See my updated answer - you need to also include the algorithm package. –  Werner Nov 15 '11 at 15:29
    
@Werner, Thank you very much. Your last solution works fine too. I think, the problem is with "\documentclass{article}" and "\documentclass[graybox]{svmult}". I am using this svmult thing(this is required). So, ... Thank you very much for your help. So, how I solved (!) it is, declared "\renewcommand{\tablename}{Algorithm}" Then while using, I called the caption of algo \captionof{table}{name of the table} [alas!] By the way, have you followed, there is not horizontal line above the Algorithm, which appears when you use only one algo? –  Droid-Bird Nov 15 '11 at 15:49

You could either use minipages (\begin{minipage}{.5\textwidth}...\end{minipage}), which you can arrange side-by-side, or you could check out the wrapfig package for wrapping images by text flow generically.

share|improve this answer

I usually put minipages it in a tabular environment. To include figures, you will need to use graphicx.

\begin{tabular}{cc}
\raisebox{.1cm}{\begin{minipage}{.5\textwidth}
Algorithm
\end{minipage}} &
\begin{minipage}{.5\textwidth}
\includegraphics[width=\textwidth]{figurename.eps}
\end{minipage}
\end{tabular}

You may also need to use \raisebox to adjust the height of one box or the other.

\begin{tabular}{cc}
\raisebox{-.1cm}{\begin{minipage}{.5\textwidth}
Algorithm
\end{minipage}} &
\begin{minipage}{.5\textwidth}
\includegraphics[width=\textwidth]{figurename.eps}
\end{minipage}
\end{tabular}
share|improve this answer
1  
Note that TeX will complain about an overfull box using a tabular as-is. This is because the contents for each column has a width of .5\textwidth plus the column padding. To remove this, use {@{}c@{}c@{}} as the column specification. Also, your tabular would be indented, so you need \noindent. –  Werner Nov 15 '11 at 2:48
    
@Werner: thanks for the comment! I've been doing this for years and have been ignoring the overfull warnings... Ha, now I know how to fix that. I forgot about the \noindent too - I usually set \parindent 0pt in my preamble for most of my reports... the other option would be to enclose the tabular expression in \begin{center}...\end{center}. –  crippledlambda Nov 15 '11 at 3:49

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.