9

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.

5
  • How do you set the algorithm?
    – egreg
    Jan 6, 2012 at 16:12
  • Use the package subcaption. Jan 6, 2012 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, 2012 at 16:16
  • @MarcoDaniel: I have already looked at it, but couldn't figure out how to make that work.
    – freitass
    Jan 6, 2012 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, 2012 at 17:44

2 Answers 2

11

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

1
  • 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, 2012 at 17:04
2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .