Hyperref not jumping to the appropriate location for memoir's subbottom

I'm trying to utilize native Memoir subbottom command. However, I'm unable to make hyperref links jump "to the picture", rather than "to the caption". Only links to (a) and (d) work as desired.

\documentclass[a4paper,14pt]{memoir}
\usepackage{graphicx}
\usepackage{hyperref}
% \usepackage[all]{hypcap}

\newsubfloat{figure} % allows to use "subbottom"

\begin{document}
This link jumps to the figure caption: \ref{fig:1}.
% This one could be fixed with "\usepackage[all]{hypcap}",
% but is it the correct approach?

This link jumps to the picture (the way I like): \ref{sf:1}.

This link jumps to slightly-above-subcaption: \ref{sf:2}.

This link jumps to slightly-above-subcaption: \subcaptionref{sf:3}, same as \ref{sf:3}.

This link jumps to the picture (the way I like): \ref{sf:4}.

This link jumps to slightly-above-subcaption: \ref{sf:5}.

\begin{figure}
\centering
% \hfill   % if uncommented, link to sf:1 jumps to incorrect vertical position too. Why?
\subbottom[Subfigure 1\label{sf:1}]{\includegraphics[width=3cm]{example-image-a}}
\hfill
\subbottom[Subfigure 2\label{sf:2}]{\includegraphics[width=3cm]{example-image-b}}
\hfill
\subbottom[\label{sf:3}Subfigure 3]{\includegraphics[width=3cm]{example-image-c}}
\\
\subbottom[Subfigure 4\label{sf:4}]{\includegraphics[width=3cm]{example-image-b}}
\hfill
\subbottom[\label{sf:5}Subfigure 5]{\includegraphics[width=3cm]{example-image-a}}
\caption{Figure with five subfigures\label{fig:1}}
\end{figure}

\clearpage
This text is here to add more space for scrolling.

\end{document}


Here's the result I see (TexLive 2018, Windows 10):

My example is similar to those provided in the docs (Figure 10.19 in version v3.7g, 2018/04/04). And it looks like the issue is present in the docs pdf itself.

Similar code, utilizing subcaptionbox works OK. And "Where do hyperlinks jump?" is nicely documented in the subcaption package docs. But it looks like using subcaptionbox with Memoir is discouraged - the following warning is generated:

You are using the caption package with the memoir class. To prepare we will now reset all captioning macros and configurations to kernel defaults, and then let the caption package take over. Please remember to use the caption package interfaces in order to configure your captions.

• Am I doing something wrong?

• Is there any other way to make links jump "to the picture", rather than using caption and subcaption packages?

• Submitted an issue to hypcap maintainer: github.com/ho-tex/oberdiek/issues/42 – Leonid Jul 13 '18 at 15:18
• Note: there is nothing wrong in using the caption package and derived packages with memoir. The warning is just there to tell you that when you load the caption package with memoir, you can no longer use memoirs own caption configuration macros, they are ignored as caption has now taken over. – daleif Dec 19 '18 at 9:29

Here is a more proper patch that does the right thing (move the refstepcoounter) into the formatting. It has been tested with \subbottom, \contsubbottom, \subtop and \contsubtop and the anchors seem to do the right thing

[sub optimal version removed]


Edit

This one seems simpler. We add the step counter at the start of the \vtop, this puts the anchor a little higher than the contents, which looks better. And it works with and without hyperref, the earlier version did not. I'll have a look at \subcaption as well.

\usepackage{xpatch}

\makeatletter
% less typing
\newcommand\mem@step@subcounter{%
\refstepcounter{sub\@captype}\@contkeep%
}

\xpatchcmd{\@memsubbody}%
{\refstepcounter{sub\@captype}\@contkeep}% search pattern
{}% replacement
{\typeout{@memsubbody is patched}}%
{\typeout{@memsubbody is NOT patched}}%

\xpatchcmd{\@memcontsubbody}%
{\refstepcounter{sub\@captype}\@contkeep}% pattern
{}% replacement
{\typeout{@memcontsubbody is patched}}%
{\typeout{@memcontsubbody is NOT patched}}%

\xpatchcmd{\@memsubfloat}%
{\vtop\bgroup}% search pattern
{\vbox\bgroup\mem@step@subcounter}% replacement
{\typeout{@memsubfloat patch is ok}}%
{\typeout{@memsubfloat patch is NOT ok}}%

\makeatother


For \subcaption

\xpatchcmd{\subcaption}%
{\refstepcounter{sub\@captype}}% search pattern
{\H@refstepcounter{sub\@captype}}% replacement
{\typeout{subcaption second patch is ok}}%
{\typeout{subcaption second patch is NOT ok}}%


can be used under hypcap, just note that the link name will be wrong.

• Great, thanks! I can confirm that this works for me. But it's a pity that \subcaption still behaves as before, though... – Igor Jan 8 '19 at 0:10
• @Igor haven't gotten to that yet. Note that the above solution actually does not work generally, something strange happens if you remove hyperref, haven't figured that one out yet. – daleif Jan 8 '19 at 5:51
• @Igor I've added a better patch for \subbottom and \subtop – daleif Jan 8 '19 at 12:07
• @dalief Thanks! But \subcaption (from rev5) still behaves strange for me, see (c) here. – Igor Jan 8 '19 at 18:16
• @Igor if subcaption jumps anywhere it would be to the start of the entire figure. I'm guessing the other targets intervene (they are probably global). It might be an idea to simply switch to the caption and subcaption packages, the have much more features than what memoir can offer (and still be maintainable) – daleif Jan 8 '19 at 18:23