1

I have the following code:

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{floatrow}

%\usepackage{caption}
\usepackage{subcaption}
%\usepackage{subfig}
\usepackage{float}

\begin{document}

\noindent
Cross-referencing test: Figure \ref{figure}

\noindent
The figure is \ref{figure_1} and not 1 (a). \\
The figure is \ref{figure_2} and not 1 (b). 

\begin{figure}[!ht]
\centering
    \begin{subfigure}{0.5\textwidth}            
            \includegraphics[width=\textwidth]{img/tex.png}
            \caption{Caption 1}
            \label{figure_1}
    \end{subfigure}%
    %\qquad
     %add desired spacing between images, e. g. ~, \quad, \qquad etc.
      %(or a blank line to force the subfigure onto a new line)
    \begin{subfigure}{0.5\textwidth}
            \centering
            \includegraphics[width=\textwidth]{img/tex.png}
            \caption{Caption 2}
            \label{figure_2}
    \end{subfigure}
    \caption{Caption of the figure}\label{figure}

\end{figure}

\end{document}

At the moment, if I cross-reference the subfigures in the text, they appear as follows: 1.a and 1.b. How do I make the cross-references appear as follows: 1 (a) and 1 (b)?

1 Answers1

2

You may achieve your objective by providing the following code block in the preamble. Note that I'm assuming that you want an unbreakable space between the figure number and the subfigure letter.

\usepackage{subcaption}
\renewcommand\thesubfigure{(\alph{subfigure})}
\captionsetup[subfigure]{labelformat=simple}
\makeatletter
\renewcommand{\p@subfigure}{\thefigure~} % unbreakable space after fig. num.
\makeatother

A full MWE (minimum working example):

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amssymb} % for "\checkmark" macro
\usepackage[demo]{graphicx} % remove "demo" option in real document
\usepackage{float} 

\usepackage{subcaption}
\renewcommand\thesubfigure{(\alph{subfigure})}
\captionsetup[subfigure]{labelformat=simple}
\makeatletter
\renewcommand{\p@subfigure}{\thefigure~} % unbreakable space after fig. #
\makeatother

\begin{document}

\noindent
Basic cross-referencing test: Figure \ref{figure}. \\
1st subfigure cross-ref is \ref{figure_1}. Desired: 1 (a). $\checkmark$ \\
2nd subfigure cross-ref is \ref{figure_2}. Desired: 1 (b). $\checkmark$

\begin{figure}[!ht]
    \begin{subfigure}{0.475\textwidth}
        \includegraphics[width=\textwidth]{img/tex.png}
        \caption{Caption 1}
        \label{figure_1}
    \end{subfigure}%
    \hfill % maximize the horizontal separation 
    \begin{subfigure}{0.475\textwidth}
        \includegraphics[width=\textwidth]{img/tex.png}
        \caption{Caption 2}
        \label{figure_2}
    \end{subfigure}
\caption{Overall Caption} 
\label{figure}
\end{figure}

\end{document} 
Mico
  • 506,678
  • Hello @Mico, your example worked, but neither figure is showing up in the text. Two black boxes appear in the figure region as well as in your response. How do I make the figures appear normally in my pdf file? This is just missing so that I can mark your answer as a solution. – ricardoramos Feb 08 '18 at 17:39
  • 1
    @ricardoramos - Did you see the comment remove "demo" option in real document next to \usepackage[demo]{graphicx}? (I had to use the demo option in my code since I don't have access to your graphics files.) – Mico Feb 08 '18 at 18:58
  • 1
    Thank​ you very much for your help @Mico and would like to congratulate you for their knowledge of Latex! – ricardoramos Feb 08 '18 at 21:43
  • @ricardoramos - Many thanks for the compliment!! You're most welcome. – Mico Feb 08 '18 at 21:47