5

I've asked a similar question but I did not realize that the question and solution relied on the caption being positioned at the bottom.

Consider the following example:

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

\begin{document} \begin{figure} \centering % from caption documentation: % Please note that position=top does NOT mean that the caption is actually placed at the top of the figure or table % Instead the caption is usually placed where you place the \caption command. \captionsetup[subfigure]{singlelinecheck=false,skip=-6pt,position=top} \begin{subfigure}{0.4\textwidth} \caption{} \includegraphics[width=1.0\textwidth]{example-image-a} \label{fig:a} \end{subfigure} \begin{subfigure}{0.4\textwidth} \caption{} \includegraphics[width=1.0\textwidth]{example-image-b} \label{fig:b} \end{subfigure} \end{figure}

\end{document}

The subcaption package mentions options that are described in further detail in the caption documentation. In the source, \caption{} should appear before \includegraphics{... to affect the ordering. Unfortunately, this has an unintended influence that the label is rendered below the figure, which means that the figure occludes it. In the example, I include a skip that partially occludes the labels:

enter image description here

How can I get captions positioned on the top, but within the figure? I am beginning to understand why these answers are so much more involved for captions placed within the top and not the bottom of the figure.

Gus
  • 1,217
  • \caption begins and ends with a \par. [skip=-6pt] is the culprit. Lose the \captionsetulp, or at least learn how to use it correctly. – John Kormylo Nov 04 '21 at 14:07
  • @JohnKormylo perhaps you misunderstood the question. I want the label to be set within the figure. – Gus Nov 05 '21 at 16:15

1 Answers1

4

The foreground must always be drawn after the background, which means raising the caption into position. (If you were wondering, TikZ does layers in order automatically.)

\caption must go inside a minipage, \parbox or \vbox, and \setbox0=\vbox{} is the simplest way to put one inside a \raisebox. The \vbox gets its width from the current environment.

You can tweak the \raisebox to change the location.

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

\begin{document} \begin{figure} \centering % from caption documentation: % Please note that position=top does NOT mean that the caption is actually placed at the top of the figure or table % Instead the caption is usually placed where you place the \caption command. \captionsetup[subfigure]{singlelinecheck=false,skip=0pt}% \begin{subfigure}{0.4\textwidth} \setbox0=\vbox{\caption{}\label{fig:a}}% label uses local values \sbox1{\includegraphics[width=1.0\textwidth]{example-image-a}}% measure height \leavevmode\rlap{\usebox1}% use horizontal overlap \raisebox{\dimexpr \ht1-\ht0}{\usebox0} \end{subfigure}\hfil \begin{subfigure}{0.4\textwidth} \setbox0=\vbox{\caption{}\label{fig:b}}% label uses local values \sbox1{\includegraphics[width=1.0\textwidth]{example-image-b}}% measure height \leavevmode\rlap{\usebox1}% use horizontal overlap \raisebox{\dimexpr \ht1-\ht0}{\usebox0} \end{subfigure} \end{figure}

\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120