1

Consider the following code, whose purpose is to place a subfigure and algorithm side by side within a floating figure.

\documentclass{article}
\usepackage{algorithm}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\begin{subfigure}[t]{.5\linewidth}
\caption{subcaption}
\end{subfigure}\hfill
\begin{minipage}[m]{.5\linewidth}
\begin{algorithm}[H]
\caption{alg caption}
\end{algorithm}
\end{minipage}\hfill
\caption{caption}
\end{figure}

\end{document}


The problem is that the main caption shows Figure 2 instead of Figure 1.

The following changes to the above code would produce Figure 1, but none of these solutions are satisfactory.

  1. Get rid of the \begin{algorithm}[H]. Produces Figure 1.
  2. Make subfigure come after the minipage. Produces Figure 1.

Question 1: Why is this problem occuring?

Question 2: Is there a simple solution to have the subfigure (a) on the left, Algorithm 1 on the right, and the main caption showing Figure 1?

1 Answer 1

0

subcaption does some juggling with the figure counter and doesn't play well with float.

Hack: step down the figure counter.

\documentclass{article}
\usepackage{algorithm}
\usepackage{subcaption}

\begin{document}

\begin{figure}[htp]
\begin{subfigure}[t]{.5\linewidth}
\caption{subcaption}
\end{subfigure}\hfill
\begin{minipage}{.5\linewidth}
\begin{algorithm}[H]
\caption{alg caption}
\end{algorithm}\addtocounter{figure}{-1}
\end{minipage}
\caption{caption}
\end{figure}

\begin{figure}[htp]
\begin{subfigure}[t]{.5\linewidth}
\caption{subcaption}
\end{subfigure}\hfill
\begin{minipage}{.5\linewidth}
\begin{algorithm}[H]
\caption{alg caption}
\end{algorithm}\addtocounter{figure}{-1}
\end{minipage}
\caption{caption}
\end{figure}

\end{document}

enter image description here

I duplicated the figure to ensure that the next figure gets its number right.

You must log in to answer this question.

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