Place subcaption labels topright of subfigures using subcaption package

labelssubcaption

I am trying to place the subcaption labels at the topright corner of the subfigures, using the \subcaption package. I have sort of managed to do it with the margin setting, but I have a couple of issues with it:

  • I would prefer having an automatic method for all figures rather than trial and error at the right margin={a, b}
  • I don't think I understand what exactly the margin setting is doing. In my thesis some figures/subfigures have different results with the same margin setting, e.g. not shifitng as far to the right, causing a vertical offset of two subfigures, etc.

Here's a MWE that shows where I'd like the labels to sit, however I cannot get the behaviour described in the second point to replicate outside of my thesis document (it's quite large and I have no idea what is causing the poor interaction – hopefully a better method for getting these labels where I want or a proper understanding of the margin setting these issues will be avoided):

\documentclass[12pt]{report}

\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\captionsetup[subfigure]{
    singlelinecheck = off,
%   margin = {-6mm, 0mm},
}

\begin{document}
    
    %
    \begin{figure}[tbp]
        \centering
        %
        \begin{subfigure}[t]{0.4\linewidth}
            \captionsetup{margin={-4mm, 0mm}}
            \centering
            \subcaption{}
            \includegraphics[height=3cm, width=5cm]{example-image-a}
        \end{subfigure}
        %
        \qquad
        %
        \begin{subfigure}[t]{0.4\linewidth}
            \captionsetup{margin={8mm, 0mm}}
            \centering
            \subcaption{}
            \includegraphics[height=3cm, width=2.5cm]{example-image-b}
        \end{subfigure}
        %
        \caption{Figure caption.}
    \end{figure}
    %
    
\end{document}

Best Answer

Two things are needed.

(1) Use a global \captionsetup that put the subcaption in the left top corner of all figures.

\captionsetup[subfigure]{% changed <<<<<<<<<<<<<<<<
        singlelinecheck = false,
        justification=raggedright, 
        margin = {-3ex, 0mm}, % make margin font size dependent
}

(The left margin depends on the font size.)

(2) Set the width of the subfigures equal to the width of the inner figure.

You can choose any horizontal separation. Subfigures and the main caption will be centered horizontally on the page.

a

\documentclass[12pt]{report}

\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}

\captionsetup[subfigure]{% changed <<<<<<<<<<<<<<<<
        singlelinecheck = false,
        justification=raggedright, 
        margin = {-3ex, 0mm}, % make margin font size dependent
}

\usepackage{showframe}% ONLY to show the margins <<<<<<<<<

\begin{document}

\begin{figure}[tbp]
\centering  
\begin{subfigure}[t]{5cm}% changed <<<<<
    \subcaption{}
    \includegraphics[height=3cm, width=5cm]{example-image-a}
\end{subfigure} 
\hspace*{70pt}% set figure separation
\begin{subfigure}[t]{2.5cm}% changed <<<<<
    \subcaption{}
    \includegraphics[height=3cm, width=2.5cm]{example-image-b}
    \end{subfigure}
\caption{Figure caption.}
\end{figure}

\end{document}