[Tex/LaTex] Placing panel label in the corner of the figure

subcaptionsubfloats

I am trying to place labels (A, B, C…) onto the top-left corners of subfigures. I tried it with caption/subcaption packages and by defining a negative skip like \captionsetup[sub]{skip=-5pt}, but the labels are typeset under the figure. Here is what I got right now:

\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}

\usepackage{caption}
\usepackage{subcaption}
    \captionsetup[sub]{labelfont=bf, position=top, 
          singlelinecheck=false, labelformat=simple, skip=-5pt}
    \renewcommand\thesubfigure{\Alph{subfigure}}

\begin{document}

\begin{figure}
    \centering
    \begin{subfigure}{.45\linewidth}
        \caption{}\label{fig:1a}
        \textcolor{gray}{\rule{5cm}{3cm}}
    \end{subfigure}
    \begin{subfigure}{.45\linewidth}
        \caption{}\label{fig:1b}
        \textcolor{gray}{\rule{5cm}{3cm}}
    \end{subfigure}
    \caption{This is Figure 1.}\label{fig:1}
\end{figure}

\end{document}

Label is partially under the image.

As you can see, A and B are hiding behind the grey box, but I would like to have them rendered on top. The desired result would be like this:

enter image description here

What would be the most elegant solution to get the z-order correct?

Best Answer

While slightly overkill, you can use TikZ, along with \phantomsubcaption and \subref, to do this:

output of code

\documentclass{article}
\usepackage{tikz} % loads graphicx and xcolor

\usepackage{caption}
\usepackage{subcaption}
\renewcommand\thesubfigure{\Alph{subfigure}}

\begin{document}

\begin{figure}
  \centering
  {\phantomsubcaption\label{fig:1a}}
  {\phantomsubcaption\label{fig:1b}}
  \tikz\node[inner sep=0pt,label={[anchor=north west]north west:\subref{fig:1a}}] {\textcolor{gray}{\rule{5cm}{3cm}}};
  \tikz\node[inner sep=0pt,label={[anchor=north west]north west:\subref{fig:1b}}] {\textcolor{gray}{\rule{5cm}{3cm}}};

  \caption{This is Figure 1.}\label{fig:1}
\end{figure}
\end{document}

Or ...

Good ol' picture mode.

\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}

\usepackage{caption}
\usepackage{subcaption}
\renewcommand\thesubfigure{\Alph{subfigure}}

\begin{document}

\begin{figure}
  \centering
  \setlength\unitlength{1cm}
  {\phantomsubcaption\label{fig:1a}}
  {\phantomsubcaption\label{fig:1b}}

\textcolor{gray}{\rule{5cm}{3cm}}%
\begin{picture}(0,0)
\put(-4.8,2.6){\subref{fig:1a}}
\end{picture}
\textcolor{gray}{\rule{5cm}{3cm}}%
\begin{picture}(0,0)
\put(-4.8,2.6){\subref{fig:1b}}
\end{picture}

\caption{This is Figure 1.}\label{fig:1}
\end{figure}
\end{document}