# Hyperref with align inside subequations

\documentclass{article}
\usepackage{amsmath}
\usepackage{showframe}
\usepackage{hyperref}
\usepackage{cleveref}

\begin{document}
Some text before the subequations.
\clearpage
\begin{subequations}
\label{eq:arithmetic}
\begin{align}
a + b &= c \\
a + b &= c \\
a + b &= c \\
a + b &= c
\end{align}
\end{subequations}
The reference to the main equation with \cref{eq:arithmetic} brings me to a line before the set of subequations.
\end{document}


Clicking the hyperlink gets me to a line before the subequations. How can I get the link to bring me to the first line of the subequations (eq 1a) instead? I want the cref to still read as eq. (1) since I am referring to the entire set of subequations.

EDIT: It should also work when the subequations appear at the start of the page.

The problem with the subequations environment is that it is not a math environment. The subequations environment merely changes the behavior of the counter of numbered equations.

As a result, when a hyperlink anchor is created for the main subequations, its position is actually on the baseline of the previous last line of text. To see this, uncomment the line \setlength\HyperRaiseLinkLength{0pt} in the solution below.

So, if there is a page break before the subequations environment, the hyperlink anchor has nowhere to go but the upper-left corner of the text body in the new page. There isn’t much hyperref can do in this case. We could rewrite the text:

1. Make it longer so one or two lines follow onto the next page;
2. Make it shorter and use \allowdisplaybreaks so the first line of subequations fits in the previous page.

In any case, avoid starting a new page with equations.

With the unpleasant “new page” situation out of the way, I provide a new implementation of shifting the hyperlink anchor. Now, the hyperlinks for eqs. (1) and (1a) point to the exact same spot!

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{cleveref}

% Let's patch \HyOrg@subequations from hyperref
\usepackage{etoolbox}
\makeatletter
% Change \HyperRaiseLinkLength for the main anchor in subequations
\preto\HyOrg@subequations{%
% Drop the main anchor from the baseline of previous text by \abovedisplayskip
% Then, raise the main anchor by 3pt
% (The 3pt is to cancel the -\lineskip from \displ@y, see amsldoc.tex)
% To see the default position of the main anchor, uncomment the following line:
}%
}
% Change nothing for the other anchors in subequations
\appto\HyOrg@subequations{%
}
\makeatother

\begin{document}
Some text before the subequations.
\begin{subequations}\label{eq:arithmetic}
\begin{align}
a + b &= c \label{eq:arithmetic-a} \\
a + b &= c \label{eq:arithmetic-b} \\
a + b &= c \nonumber \\
a + b &= c \label{eq:arithmetic-c}
\end{align}
\end{subequations}
Testing:
\Cref{eq:arithmetic,eq:arithmetic-a} bring me to the \emph{exact} same place,
while \cref{eq:arithmetic-b,eq:arithmetic-c} bring me to the correct subequations.
$$e^{i\pi} = -1 \label{eq:euler}$$
Testing:
\Cref{eq:euler} bring me to Euler's identity.
\end{document}


An extremely dirty hack:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{cleveref}

% Since you have loaded cleveref, we can patch \cref@old@subequations directly
\usepackage{etoolbox}
\makeatletter
% Vertical space No. 1:
\preto\cref@old@subequations{\vskip\abovedisplayskip\nobreak}
% Vertical space No. 2:
\appto\cref@old@subequations{\vskip-\abovedisplayskip\vskip-\baselineskip\nobreak}
\makeatother

\begin{document}
Some text before the subequations.
\begin{subequations}\label{eq:arithmetic}
\begin{align}
a + b &= c \\
a + b &= c \\
a + b &= c \\
a + b &= c
\end{align}
\end{subequations}
The reference to the main equation with \cref{eq:arithmetic} brings me to a line before the set of subequations.
\end{document}

• Hi, I tried your solution. Unfortunately, it doesn't really work well if the set of subequations appear at the start of the page. I'll edit the question; (if I insert your solution in the new MWE, the hyperlink still brings me to a line above (1a).) – ksgj1 Oct 22 '18 at 21:06
• @ksgj1 You are right. However, it is worth pointing out that “starting a page with an equation” is generally considered as bad typography. TeX, by default, tries very hard to avoid your \clearpage situation. When an equation is about to be pushed onto the next page, TeX usually stretches all available \vskip’s (spaces around section headings, paragraph separations, etc.) in the previous page, so it can push the text line just before the equation onto the next page as well (so the \clearpage situation never occurs naturally). IMHO, the deliberate \clearpage seems very artificial. – Ruixi Zhang Oct 22 '18 at 21:47
• Yes, it's artificial -- I wanted to make the example minimal and the \clearpage was the fastest way to prove my point. In my actual document, the new page does happen "naturally", but you're right in that there's bad spacings before that. I'll rewrite that entire section so it'll probably not be an issue later, but I just wanted to feedback to you anyway. – ksgj1 Oct 22 '18 at 23:45
• @ksgj1 Perhaps a patch in a different spot would help. I’ll see what I can do. On the other hand, I do feel that this should be a feature implemented by hyperref. – Ruixi Zhang Oct 23 '18 at 0:22
• @ksgj1 You are very welcome! For a reference, I have opened an issue on the hyperref GitHub. Let’s hope this will be implemented into the next version of hyperref. – Ruixi Zhang Oct 23 '18 at 21:17

You can move the actual mark down by 2 baseline skips (1 to counter the default raise and another to bring it to the first equation) and then force a new target using \phantomsection:

\documentclass{article}

\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{cleveref}

\begin{document}

Some text before the subequations.
\begin{subequations}
\raisebox{-2\baselineskip}[0pt][0pt]{%
\phantomsection % Set new hyper target
\label{eq:arithmetic} % Mark label
}
\begin{align}
a + b &= c \\
a + b &= c \\
a + b &= c \\
a + b &= c
\end{align}
\end{subequations}
The reference to the main equation with \cref{eq:arithmetic} brings me to a line before the set of subequations.

\end{document}


We use \raisebox with a 0pt resulting depth and 0pt height (similar to \smash).