16

I'm kinda new on LaTeX and I have a little problem. I'm using the subfigure package and trying to reference my subfigures with \label{key}, but appears "(??)" when I do that.

Here's the code:

\documentclass[11pt,twocolumn]{article}
\usepackage[spanish]{babel}
\usepackage{graphicx, multicol, latexsym, amsmath, amssymb}
\usepackage{blindtext}
\usepackage{subfigure}
\usepackage{caption}
\usepackage{capt-of}
\usepackage{tabu}
\usepackage{hyperref}
\usepackage{booktabs}% for better rules in the table
\usepackage{anysize} % Soporte para el comando \marginsize
\setlength\parindent{0pt}
\spanishdecimal{.}


%===================================================  


\begin{document}


\begingroup
\centering
\begin{figure}[htbp]
\centering
\subfigure[first caption.]{\includegraphics[width=0.2\textwidth]{fig1a}}\label{fig:1a}
\subfigure[second caption.] {\includegraphics[width=0.185\textwidth]{fig1b}}\label{fig:1b}
\subfigure[third caption.]{\includegraphics[width=0.2\textwidth]{fig1c}}\label{fig:1c}
\caption{General caption.} \label{fig:1}
\end{figure}

\endgroup

Using \ref{fig:1a} appears ``(??)'', but using \ref{fig1}, appears ``(1)''.

\end{document}

If someone could help me, I will appreciate it.

Greetings from South America.

3
  • 3
    Welcome to TeX.SX! subfigure is outdated/deprecated. Use subfig for or subcaption example instead
    – user31729
    Commented Jun 7, 2015 at 17:01
  • 1
    @Christian Hupfer: Or floatrow. Btw what is a subcaption example?
    – Bernard
    Commented Jun 7, 2015 at 17:04
  • And remember that you have to run latex twice for it to produce all the cross references, if you don't you might be getting that ?? instead of the actual reference. Commented Jun 7, 2015 at 17:06

3 Answers 3

7

Here is subcaption solution, which has to been used in its own box or environment (that's a little bit tedious) Otherwise, everything works like normal.

The optional argument to \subcaption , i.e. the one enclosed with [...] will appear in the List of Figures (Lof). If no such option is given, then the value of the mandatory argument {...} will go to the LoF.

If removed the \begingroup...\endgroup pair, since \begin{figure}...\end{figure} establishs grouping already.

\documentclass[11pt,twocolumn]{article}
\usepackage[spanish]{babel}
\usepackage[demo]{graphicx}
\usepackage{multicol, latexsym, amsmath, amssymb}
\usepackage{blindtext}
\usepackage{subcaption}
\usepackage{caption}
\usepackage{tabu}
   \usepackage{booktabs}% for better rules in the table
\usepackage{anysize} % Soporte para el comando \marginsize
\usepackage{hyperref}
\setlength\parindent{0pt}
\spanishdecimal{.}


%===================================================  


\begin{document}


\begin{figure}[htbp]
\centering
\begin{minipage}{0.3\textwidth}
  \centering
\includegraphics[width=0.2\textwidth]{fig1a}
\subcaption[first caption.]{First}\label{fig:1a}
\end{minipage}%
\begin{minipage}{0.3\textwidth}
  \centering
\includegraphics[width=0.185\textwidth]{fig1b}
\subcaption[second caption.]{Second}\label{fig:1b}
\end{minipage}%
\begin{minipage}{0.3\textwidth}
  \centering
\includegraphics[width=0.185\textwidth]{fig1c}
\subcaption[third caption.]{Third}\label{fig:1c}
\end{minipage}

\caption{General caption.} \label{fig:1}
\end{figure}

Using \verb|\ref{fig:1a}| now appears as ``\ref{fig:1a}'' and using \verb|\ref{fig:1}|, appears ``\ref{fig:1}''.

\end{document}

enter image description here

8
  • Awesome, it worked perfectly! Thank u very much.
    – hyriusen
    Commented Jun 7, 2015 at 18:31
  • Another question emerged: What can I do if a subcaption is too wide and overcomes the other image? How can I separate both images? I read about commands \hfill, \quad and \qquad, but they didn't fixed it
    – hyriusen
    Commented Jun 7, 2015 at 18:54
  • The \centering before the figure doesn't really do anything I suppose. subcaption loads caption, so loading the latter in addition is unnecessary. (I know all of that comes from the OPs code.) And it might be useful to note that the optional argument to subcaption is what will be printed in the list of figures. Commented Jun 7, 2015 at 19:57
  • @TorbjørnT. You are right: I've used most of the code to get a working, but not elegant solution, I must admit
    – user31729
    Commented Jun 7, 2015 at 19:58
  • 1
    Why using minipages (that you hate) and subcaptions? You can use subfigure and subtable (with a similar syntax to minipage) and use standard caption. More intuitive syntax.
    – Johannes_B
    Commented Jun 7, 2015 at 20:07
16

Don't use subfigure, that has been obsolete for 15 years or so.

With subfig and its \subfloat command that substitute the obsolete package, the label should go in the optional argument (the one for the subcaption).

\documentclass[11pt,twocolumn]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc} % ensure your document is UTF-8
\usepackage[spanish]{babel}

\usepackage[demo]{graphicx} % demo option just for testing
\usepackage{subfig}
\usepackage{caption}

\begin{document}

\begin{figure}[htbp]
\centering
\subfloat[first caption.\label{fig:1a}]{\includegraphics[width=0.2\textwidth]{fig1a}}\hfill
\subfloat[second caption.\label{fig:1b}] {\includegraphics[width=0.185\textwidth]{fig1b}}\hfill
\subfloat[third caption.\label{fig:1c}]{\includegraphics[width=0.2\textwidth]{fig1c}}
\caption{General caption.} \label{fig:1}
\end{figure}

Subfloat reference: \ref{fig:1a}

Figure reference: \ref{fig:1}

\end{document}

Note that \begingroup\centering...\endgroup around the figure is an error.

enter image description here

However, you may be wanting to use subcaption, that also behaves much better with hyperref.

\documentclass[11pt,twocolumn]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc} % ensure your document is UTF-8
\usepackage[spanish]{babel}

\usepackage[demo]{graphicx} % demo option just for testing
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{figure}[htbp]
\centering
\setlength{\lineskip}{\medskipamount}
\subcaptionbox{first caption.\label{fig:1a}}{\includegraphics[width=0.2\textwidth]{fig1a}}\hfill
\subcaptionbox{second caption.\label{fig:1b}}{\includegraphics[width=0.185\textwidth]{fig1b}}\hfill
\subcaptionbox{third caption.\label{fig:1c}}{\includegraphics[width=0.2\textwidth]{fig1c}}
\caption{General caption.} \label{fig:1}
\end{figure}

Subfloat reference: \ref{fig:1a} 

Figure reference: \ref{fig:1}

\end{document}
1
  • Is subcaption not compatible center environment? Commented Feb 5, 2021 at 18:44
0

For anyone who has to use the package subfigure, the issue can be resolved by shifting the label into the bracket like so:

\subfigure[first caption.]{\includegraphics[width=0.2\textwidth]{fig1a}\label{fig:1a}}

You must log in to answer this question.

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