[Tex/LaTex] Put subfigure labels inside figures using subfig package

subfloats

I'd like to put subfigure labels (a), (b), etc., at the top left corner of subfigures like the following figure:

image

I know there are some workarounds like this post (with stackengine) or this post (with subcaption). However, I don't want to use stackengine unless there's really no way to do so, and REVTeX4 rules out the subcaption option. I therefore wonder if there exists a workaround specifically for subfig package (and potentially compatible with REVTeX4).

The following MWE provides an unsuccessful attempt to achieve the desired effect:

\documentclass{revtex4-1}
\usepackage{graphicx}
\usepackage{subfig}
\captionsetup[subfloat]{farskip=-10pt, position=top}

\begin{document}
\begin{figure}
\centering
    \subfloat[][]{
    \includegraphics[width=0.45\linewidth]{example-image-a}}
    \subfloat[][]{
    \includegraphics[width=0.45\linewidth]{example-image-b}}\\
    \subfloat[][]{
        \includegraphics[width=0.45\linewidth]{example-image-c}}
    \subfloat[][]{
        \includegraphics[width=0.45\linewidth]{example-image-a}}
    \caption{abcd}
\end{figure}
\end{document}

Best Answer

I'd suggest writing your own macro to place the sub-figure numbering:

enter image description here

\documentclass{revtex4-1}% http://ctan.org/pkg/ltxutil
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\newcommand{\subfigimg}[3][,]{%
  \setbox1=\hbox{\includegraphics[#1]{#3}}% Store image in box
  \leavevmode\rlap{\usebox1}% Print image
  \rlap{\hspace*{10pt}\raisebox{\dimexpr\ht1-2\baselineskip}{#2}}% Print label
  \phantom{\usebox1}% Insert appropriate spcing
}
\begin{document}
\begin{figure}
  \centering
  \begin{tabular}{@{}p{0.45\linewidth}@{\quad}p{0.45\linewidth}@{}}
    \subfigimg[width=\linewidth]{A)}{example-image-a} &
    \subfigimg[width=\linewidth]{B)}{example-image-b} \\
    \subfigimg[width=\linewidth]{C)}{example-image-c} &
    \subfigimg[width=\linewidth]{D)}{example-image-a}
  \end{tabular}
  \caption{A caption}
\end{figure}
\end{document}

Once you have the image, you can print it in a box and extract its dimensions in order to use them to position the label.

The above macro \subfigimg[<options>]{<label>}{<image>} does exactly that. It inserts <image> with options <options> and imprints the <label> 10pt from the left and 2 baseline skips from the top of the image.

No packages nor tomfoolery. Just box manipulation.


Some added flair with string comparison for positioning...

pdfTeX introduced \pdfstrcmp for string comparison in version 1.30:

\pdfstrcmp{<a>}{<b>} compares two strings and returns the strings "0" if equals <b>, "-1" if <a> is less than <b>, "1" if <a> is greater than <b>.

enter image description here

\documentclass{revtex4-1}% http://ctan.org/pkg/ltxutil
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{xkeyval,xcolor}% http://ctan.org/pkg/{xkeyval,xcolor}
\makeatletter
\newlength{\sfp@hseplen}\newlength{\sfp@vseplen}
\define@cmdkey{subfigpos}[sfp@]{pos}[ul]{}% \sfp@pos
\define@cmdkey{subfigpos}[sfp@]{font}[\small]{}% \sfp@font
\define@cmdkey{subfigpos}[sfp@]{vsep}[2\baselineskip]{\setlength{\sfp@vseplen}{\sfp@vsep}}% \sfp@vsep
\define@cmdkey{subfigpos}[sfp@]{hsep}[10pt]{\setlength{\sfp@hseplen}{\sfp@hsep}}% \sfp@hsep
\newcommand{\subfigimg}[3][,]{%
  \setkeys{Gin,subfigpos}{pos,font,vsep,hsep,#1}% Set (default) keys
  \setbox1=\hbox{\includegraphics{#3}}% Store image in box
  \ifnum\pdfstrcmp{\sfp@pos}{ul}=0% UPPER LEFT placement of subfig label
    \leavevmode\rlap{\usebox1}% Print image
    \rlap{\hspace*{\sfp@hsep}\raisebox{\dimexpr\ht1-\sfp@vsep}{\sfp@font{#2}}}% Print label
    \phantom{\usebox1}% Insert appropriate spacing
  \else\ifnum\pdfstrcmp{\sfp@pos}{ur}=0% UPPER RIGHT placement of subfig label
    \leavevmode\usebox1% Print image
    \llap{\raisebox{\dimexpr\ht1-\sfp@vsep}{\sfp@font{#2}}\hspace*{\sfp@hsep}}% Print label
  \else\ifnum\pdfstrcmp{\sfp@pos}{lr}=0% LOWER RIGHT placement of subfig label
    \leavevmode\usebox1% Print image
    \llap{\raisebox{\sfp@vsep}{\sfp@font{#2}}\hspace*{\sfp@hsep}}% Print label
  \else% Assume LOWER LEFT placement of subfig label
    \leavevmode\rlap{\usebox1}% Print image
    \rlap{\hspace*{\sfp@hseplen}\raisebox{\sfp@vsep}{\sfp@font{#2}}}% Print label
    \phantom{\usebox1}% Insert appropriate spacing
  \fi\fi\fi
}
\makeatother
\begin{document}
\begin{figure}
  \centering
  \begin{tabular}{@{}p{0.45\linewidth}@{\quad}p{0.45\linewidth}@{}}
    \subfigimg[width=\linewidth,pos=lr]{A)}{example-image-a} &
    \subfigimg[width=\linewidth,pos=ll,font=\color{green!30!red}]{B)}{example-image-b} \\
    \subfigimg[width=\linewidth,pos=ur,font=\LARGE]{C)}{example-image-c} &
    \subfigimg[width=\linewidth,pos=ul]{D)}{example-image-a}
  \end{tabular}
  \caption{A caption}
\end{figure}
\end{document}

The above MWE adds the keys pos, font, hsep and vsep to the mix. In turn they allow for selecting the position of the label, which font to use, the horizontal separation and vertical separation.