25

I'm trying to reference to subfigures in the main caption of my figure. Unfortunately I'm only getting blanks, not even undefined referencing ??. What's going on?

\documentclass{report}

\usepackage[superscript]{cite}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}
Some text.

\begin{figure}[!h]
    \centering
    \begin{subfigure}[b]{0.4\textwidth}
            \centering
            \includegraphics[width=\textwidth]{figure1}
    \label{fig:fig1}
    \end{subfigure}
\begin{subfigure}[b]{0.4\textwidth}
            \centering
            \includegraphics[width=\textwidth]{figure2}
    \label{fig:fig2}
    \end{subfigure}
    \caption{\protect\subref{fig:fig1} shows figure 1 and \protect\subref{fig:fig2} shows figure 2.}
\end{figure}
\end{document}
7
  • 1
    Neither of your subfigures actually have a caption. Use \caption{\label{fig:fig1}} and \caption{\label{fig:fig2}} in both subfigures.
    – Werner
    Aug 28, 2013 at 14:08
  • Yes, I left the captions of the subfigures away on purpose. I thought that captions weren't necessary to label subfigures?
    – Robert
    Aug 28, 2013 at 14:18
  • See p 5 of the subcaption documentation for an example very similar to yours.
    – Werner
    Aug 28, 2013 at 14:21
  • 1
    If there is no (a), it is not nice to link to a. The lector wouln't know, what you mean. In this case you should write: The above shows figure 1 (left) and figure 2 (right)
    – LaRiFaRi
    Aug 28, 2013 at 14:25
  • 1
    @Robert: If the sub-caption labels are "hard-coded" in the figures, then you might just as well "hard-code" the actual reference to them: \caption{(a)~shows Figure~1 and~(b) shows Figure~2}. Because if you wish to switch the subfigures, you'd have to change the "hard-coded" image captions as well. As you now know, LaTeX can handle this without the "hard-coding".
    – Werner
    Aug 28, 2013 at 14:50

2 Answers 2

30

You need to add a separate \caption for each subfigure in order to display the label (which can then be referenced):

enter image description here

\documentclass{article}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{caption,subcaption}% http://ctan.org/pkg/{caption,subcaption}

\begin{document}
Some text.

\begin{figure}[ht]
  \centering
  \begin{subfigure}[b]{0.5\linewidth}
    \centering\includegraphics[width=100pt]{example-image-a}
    \caption{\label{fig:fig1}}
  \end{subfigure}%
  \begin{subfigure}[b]{0.5\linewidth}
    \centering\includegraphics[width=100pt]{example-image-b}
    \caption{\label{fig:fig2}}
  \end{subfigure}
  \caption{\subref{fig:fig1} shows Figure~1 and~\subref{fig:fig2} shows Figure~2.}
\end{figure}
\end{document}
0
12

Taken from the subcaption documentation, section "Referencing sub-figures without sub-captions":

If you don’t want to give a sub-figure a caption, because the picture itself already contains the caption, or for some other reason, you can use the command \phantomsubcaption instead of \subcaption, or – when inside a subfigure or subtable environ- ment – \phantomcaption instead of \caption. \phantomsubcaption and \phantomcaption do not have any arguments, and they do not generate any output, but give you an anchor for a \label command which can be placed afterwards. Fur- thermore it increases the sub-figure resp. sub-table counter. Please note that – just like \subcaption – the \phantomsubcaption command must be applied inside its own group, box, or environment.

\documentclass{report}

\usepackage[superscript]{cite}
\usepackage{mathtools}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}
Some text.

\begin{figure}[!h]
    \centering
    \begin{subfigure}[b]{0.4\textwidth}
            \centering
            \includegraphics[width=\textwidth]{figure1}
    \phantomcaption
    \label{fig:fig1}
    \end{subfigure}
    \begin{subfigure}[b]{0.4\textwidth}
            \centering
            \includegraphics[width=\textwidth]{figure2}
    \phantomcaption
    \label{fig:fig2}
    \end{subfigure}
    \caption{\subref{fig:fig1} shows figure 1 and \subref{fig:fig2} shows figure 2.}
\end{figure}
\end{document}

Note: \phantomsubcaption needs subcaption version 1.1 (2011/08/17) or newer.

1
  • If you don't want the hyperlink in the caption (seems a bit silly to do, esp. for a single row figure), consider using \subref* instead of \subref
    – Sterling
    Apr 13, 2021 at 0:06

You must log in to answer this question.

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