# How to obtain specific distance between image and caption in beamer?

I'm trying to set the the caption 1pt below the image in a figure in beamer. If I set \abovecaptionskip to 0pt there is still a gap between the image and the caption. This results from the \baselineskip being larger than the fontsize (default 10pt/12pt). I can achieve a zero distance by using the caption package as in the following MWE:

\documentclass{beamer}
\usepackage[compatibility=false]{caption}
\captionsetup{skip=0pt}
\begin{document}
\begin{frame}
\begin{figure}
\centering
\textcolor{yellow}{\vrule width \linewidth height .5\textheight depth 0pt\relax}
\caption{Caption}
\end{figure}
\end{frame}
\end{document}


Did you notice compatibility=false? That's not good practice! caption doesn't support modifying beamer captions in newer version.

The question is: How to obtain a specific distance between image and caption in beamer without using any unsupported package? In my case the specific distance would be 1pt.

It would be nice to have a beamer only solution without any extra packages.

Another option, not needing calculations. You can kill the interline skip before the caption in the definition of the internal \beamer@makecaption command, which actually typesets the caption. The original definition (in beamerbaselocalstructure.sty) is

\long\def\beamer@makecaption#1#2{%
\def\insertcaptionname{\csname#1name\endcsname}%
\def\insertcaptionnumber{\csname the#1\endcsname}%
\def\insertcaption{#2}%
\nobreak\vskip\abovecaptionskip\nobreak
\sbox\@tempboxa{\usebeamertemplate**{caption}}%
\ifdim \wd\@tempboxa >\hsize
\usebeamertemplate**{caption}\par
\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\nobreak\vskip\belowcaptionskip\nobreak
}


so you can insert \nointerlineskip after the first \nobreak, as in

\documentclass{beamer}

\makeatletter
\long\def\beamer@makecaption#1#2{%
\def\insertcaptionname{\csname#1name\endcsname}%
\def\insertcaptionnumber{\csname the#1\endcsname}%
\def\insertcaption{#2}%
\nobreak\nointerlineskip\vskip\abovecaptionskip\nobreak
\sbox\@tempboxa{\usebeamertemplate**{caption}}%
\ifdim \wd\@tempboxa >\hsize
\usebeamertemplate**{caption}\par
\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\nobreak\vskip\belowcaptionskip\nobreak
}
\makeatother

\begin{document}

\begin{frame}
\setlength\abovecaptionskip{0pt}
\begin{figure}
\rule{3cm}{1cm}
\caption{Test with \texttt{0pt} and some more text so the caption will span more than one line}
\end{figure}
\setlength\abovecaptionskip{1pt}
\begin{figure}
\rule{3cm}{1cm}
\caption{Test with \texttt{1pt} and some more text so the caption will span more than one line}
\end{figure}
\setlength\abovecaptionskip{10pt}
\begin{figure}
\rule{3cm}{1cm}
\caption{Test with \texttt{10pt} and some more text so the caption will span more than one line}
\end{figure}
\end{frame}

\end{document}


or you can patch the command to introduce the change simplifying the code, as in the following example:

\usepackage{etoolbox}
\makeatletter
\patchcmd{\beamer@makecaption}
{\nobreak}
{\nobreak\nointerlineskip}
{}
{}
\makeatother


If you're only including text in the caption, the maximum height should be as tall as \figurename (or \tablename, in the case of a table). As such, capturing \figurename in a box (say \captionsavebox) and measuring its height allows you to adjust \abovecaptionskip to suit your needs exactly:

\documentclass{beamer}

\newsavebox{\captionsavebox}
\savebox{\captionsavebox}{\figurename}

\begin{document}

\begin{frame}
\begin{figure}
\centering
\textcolor{yellow}{\vrule width \linewidth height .5\textheight depth 0pt\relax}
\caption{Caption}
\end{figure}
\end{frame}

\setlength{\abovecaptionskip}{\dimexpr-\baselineskip+\ht\captionsavebox+1pt}

\begin{frame}
\begin{figure}
\centering
\textcolor{yellow}{\vrule width \linewidth height .5\textheight depth 0pt\relax}
\caption{Caption}
\end{figure}
\end{frame}

\end{document}


The \caption setup in beamer very much mimics that of a regular caption. Here's the "make caption" macro (from beamerbaselocalstructure.sty):

\long\def\beamer@makecaption#1#2{%
\def\insertcaptionname{\csname#1name\endcsname}%
\def\insertcaptionnumber{\csname the#1\endcsname}%
\def\insertcaption{#2}%
\nobreak\vskip\abovecaptionskip\nobreak
\sbox\@tempboxa{\usebeamertemplate**{caption}}%
\ifdim \wd\@tempboxa >\hsize
\usebeamertemplate**{caption}\par
\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\nobreak\vskip\belowcaptionskip\nobreak}