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 am trying to put an algorithm and a tikz' drawing side-by-side inside a figure environment. I am aware of this question but I would like to have a single caption for them. For instance, in the text, Figure 1a would refer to the algorithm while Figure 1b would refer to the tikz' drawing.

share|improve this question
    
How do you set the algorithm? –  egreg Jan 6 '12 at 16:12
    
Use the package subcaption. –  Marco Daniel Jan 6 '12 at 16:13
    
@egreg: I have tried using nested \minipage...\begin{algorithm}...\begin{algorithmic}<algorithm here>...\end..\end..\end, nested \minipage...\begin{algorithmic}<algorithm here>...\end..\end and just \begin{algorithmic}<algorithm here>...\end directly inside the \subfloat (obviously with the correct syntax) but none of them worked out. –  freitass Jan 6 '12 at 16:16
    
@MarcoDaniel: I have already looked at it, but couldn't figure out how to make that work. –  freitass Jan 6 '12 at 16:19
    
@freitass: I edited slightly the title to make it more generic. I hope that's ok with you. –  Count Zero Jan 6 '12 at 17:44
add comment

2 Answers

up vote 9 down vote accepted

I recommend the using of the subcaption package. It works well with caption (same author, thanks Axel).

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\begin{document}
\section{foo}
Text

\begin{figure}[!ht]
\begin{subfigure}[b]{.5\linewidth}
\centering
\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}

\caption{Algorithm}\label{fig:alg}
\end{subfigure}%
\begin{subfigure}[b]{.5\linewidth}
\centering
\tikz\draw [fill=red!20] (0,0) rectangle (3,2);
\caption{tikz}\label{fig:tikz}
\end{subfigure}
\caption{Both}\label{fig:both}
\end{figure}

Text \ref{fig:alg} and \ref{fig:tikz} and \ref{fig:both}

\end{document}

enter image description here

share|improve this answer
    
That is just what I needed. Thank you very much! Thank also to the algpseudocode package. I tried the same with the algorithmic package and it didn't work. –  freitass Jan 6 '12 at 17:04
add comment

Just don't enclose algorithmic in the algorithm environment, but rather in a minipage; here I've used varwidth to get the exact width.

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{varwidth,algorithmic}
\begin{document}
\begin{figure}
\centering
\begin{varwidth}[c]{\textwidth}
\begin{algorithmic}
\REQUIRE $n \geq 0 \vee x \neq 0$ 
\ENSURE $y = x^n$ 
\STATE $y \leftarrow 1$ 
\IF{$n < 0$} 
\STATE $X \leftarrow 1 / x$ 
\STATE $N \leftarrow -n$ 
\ELSE 
\STATE $X \leftarrow x$ 
\STATE $N \leftarrow n$ 
\ENDIF 
\WHILE{$N \neq 0$} 
\IF{$N$ is even} 
\STATE $X \leftarrow X \times X$ 
\STATE $N \leftarrow N / 2$ 
\ELSE[$N$ is odd] 
\STATE $y \leftarrow y \times X$ 
\STATE $N \leftarrow N - 1$ 
\ENDIF 
\ENDWHILE 
\end{algorithmic}
\end{varwidth}
\qquad
\includegraphics[width=3cm,height=2cm]{abc}
\caption{A caption}
\end{figure}
\end{document}

enter image description here

share|improve this answer
add comment

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.