46

I've used subfloat to include subfigures, with their relevant labels but with empty captions. what I'm going to do is to reference these figures with \subref in the main caption of the figure. But what I get is only ?? signs in the caption [main caption]. I can't figure out what's the problem. this is what I have:

\begin{figure}
\centering
\subfloat[][]
{
\includegraphics[scale=0.55]{signal}
\label{signal_model}
}
\subfloat[][]
{
\includegraphics[scale=0.55]{amplitude}
\label{amplitude_mod}
}
\caption{phase noise variability: \subref{signal model} some text \subref{amplitude_mod} some other text}
\end{figure}
pasha
  • 719
  • 1
    You need a subcaption nevertheless, even if empty, so that the subfigure is at least labelled. – JLDiaz Jul 08 '12 at 16:53
  • 1
    can you turn your snippet into a complete MWE? – cmhughes Jul 08 '12 at 17:13
  • 1
    Actually I want to have labeled subfigures, i.e. to have (a) (b) (c) etc. below each subfigure without sub-captions. but I want to cite or refer them in the main caption of the figure (I thought it's possible with '\subref') – pasha Jul 08 '12 at 17:24
  • I got the answer, but I think something missing with subcaption package:

    "The subcaption package was updated to version 1.1. It got a new option for selecting a format used by \subref, and a new command called \phantomsubcaption for referencing a sub-figure without caption. Furthermore the subcaption package documentation was enhanced regarding references."

    – pasha Jul 08 '12 at 17:49
  • 1
    If you are really using the subcaption package (and not the subfig one) please use \subcaptionbox instead of \subfloat and put the \label inside the caption text, not inside the sub-figure body. –  Jul 09 '12 at 06:45
  • 1
    Turns out refcheck can also mess with \subref. Was getting ??sub@<ref> instead of (a), and it showed properly as (a) when I turned off refcheck. Note that \protect\subref while refcheck was on didn't fix it. – Sterling Mar 15 '22 at 07:04
  • 1
    Same behaviour as @Sterling's with showkeys instead of refcheck: including \protect does not help, but turning off showkeys makes the subrefs appear properly. – Saibot May 21 '22 at 11:58

1 Answers1

59

You need to \protect the \subref command in the caption:

\caption{Phase noise variability: \protect\subref{signal_model} some text 
  \protect\subref{amplitude_mod} some other text}

MWE:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subfig}
\begin{document}

\begin{figure}
\centering

\subfloat[][]{\includegraphics[scale=0.55]{example-image-a}\label{signal_model}}
\subfloat[][]{\includegraphics[scale=0.55]{example-image-b}\label{amplitude_mod}}

\caption{Phase noise variability: \protect\subref{signal_model} some text 
  \protect\subref{amplitude_mod} some other text}

\end{figure}
\end{document}

enter image description here

egreg
  • 1,121,712
  • 3
    +1 and BIG thanks for this .. I was struggling with this (my documentclass prohibits use of caption, and therefore subcaption, where for some reason \subref does not need \protect) – user1207217 Mar 14 '13 at 16:53
  • 17
    Thanks for this answer. I just wanted to add the reason for using \protect: The command \subref{} is a fragile command, and \caption has a moving argument. The \protect command is needed whenever we use a fragile command in a moving argument. – hadi May 27 '14 at 21:47
  • 3
    @hadi That doesn't really do much to explain the need to \protext that \subref call. It works perfectly fine in the subfigure package, so at a first look it is a regression in behaviour. Why is making \subreffragile an advantage in any way, other than breaking code which used to work? – E.P. Aug 31 '16 at 13:20
  • 1
    @hadi - thanks! Now a newbie like me still needs to read this and then have a look at this (harder still). – Tomasz Gandor Mar 21 '20 at 04:10
  • 1
    Thanks, can someone explain what protect does? – Hao S Sep 24 '21 at 01:56
  • Is it possible to automatically display them in the correct order? That is, instead of manually referring to (a) before (b) in the main caption, let this be done automatically? – sam wolfe Mar 11 '22 at 15:38
  • @samwolfe Sorry, I don't get it. – egreg Mar 11 '22 at 15:42
  • @egreg Please see this question: https://tex.stackexchange.com/questions/636701/automatic-subcaption-listing – sam wolfe Mar 11 '22 at 15:44
  • When I use \protect\subref, I get the letters, but not with parenthesis around them as you do. why do you get with parenthesis but I don't? – StrawberryFieldsForever Oct 03 '23 at 17:33