2

If I have the following LaTeX code, is it possible to combine both graphic and algorithm so that they are fixed to a common page?

\begin{figure} [H]
\centering{\includegraphics[scale=1]{Figure.png}}
\caption{Figure.}
\label{fig:Figure}
\end{figure}

\begin{algorithm}[H]
 \algsetup{indent=2em}
    \begin{adjustwidth}{1em}{} 
        \begin{algorithmic}[1]
            \STATE $Line$ $of$ $code$
        \end{algorithmic}
        \caption{Algorithm.}
        \label{alg:Algorithm}
    \end{adjustwidth}
\end{algorithm}

To give something like this:

enter image description here

2 Answers 2

2

I would place the algorithm as a non-float - using the [H] float specifier - inside the figure float:

enter image description here

\documentclass{article}

\usepackage{lipsum,graphicx}
\usepackage{algorithm,algorithmic}

\begin{document}

\lipsum[1-4]

\begin{figure}[tbp]
  \centering
  \includegraphics[width=.7\linewidth]{example-image}
  \caption{This is a figure}
  \label{fig:Figure}

  \bigskip% Insert a gap between the figure/algorithm

  \begin{algorithm}[H]
    \begin{algorithmic}[1]
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
      \STATE Line of code
    \end{algorithmic}
    \caption{This is an algorithm}
    \label{alg:Algorithm}
  \end{algorithm}
\end{figure}

\lipsum[5-20]

\end{document}

The [H] float specifier process the float as a immovable block, making it possible to embed it inside another float (figure in the above case). Since both objects are contained within the same float, they will always stay together.

0

You can use subfig package to put both the figure and algorithm in a same float. You can add your labels and captions to each subfloat.

\usepackage{subfig}
\begin{figure}
    \centering
    \subfloat[][a]{\includegraphics{<figure1>}}
    \subfloat[][b]{\begin{algorithmic}[1]\end{algorithmic}}
\end{figure}

Or using subcaption:

\usepackage{subcaption}
\begin{figure}
    \centering
    \begin{subfigure}[⟨pos⟩]{⟨width⟩} ... \end{subfigure}
    \begin{subfigure}[⟨pos⟩]{⟨width⟩} ... \end{subfigure}
\end{figure}

Or minipage, where use can still add subcaptions:

\begin{figure}
  \begin{minipage}[b]{.5\linewidth}
     \includegraphics{<figure1>}
  \end{minipage}
  \begin{minipage}[b]{.5\linewidth}
      \begin{algorithmic}[1]\end{algorithmic}
  \end{minipage}%
\end{figure}

Or in many other ways that you can put your algorithmic environment as a sub-figure.

2
  • Unfortunately, I can't use subfig because it is incompatible with subcaption which I'm already using.
    – Mr Morgan
    Oct 14, 2015 at 17:30
  • Then use the subfigure command from subcaption or use minipage. I included example above.
    – Aydin
    Oct 14, 2015 at 22:28

You must log in to answer this question.

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