# hyperlink pointing options for multiline equations with centered labels

I have a split equation as in the MWE below. However, when I click on the corresponding hyperlink, it directs/ points me to the text line right before the first equation line. I would like it to point to the first equation line. Is that possible? If, how? Thanks a lot in advance!

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

\begin{document}

$$\begin{split} A &= B\\ C &= D\\ E &= F\\ G &= H = I = \\ & \quad = J = K \end{split} \label{eq:1}$$

\lipsum

Insertion of~\eqref{eq:1}

\end{document}
• This is strange. For me, the reference points to the line that contains the equation number. – frougon May 10 at 8:19
• could be. I have my multiline equations inserted in a wider document with a lot of packages and options. Still, in that document all hyperlinks associated to single line equations work fine, i.e. they point to the corresponding labels, which in the case of single line equations are always the first equation line of course. Still, the basic question remains even in the simple MWE above: How to make an hyperlink point to the first line of a multiline equation, while keeping the label vertically centered? – Euclides May 10 at 8:28
• a lot of packages includes cleverref, so it could be that there is some kind of conflict as in this discussion: tex.stackexchange.com/questions/210480/…. In the abovementioned wider document I also have multiline equations within align environments, with each equation containing a label in that case, and the hyperlinks also point correctly to the label. For some reason, the label for the split environment is giving trouble. Is there any other environment that centers labels for multiline equations? – Euclides May 10 at 8:47
• If there was an alternative to the split environment for having centered labels in multiline equations, I should probably try it, and first make sure the hyperlink points to the label, before finally solving the original question, which is to move the pointer to the first equation line.............. – Euclides May 10 at 8:51
• Your question gets with every comment more unclear. Does your example show the problem you have or not? And where do you want the link to point to? – Ulrike Fischer May 10 at 8:53

Here is one way, inspired by this answer for the starting point, to make equation references that point to:

• the first equation line in an equation environment;

• the current line in an align or gather environment (other environments can be easily added, see below).

Note that due to the different times at which the equation counter is incremented depending on the amsmath environment used, my code has to distinguish between two cases. However, by detection of the current environment, it offers you an “automatic mode” where normally, you as a user don't have to distinguish between the two cases. So, here are the two options:

1. Automatic choice of the custom labelling command: always use \myeqlabel to create an equation label;

2. Manual choice of the custom labelling command:

• use \myeqlabelnoauto* inside environments such as align and gather for which the correct value of the equation counter for a given line is obtained after the \\ command that ends a line (i.e., the counter is one unit too small at the point where \myeqlabelnoauto* has to be called in order for the reference to point to the line we want);
• use \myeqlabelnoauto when the equation counter is already correct at the point where \myeqlabelnoauto is called (normally, all other cases).

Automatic mode (via the \myeqlabel command) knows the align and gather environments as requiring “special handling” (corresponding to what \myeqlabelnoauto* does). If you discover other environments that need the same special handling (which would manifest as off-by-one reference numbers), add them to the following line of my code:

\seq_gset_from_clist:Nn \g_euclides_special_envs_seq { align, gather }

Here is the code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\usepackage{zref}
\usepackage{hyperref}

\ExplSyntaxOn

\seq_new:N \g_euclides_special_envs_seq
\seq_gset_from_clist:Nn \g_euclides_special_envs_seq { align, gather }

