0

Consider the following

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}

\begin{document}

\begin{figure}[htpb] \centering \begin{minipage}{0.3\linewidth} \begin{subfigure}[t]{\textwidth} \includegraphics[width=\linewidth]{example-image-a} \caption{} \label{figa} \end{subfigure} \end{minipage} \begin{minipage}{0.3\linewidth} \begin{subfigure}[t]{\textwidth} \includegraphics[width=\linewidth]{example-image-b} \caption{} \label{figb} \end{subfigure} \end{minipage} \par\medskip \caption{Example caption.} \label{fig} \end{figure}

\end{document}

which gives

enter image description here

How can I position the captions of the subfigures, (a) and (b), in any position overlapping the image? For example, how can I get

enter image description here

Edit: For example, using something like

\captionsetup[subfigure]{font={bf,small}, skip=-100pt, margin=0.1cm, singlelinecheck=false}

almost does the trick

enter image description here

but not quite. Putting \caption before \includegraphics also does not seem to help, as the caption moves to the back layer. Any ideas?

sam wolfe
  • 257
  • 1
    Unfortunately your posted code can't compile. Please add the missing lines. Nobody here likes to reengineer preambles. Thank you – MS-SPO Aug 17 '23 at 07:20
  • 1
    @MS-SPO Thanks for pointing that out. Please see the edit. A potential solution without tiks would be appreciated. – sam wolfe Aug 17 '23 at 14:24
  • Possible duplicate of https://tex.stackexchange.com/questions/621260/position-subcaption-within-subfigure-at-the-top-inset – John Kormylo Aug 17 '23 at 19:08

1 Answers1

3

In those kind of scenarios, I think tikz provides good solutions, at least to start with. The only thing is in this case you have to split actions of a regular \caption because the latter cannot be used as a argument of \node (see section 7.3 of documentation).

Below is one example. Referencing to each subfigure should work, as usually. Since you keep them on the same page, I just stuck \captionlistentrys to generate anchors, so that you can reference each subfigure. Then, \captiontext* is just to produce empty labels. This way there shouldn't be a problem to append more images.

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{tikz}
\usepackage{kantlipsum}

\usetikzlibrary{positioning}

\newcounter{subfig}

\begin{document} \kant[1]

\begin{figure}[tbh] \setcounter{subfig}{0} \newcommand\typecap{\stepcounter{subfig}\captiontext*[\value{subfig}]{}} \captionsetup{position=bottom, skip=3pt} \centering \begin{subcaptiongroup} \captionlistentry{}\label{figa} % Required to reference subfigures \captionlistentry{}\label{figb} \tikz[ node distance=3pt, caption/.style={ anchor=north west, font=\bfseries, outer sep=6pt, }, relpos/.style={ right=of #1.south east, anchor=south west, }, ] { \node (A) {\includegraphics[width=0.3\textwidth]{example-image-a}}; \node (B) [relpos=A] {\includegraphics[width=0.2\textwidth]{example-image-b}}; \node (C) [relpos=B] {\includegraphics[width=0.35\textwidth]{example-image-b}}; \node at (A.north west) [caption] {\typecap}; \node at (B.north west) [caption] {\typecap}; \node at (C.north west) [caption] {\typecap}; } \end{subcaptiongroup} \caption{Example caption.}\label{fig} \end{figure}

\kant[2] \end{document}

enter image description here

Celdor
  • 9,058
  • 1
    Thanks, this is quite useful! It's a bit sad that I can't get away with my approach. Positioning \caption above \includegraphics seems to almost solve the issue, but the graphics still appear on the front layer, hiding the caption. Nonetheless, this is a great answer. – sam wolfe Aug 17 '23 at 14:27
  • If I may ask, how would you use minipage or \quad to display the subfigures in multiple rows? – sam wolfe Aug 17 '23 at 17:26
  • Also, is it possible to slightly push the caption to the right or further down, to adjust depending on the image? In my case I have a framed plot, so ideally I would like it to be positioned a bit more towards the center, so that the subcaption does not overlap the frame or one of the axes. – sam wolfe Aug 17 '23 at 17:49
  • Perhaps remove outer sep or set it to 0pt, that is use outer sep=0pt to remove margins. The labels will stick to left-hand side upper corner. Then, you can append xshift and/or yshift to move labels around the default anchor. – Celdor Aug 17 '23 at 20:17