I'm trying to place my figure captions on top of the figures. I've found some code that does this and I can get it to work for a normal figure - e.g. I can overlay "Figure 1" on top of the figure. However it doesn't work for overlaying the caption of subfigures e.g. "(a)" etc. See end of my post for a drawn example of what I'd like to do.
In the example below I've pasted the code I'm trying use. The subfigures appear in the document but are missing the caption. The normal figure works correctly and the caption is displayed in the center of the figure in white. I don't really understand how this code works so I'm having trouble fixing it for the subfigure case, but I'm guessing it has something to do with the \gdef\capoverlay{#1#2#3\par}
line? Does anyone know how to get this working please?
Thanks for any help!
\documentclass{article}
\usepackage{graphicx,caption,subcaption,tikz}
\usetikzlibrary{calc}
\DeclareCaptionFormat{overlay}{\gdef\capoverlay{#1#2#3\par}}
\DeclareCaptionStyle{overlay}{format=overlay}
\newcommand{\captionOverlay}[1]{%
\captionsetup{format=overlay}
\caption{}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0](image)at(0,0){#1};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\draw let \p1 = (1,0) in node[color=white] at (0.5,0.5) {\capoverlay};
\end{scope}
\end{tikzpicture}%
}
\begin{document}
\begin{figure}[htpb]
\centering
\begin{subfigure}[b]{0.3\textwidth}
\centering
\captionOverlay{\includegraphics[width=\textwidth]{image03}}
\label{subA}
\end{subfigure}
%
%
\begin{subfigure}[b]{0.3\textwidth}
\centering
\captionOverlay{\includegraphics[width=\textwidth]{image}}
\label{subB}
\end{subfigure}
\caption{}
\label{mainFig}
\end{figure}
\begin{figure}[htpb]
\centering
\captionOverlay{\includegraphics[width=\textwidth]{image}}
\label{normalFig}
\end{figure}
\end{document}