# reference to a label linked with a longtable row

Is there a way to link to a specific row in a Longtabl environment. This is usefull when using the hyperref package and allows the viewer to simply jump from somewhere in the document to the right row of the table. At the moment one jumps always to the top of the column?

For instance:

\begin{longtable}
\label{r1}cell1a&cell1b&cell1c&cell1d&cell1e\\
\label{r2}cell2a&cell2b&cell2c&cell2d&cell2e\\
\label{r3}cell3a&cell3b&cell3c&cell3d&cell3e\\
\label{r4}cell4a&cell4b&cell4c&cell4d&cell4e\\
\end{longtable}

As one can see in \hyperref[r3]{row 3}...

-
Not a possible duplicate, in my view: the older question asks about auto-numbering lines in a table, whereas the new one (I believe) is asking about providing a hyperlink to a specific row of a table. – Mico May 8 '12 at 14:02
@Mico, yes, the wording is not the same, but I am guessing that the OP does not have a label referencing a certain row, I am guessing that the jump to the top of the table is due to \label referencing the \caption of the float and not the row. Otherwise the link would have been correct, as in the linked question. OP, please correct me if I am wrong. I have also asked the OP in the linked question to make the title more general. – zeroth May 8 '12 at 14:08
I hope my edit solves the problem? – Willem Van Onsem May 8 '12 at 14:31
zeroth -- I guess the two issues that distinguish the new question are (i) the explicit requirement to use the hyperref package and (ii) no apparent need for auto-numbering (and auto-labeling) all rows of the given tabular (or longtable) environment. The code given in your answer to the older question will work for the new question, provided one adds a \phantomsection command before each automatically generated \label; otherwise, hyperref will "anchor" all auto-generated label to the top of the environment in question. Why don't you post that as a separate answer to the new question? – Mico May 8 '12 at 14:35
@CommuSoft: if you add a \phantomsection command in front of each \label command in your code, things should work fine. If you need to have links only to certain rows of the table, take a look at my answer for a possible solution. – Mico May 8 '12 at 14:37

Have a look at the macros \hypertarget and \hyperlink of the hyperref package.

One can create an internal link (almost) anywhere in the document -- including some row in a tabular environment -- with the instruction

\hypertarget{<name>}{<text>}


where <name> should be a unique "label" that will be used by hyperref to create the "target" and <text> should be whatever should be placed there. E.g., in a tabular environment, <text> could be the content of the first cell of a certain row. You can then link to that "label" elsewhere in the document with the command

\hyperlink{<name>}{<text>}


where <name> should be the label you chose in the \hypertarget command and <text> can be anything that passes TeX's syntax rules; e.g., it could be the string "there".

-

If you always wish to refer to explicit rows in the text (by text) it seams obvious that you will not change your table often. Therefore you can automate the referencing using Mico's suggestions.

I have created a new table environment which automatically creates labels for the tables according to a user defined prefix, furthermore there is a correction to the anchor placement to enable the correct linking:

\begin{refltable}{<table format>}{<ref prefix>}


This can be used in the following way:

\documentclass{article}
\usepackage{hyperref,array,longtable}

\newcounter{tablerowno}
% New environment for the longtable, this will set it up correctly
\makeatletter
\newenvironment{refltable}[2]{%
\setcounter{tablerowno}{0}
\begin{longtable}{>{\stepcounter{tablerowno}\raisebox{\f@size pt}{\phantomsection}\label{#2\thetablerowno}}#1}}%
{%
\end{longtable}%
}
\makeatother
% In order for the link to pop-up the correct row we have to raise
% the anchor of the link, this is done using the size of the font (\f@size).
% This will also capture \Huge, \LARGE, etc.

\begin{document}

\begin{refltable}{cl}{r}
Content of row 1 \\
Content of row 2 \\  Content of row 3 \\
Content of row 4 \\  Content of row 5 \\
Content of row 6 \\  Content of row 7 \\
Content of row 8 \\  Content of row 9 \\
Content of row 10\\
Content of row 11\\
Content of row 12
\end{refltable}
As one can see in \hyperref[r10]{row 10} and \hyperref[r1]{row 1}.

\begin{refltable}{cl}{secr}
Content of row 1 \\
Content of row 2 \\  Content of row 3 \\
Content of row 4 \\  Content of row 5 \\
Content of row 6 \\  Content of row 7 \\
Content of row 8 \\  Content of row 9 \\
Content of row 10\\
Content of row 11\\
Content of row 12
\end{refltable}
As one can see in \hyperref[secr10]{row 10} and \hyperref[secr1]{row 1} together with \hyperref[r1]{row 1} of the first table.

\end{document}


However, you might be better off by using the manual way by Mico as it would ensure that you dont accidentally reference a wrong row in the table.

-
Very nice! A completely separate issue: I've noticed that both your solution (which uses LaTeX labels and explicit \phantomsection commands) and my method (which uses \hypertarget and \hyperlink directives) tend to produce the following odd/unexpected behavior, in both tabular and longtable environments: When one clicks on the hyperlink, the pdf reader doesn't "jump" to the row being cross-referenced but to the subsequent row. I wonder if this might be a bug in hyperref. – Mico May 8 '12 at 15:56
@Mico I have seen this several times. It happens all the time when one uses \phantomsection. I also see it when I manually add sections for bibliography etc. It appears that the phantom section has difficulties in placing the anchor correct. However, I have never considered it a bug, it might be one?!? :) – zeroth May 8 '12 at 17:26
@Mico it certainly is due to the placement of the anchor being located on the baseline of the line. I realised this last night right before I fell asleep! Try my updated version of the code which raises the phantomsection to the current font height! :) – zeroth May 9 '12 at 6:11