\prg_new_protected_conditional:Npnn \euclides_if_special_env:n #1
{ T, F, TF }
{
\seq_if_in:NnTF \g_euclides_special_envs_seq {#1}
{ \prg_return_true: }
{ \prg_return_false: }
}

\prg_generate_conditional_variant:Nnn \euclides_if_special_env:n { V }
{ T, F, TF }

\NewDocumentCommand \ifeuclspecialenv { m m m }
{
\euclides_if_special_env:VTF {#1} {#2} {#3}
}

\ExplSyntaxOff

\makeatletter

\zref@newlist{mylist}
\zref@newprop{equation}[??]{0}

\newcommand*{\my@set@zref@equation@prop}[1]{%
\zref@setcurrent{equation}{#1}%
}

% Manual labelling command: depending on the enclosing environment, one has to
% use the normal or the star form.
\newcommand*{\myeqlabelnoauto}{%
\@ifstar{\@myeqlabelnoauto{+1}}{\@myeqlabelnoauto{}}%
}

\newcommand*{\@myeqlabelnoauto}[2]{%
\begingroup
\edef\my@tmp@eqnum{\number\numexpr \value{equation} #1 \relax}%
\expandafter
\endgroup
\expandafter\my@set@zref@equation@prop\expandafter{\my@tmp@eqnum}%
\phantomsection\label{zref:#2}%
\zref@labelbylist{#2}{mylist}%
}

% Automatic detection of the appropriate labelling command: the code will use
% either \myeqlabelnoauto or \myeqlabelnoauto* depending on the enclosing
% environment.
\newcommand*{\myeqlabel}{%
\ifeuclspecialenv{\@currenvir}{\myeqlabelnoauto*}{\myeqlabelnoauto}%
}

% Referencing command that goes with the above labelling commands.
\newcommand*{\myeqref}[1]{%
\hyperref[zref:#1]{\textup{\tagform@{\zref@extract{#1}{equation}}}}%
\zref@refused{#1}%
}

\makeatother

\begin{document}

The \verb|equation| environment increments the \verb|equation| counter at the
beginning of the environment; use either \verb|\myeqlabel| or
\verb|\myeqlabelnoauto| there:
$$\begin{split} \myeqlabel{some-eq} A &= B\\ C &= D = E = \\ & \quad = F = G \end{split}$$

The \verb|align| environment increments the \verb|equation| counter
\emph{after} the \verb|\\| that ends a line; use either \verb|\myeqlabel| or
\verb|\myeqlabelnoauto*|:
%
\begin{align}
\myeqlabel{other-eq-a} a &= b + c+ d + e   & u - v &= f + f'\\
\myeqlabel{other-eq-b}   &= g                  & z &= h\\
\myeqlabel{other-eq-c}   &= i                  & t &= j
\end{align}

The \verb|gather| environment is similar to \verb|align|; use either
\verb|\myeqlabel| or \verb|\myeqlabelnoauto*|:
\begin{gather}
\myeqlabel{gather-eq-a} a_1 = b_1 + c_1\\
\myeqlabel{gather-eq-b} a_2 = b_2 + c_2 - d_2 + e_2
\end{gather}

Pointers to equations \myeqref{some-eq}, \myeqref{other-eq-a},
\myeqref{other-eq-b}, \myeqref{other-eq-c}, \myeqref{gather-eq-a}
and \myeqref{gather-eq-b}.
\end{document}

• Thanks a lot frougon! It does work! However, I was hoping to avoid such ad hoc solutions and to have way more synthetic code. Is there no simpler alternative? – Euclides May 10 at 9:16
• It's the only way I know... and not difficult to use. Once the commands are defined, you just need to call them instead of \label and \eqref. – frougon May 10 at 9:19
• I have another related problem! I actually would need the hyperlink to point to the first line of a split equation inside an align, not an equation environment. Would it be possible to adjust the solution you proposed earlier? – Euclides May 28 at 16:13
• Dear @Euclides, two persons took the care to answer your question; you upvoted none of the answers, didn't mark any as accepted either (you know, this is done by clicking on the “tick” mark left of your preferred answer), and now you come back to us eighteen days later and ask a new question in the comments. We are not your slaves. :-) – frougon May 28 at 19:14
• sorry about the upvoting. I thought some external entity had the authority to do that. Already upvoted. As for the 18 days time interval, I'm sorry. It's just that I changed all equation environments in my document to align environments as these latter offer some (alignment, what else? :-D) advantages, and I'm not sure about how to adapt your solution to the "new circumstances". I could probably get there on my own, but I have currently absolutely no time to do research on .tex, so I would be very grateful if I could get any help on this issue. – Euclides May 29 at 13:28

You can locally or generally change the amount the link is raised by hyperref:

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

\begin{document}
Some preliminary text...
%also possible, default is 1 \baselineskip.
$$\begin{split} A &= B \\ C &= D = E = \\ & \quad = F = G \end{split} \label{eq:1}$$