4

I am using the subfigure environment, but when I refer to it, the counter is either not showing or showing incorrectly.

Here is the code.

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage[margin=1in]{geometry}
\usepackage{float}
\usepackage{algorithm2e}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{setspace}
\usepackage{cleveref}

\captionsetup[subfigure]{subrefformat=simple,labelformat=simple}
\renewcommand\thesubfigure{(\alph{subfigure})}


\begin{figure}[H]
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=.8\linewidth]{g/iso-perm-vs-objective-4-vert.png}
  \label{fig:iso-perm-vs-objective-4-vert}
  \caption{Isomorphic}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=.8\linewidth]{g/non-iso-perm-vs-objective-4-vert.png}
  \label{fig:non-iso-perm-vs-objective-4-vert}
    \caption{Non-isomorphic}
\end{subfigure}
\caption{{\it Permutation terms} vs. complete $f$}
\label{fig:four-vert-perm-vs-f-landscape}
\end{figure}

Figure ~\ref{fig:four-vert-perm-vs-f-landscape} \subref{fig:non-iso-perm-vs-objective-4-vert} and Figure ~\ref{fig:four-vert-perm-vs-f-landscape} \subref{fig:iso-perm-vs-objective-4-vert}.

The output is shown below.

enter image description here

sodd
  • 5,771
Omar Shehab
  • 143
  • 1
  • 5

1 Answers1

7

You have to place the \label command after the \caption command, since it is the latter that steps the subfigure counter.

Also, you can use \ref{<thesubfigurelabel>} to get output like 1(a). If you want space between the figure and subfigure counter, e.g. 1 (a), then you can use

\makeatletter
\renewcommand\p@subfigure{\thefigure\,}
\makeatother

as explained by @Axel Sommerfeldt (the author and maintainer of the caption bundle) in this answer. If you want a full-width space, replace \, (thinspace) with ~ (non-breakable space).

Also, do not use both a space and tilde (hard space) between "Figure" and the reference, as that will give double spacing. Use just the tilde (~), which gives a non-breakable space, i.e. Figure~\ref{...}.

At last, you should not use the {\it ...} construct, but \textit{...}. For details, see Does it matter if I use \textit or \it, \bfseries or \bf, etc.

\documentclass[11pt]{article}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[demo]{graphicx} % Added demo option to get black rectangles
\usepackage{hyperref}

\captionsetup[subfigure]{subrefformat=simple,labelformat=simple}
\renewcommand\thesubfigure{(\alph{subfigure})}

\begin{document}

\begin{figure}
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=.8\linewidth]{g/iso-perm-vs-objective-4-vert.png}
  \caption{Isomorphic}
  \label{fig:iso-perm-vs-objective-4-vert}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=.8\linewidth]{g/non-iso-perm-vs-objective-4-vert.png}
  \caption{Non-isomorphic}
  \label{fig:non-iso-perm-vs-objective-4-vert}
\end{subfigure}
\caption{\textit{Permutation terms} vs. complete $f$}
\label{fig:four-vert-perm-vs-f-landscape}
\end{figure}

Figure~\ref{fig:non-iso-perm-vs-objective-4-vert} and Figure~\ref{fig:iso-perm-vs-objective-4-vert}.

\end{document}

Output

sodd
  • 5,771