3

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}

Desired result (if image is a black rectangle)

1 Answer 1

3

This seems to work but I don't really understand why. Note that I don't have your images so I had to replace them and the white doesn't show up as well on the examples from mwe. However, you can see the captions if you look closely...

\documentclass{article}
\usepackage{graphicx,caption,subcaption,tikz}
\usetikzlibrary{backgrounds}

\usetikzlibrary{calc}
\DeclareCaptionFormat{overlay}{\gdef\capoverlay{#1#2#3\par}}
\DeclareCaptionStyle{overlay}{format=overlay}
\DeclareCaptionFormat{suboverlay}{\gdef\subcapoverlay{(\thesubfigure) #3\par}}
\DeclareCaptionStyle{suboverlay}{format=suboverlay}

\newcommand{\captionOverlay}[1]{%
  \captionsetup{format=overlay}
  \caption{}%
  \begin{tikzpicture}
    \node [inner sep=0] (image) {#1};
    \draw node [white] {\capoverlay};
  \end{tikzpicture}%
}
\captionsetup[subfigure]{format=suboverlay}
\newcommand{\subcaptionOverlay}[1]{%
  \subcaption{}%
  \begin{tikzpicture}
    \node [inner sep=0] (image) {#1};
    \draw node [white] {\subcapoverlay};
  \end{tikzpicture}%
}

\begin{document}

  \begin{figure}[htpb]
    \centering
    \begin{subfigure}[b]{.3\textwidth}
      \centering
      \subcaptionOverlay{\includegraphics[width=\textwidth]{example-image-a}}
      \label{subA}
    \end{subfigure}
    \begin{subfigure}[b]{0.3\textwidth}
      \centering
      \subcaptionOverlay{\includegraphics[width=\textwidth]{example-image-a}}
      \label{subB}
    \end{subfigure}
    \caption{}
    \label{mainFig}
  \end{figure}

  \begin{figure}[htpb]
    \centering
    \captionOverlay{\includegraphics[width=\textwidth]{example-image-a}}
    \label{normalFig}
  \end{figure}

\end{document}

Overlaid subcaptions

2
  • Yes, that works, thanks! I didn't mention that I wanted to be able to move the caption relative to the figure (I actually want it top left instead of center), but it seems that this is easy to do by changing the \draw line to \draw node [white] at (x,y) {\subcapoverlay}; where (x,y) is an offset for the caption. This might be obvious to others but I haven't really used TikZ before.
    – Cog77
    Commented Jun 29, 2014 at 8:15
  • @Cog77 Glad it worked. Yes, that offset should do it. I only removed it because it seemed unnecessary in the MWE. TikZ takes some getting used to, I think. (I am definitely not in the select group of those who have got used to it yet!)
    – cfr
    Commented Jun 29, 2014 at 14:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .