# hyperref causes a gap between the figure and subcaption with memoir

I'm experiencing a strange problem with memoir subfigure captions together with hyperref. Below is a working example:

\documentclass{memoir}

\usepackage{hyperref}
\newsubfloat{figure}

\begin{document}
\begin{figure}[tb]
\rule{\textwidth}{4ex}
\subcaption{}
\end{figure}
\end{document}


This produces the following result:

If I remove the hyperref package and recompile, the gap disappears:

Anyone have any idea why this is happening, and how to solve it?

-

The solution is to place % behind the \rule line:

\documentclass{memoir}

\usepackage{hyperref}
\newsubfloat{figure}

\begin{document}
\begin{figure}[tb]
\rule{\textwidth}{4ex}%
\subcaption{}
\end{figure}
\end{document}


I guess that \subcaption uses \unskip to remove the space cause by the line break, but hyperref adds some internal code which stops \unskip to go that far back. Because the \rule is \textwidth long the space is creating a new line if not removed. Alternatively add a manual \unskip before \subcaption if you have a more complicated content than \rule.

In general it is a good idea to place a % after } and { at the end of a source line. Such spurious spaces are appearing often if you are not careful.

-
In my document where this problem occurs (it's only in one subfigure - in all other subfigures there is no gap), I had to use both \unskip and place a % after the figure code. Anyhow, the gap is now gone. Thanks. –  gablin Oct 18 '11 at 13:42