Put subfigure labels inside figures using subfig package

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.


As the author of stackengine and the provider of a solution on Subfigures with number inside figure, I should be a bit loathe to undercut myself and provide an alternative solution. But here it is (we're all in it together). Not nearly as elegant, quite brute force, but it avoids use of the package.

EDITED to remove vestigial part of the solution.

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

\includegraphics[width=5in]{vjbrp}\llap{
  \parbox[b]{4.5in}{(A)\\\rule{0ex}{2.2in}
  }}

yyy
\end{document} 

enter image description here


I wanted to fill the background of the label (a), (b) and I thought to insert the image inside the tikzpicture-environment as shown here:

How can I embed an external image within a tikzpicture environment?

So this is the result: duck with mark (a) and (b)

\documentclass{article}

\usepackage{graphicx}
\usepackage{tikz}
\usepackage{subcaption}

\begin{document}
\begin{figure}[htbp]
    \centering
    \begin{subfigure}[htbp]{0.48\textwidth}
        \begin{tikzpicture}
        \node[inner sep=0pt] (duck) at (0,0)
        {\includegraphics[width=\textwidth]{duck}};
        \node[align=center,fill=white,draw] at (-2.4,2.1) {(a)};
        \end{tikzpicture}
    \end{subfigure}
    ~
    \begin{subfigure}[htbp]{0.48\textwidth}
        \begin{tikzpicture}
        \node[inner sep=0pt] (duck2) at (0,0)
        {\includegraphics[width=\textwidth]{duck}};
        \node[align=center,fill=white,draw] at (-2.4,2.1) {(b)};
        \end{tikzpicture}
    \end{subfigure}
    \caption{text}
    \label{duck}
\end{figure}
\end{document}

Picture of the duck: https://www.thoughtco.com/fun-facts-about-ducks-4043231

Tags:

Subfloats