Wrong references in subfloat

subfigsubfloats

Recently I asked a question about the incompatibility of caption in revtex4-2. I utilized the suggestions but now I face new problems, e. g.:

  1. the difference between the number style of the figure in the text and below it
  2. I would like to place pictures side by side. I attach the file (I don't know how attach the pictures)
\documentclass[aps,pra,twocolumn,floatfix,footinbib,notitlepage,superscriptaddress,groupaddress,subcaption,caption,showpacs]{revtex4-2}
\usepackage{graphicx,graphics,times,bm,bbm,bbold,amssymb,amsmath,amsfonts,dsfont,hyperref,mathrsfs,color,cancel}
\usepackage{adjustbox}
\usepackage{tabularx}
\usepackage{tabulary}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\usepackage{dcolumn}% Align table columns on decimal point
\usepackage{booktabs}
\usepackage{nicefrac}
\usepackage{subfig}
%\usepackage[mathlines]{lineno}% Enable numbering of text and display math
%\linenumbers\relax % Commence numbering lines
\usepackage{kantlipsum} % dummy text <<<<<<<<<<<<<<<
\begin{document}
   
   \preprint{AIP/123-QED}
       1.  \kant[1-2]
   %\title[Sample title]{Sample Title:\\with Forced Linebreak\footnote{Error!}}% Force line breaks with \\

   \begin{abstract}
       
   \end{abstract}
   
   \keywords{}
   \maketitle
   \begin{quotation}
       %   The ``lead paragraph'' is encapsulated with the \LaTeX\ 
       %   \verb+quotation+ environment and is formatted as a single paragraph before the first section heading. 
       %   (The \verb+quotation+ environment reverts to its usual meaning after the first sectioning command.) 
       %   Note that numbered references are allowed in the lead paragraph.
       %
       %   The lead paragraph will only be found in an article being prepared for the journal \textit{Chaos}.
   \end{quotation}
   
   \section{Introduction}\label{sec:introduction}
   \textcolor{blue}{As Fig.~(\ref{fig1}) and Fig.(\ref{fig2}) show there is a difference between the number of the picure in text and caption }
   \begin{figure}[ht!]\label{fig1}
       \subfloat[\label{subfig2a}]{%}
       \includegraphics[width=0.45\textwidth]{fig1}%
   }\hfill
   \subfloat[\label{subfig2b}]{%
       \includegraphics[width=0.45\textwidth]{fig2}%
   }\hfill
   \caption{caption for \ref{subfig2a} subfloat 1 \ref{subfig2b} subflot2}
\end{figure}

\begin{figure}[ht!]\label{fig2}
   \subfloat[\label{subfig3a}]{%}
   \includegraphics[width=0.45\textwidth]{fig1}%
}\hfill
\subfloat[\label{subfig3b}]{%
   \includegraphics[width=0.45\textwidth]{fig2}%
}\hfill
\caption{caption for \ref{subfig2a} subfloat 1 \ref{subfig2b} subflot2}
\end{figure}

2.  \kant[2-4]  
\end{document}
%
% ****** End of file aipsamp.tex ******

Best Answer

Try

\documentclass[aps,pra,twocolumn,floatfix,footinbib,notitlepage,superscriptaddress,groupaddress,subcaption,caption,showpacs]{revtex4-2}
\usepackage{graphicx}
\usepackage{xcolor}

%\usepackage[caption=false]{subfig}
\usepackage{subcaption}
\usepackage{lipsum}

\begin{document}
\section{Introduction}
\label{sec:introduction}
    \textcolor{blue}{As Fig.~(\ref{fig1}) and Fig.(\ref{fig2}) show there is a difference between the number of the picture in text and caption}
\begin{figure}[ht!]
\setkeys{Gin}{width=0.45\linewidth}
\subfloat[\label{subfig1a}]{\includegraphics{example-image-a}}
    \hfill
\subfloat[\label{subfig1b}]{\includegraphics{example-image-b}}

\caption{caption for \ref{subfig1a} subfloat-1 \ref{subfig1b} subflot-2}
\label{fig1}
\end{figure}

Referencing subfigures \ref{subfig1a} and \ref{subfig1b}.
\begin{figure}[ht!]
\setkeys{Gin}{width=0.45\linewidth}
\subfloat[\label{subfig2a}]{\includegraphics{example-image-a}}
    \hfill
\subfloat[\label{subfig2b}]{\includegraphics{example-image-b}}

\caption{caption for \ref{subfig2a} subfloat-1 \ref{subfig2b} subfloat-2}
\label{fig2}
\end{figure}

\begin{figure}[ht!]
\setkeys{Gin}{width=0.45\linewidth}
\subfloat[\label{subfig3a}]{\includegraphics{example-image-a}}
    \hfill
\subfloat[\label{subfig3b}]{\includegraphics{example-image-b}}

\caption{caption for \ref{subfig3a} subfloat-1 \ref{subfig3b} subfloat-2}
\label{fig3}
\end{figure}

Referencing subfigures \ref{subfig2a}, \ref{subfig2a},\ref{subfig3a} and \ref{subfig3b}, and figures \ref{fig2} and \ref{fig3}.

\lipsum[1-3]
\end{document}

enter image description here

From comparison of above code and your you should observe:

  • labels of figures should be after captions
  • MWE for your problem can be significantly shorter
  • It works also with recent subcaption package (version 1.3 or newest)
  • You not need to load graphics package (it is load by graphicx).
  • Please for images load example-image`s, which available to all

Addendum:

In the cases, that you like to have for example for image in figure, which span bot document columns, than you need to use figure* float. For example:

\documentclass[aps,pra,twocolumn,floatfix,footinbib,notitlepage,superscriptaddress,groupaddress,subcaption,caption,showpacs]{revtex4-2}
\usepackage{graphicx}
\usepackage{xcolor}

\usepackage{subcaption}
\usepackage{lipsum}

\begin{document}
\section{Introduction}
\label{sec:introduction}
\textcolor{blue}{As Fig.~(\ref{fig1}) and Fig.(\ref{fig2}) show there is a difference between the number of the picture in text and caption}
\begin{figure}[!ht]
\setkeys{Gin}{width=0.45\linewidth}
\subfloat[\label{subfig1a}]{\includegraphics{example-image-a}}
    \hfill
\subfloat[\label{subfig1b}]{\includegraphics{example-image-b}}

\caption{caption for \ref{subfig1a} subfloat-1 \ref{subfig1b} subflot-2}
\label{fig1}
\end{figure}

\lipsum[1]
\begin{figure*}
\setkeys{Gin}{width=0.22\linewidth}
\subfloat[\label{subfig2a}]{\includegraphics{example-image-a}}
    \hfill
\subfloat[\label{subfig2b}]{\includegraphics{example-image-b}}
    \hfill
\subfloat[\label{subfig2c}]{\includegraphics{example-image-a}}
    \hfill
\subfloat[\label{subfig2d}]{\includegraphics{example-image-b}}

\caption{caption for subfloats \ref{subfig2a}, \ref{subfig2b},  
         \ref{subfig2c} and \ref{subfig2d}.}
\label{fig2}
\end{figure*}

\textcolor{blue}{Referencing subfigures \ref{subfig2a}, \ref{subfig2a},\ref{subfig2c} and \ref{subfig2d} in figure \ref{fig2}.}

\lipsum[2-11]
\end{document}

Figure will appear at the top of the next page from point of it insertion.

enter image description here

Related Question