[Tex/LaTex] Placing graphic and algorithm on same page

algorithmsfloatsgraphics

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

Best Answer

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.

Related Question