1

I would like to use \ref and \label for two lines in a table, which can be done using \phantomsection:

\documentclass{article}
\usepackage{hyperref}
\usepackage{lipsum}
\begin{document}

Cross-references to line \ref{line:x} and line
\ref{line:y} and \ref{line:z}.

\makeatletter
\begin{tabular}{ll}
  \phantomsection\edef\@currentlabel{$x$}\label{line:x} Line $x$ & $\frac{1}{2}$ \\
  \phantomsection\edef\@currentlabel{$y$}\label{line:y} Line $y$ & $\frac{3}{4}$ \\
\end{tabular}

\phantomsection\edef\@currentlabel{$z$}\label{line:z}Line $z$. This one works.

\lipsum

\end{document}

The problem is that when clicking on the $x$ and $y$ framed in red at the top, the PDF reader is sent to the baseline of the targetted line, while the \phantomsection at the beginning of a paragraph works:

Only the bottom of the lines $x$ and $y$ are visible after clicking the links, although for $z$ the reader is sent to the top of the line

It is clearly visible that the PDF reader is sent at the vertical position of the fraction bars, and not at the top of the line.

How can I get the hyperref anchor at the top of the line, and not on the baseline?

Below is a screenshot of the whole document:

Screenshot, with links $x$ and $y$ each framed in red at the top, and the table with two lines below, followed by the $z$ line in a paragraph outside the table

I could hack and put the \phantomsection on the preceeding line, but that won't take into account deep lines (with lots of stuff below the baseline).

I tried messing around with \begin{minipage}[t] or with negative \vspace but still can't get rid of that misalignment.

Suzanne Soy
  • 3,043

2 Answers2

2

I do not understand why you use \phantom section. For just referencing lines I would be use a new counter and \refstepcounter macro:

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

    \newcounter{linenumber}
    \newcommand{\linelabel}[1]{\refstepcounter{linenumber}\label{#1}}

\begin{document}
Cross-references to line \ref{line:x} and line
\ref{line:y} and \ref{line:z}.

\begin{tabular}{ll}
  \linelabel{line:x} Line $x$ & $\frac{1}{2}$ \\
  \linelabel{line:y} Line $y$ & $\frac{3}{4}$ \\
\end{tabular}

\linelabel{line:z}Line $z$. This one works.

\lipsum[1]
\end{document}

Edit: Just for curiosity, does this meet your expectation:

\newcounter{linenumber}
\newcommand{\linelabel}[1]{\raisebox{1em}%
       \refstepcounter{linenumber}\label{#1}}}

I can't test this because I dont know your final macro. My pdf reader point me above labeled line.

Zarko
  • 296,517
  • I used \phantomsection just as a shorthand to avoid using a dummy counter and have a shorter MWE. Your code exhibits the exact same problem as mine, when compiled using pdflatex, namely the \ref{line:x} points to the baseline of Line $x$, not to the top of the line. – Suzanne Soy May 19 '15 at 18:25
  • I do not understand you. This referencing is realy usesles, because it dosn't mar a line, vhere the line is. To my opinion, you should first decide how you will mark lines, which are labeled. – Zarko May 19 '15 at 18:34
  • This is just a MWE showing the problem I have. What I am doing is using a \customlabel{the-label}{the-name} macro I wrote to be able to use \ref{the-label} and have it show the-name, for referencing small parts of a figure or table (on line $a$, we see that… on line $b$, blah blah), but with the safety net that if I rename/renumber/reorder my labels in the table or figure, they will automatically be changed in the paragraphs containing references to them. – Suzanne Soy May 19 '15 at 18:41
  • Now I even less understand your problem. As far I see, my proposition of labeling doesn't influence on formatting of table content or on text. – Zarko May 19 '15 at 18:47
  • I have found a solution to my problem, which could be rephrased as putting a hyperref anchor inside a table, and have that anchor be above the line, instead of on the baseline (otherwise clicking on links to that anchor bring the PDF reader at the wrong point, see screenshots). Thanks for helping, anyway! – Suzanne Soy May 19 '15 at 18:52
  • You could use a new columntype N which does the line numbering automatically –  Jan 03 '16 at 00:48
  • @ChristianHupfer, good idea. During day I will test this. I have to rethinking, how will work referencing of rows (i.e. equations in it). – Zarko Jan 03 '16 at 01:00
  • @Zarko: The \label must be placed in the line number cell still, due to grouping. –  Jan 03 '16 at 01:04
  • @Zarko: \newcounter{foo}\usepackage{array} \newcolumntype{N}{>{\refstepcounter{foo}\thefoo}r}, for example –  Jan 03 '16 at 01:14
  • @ChristianHupfer, your solution work fine (I check my notes and found mine similar solution), however as I understand question, it is not applicable here. Here the rows should not be numbered, so and hyperref should point the label position correctly (in OP solution it miss for one line). This my solution in proposed in Edit quite well fulfill. – Zarko Jan 03 '16 at 02:01
1

\raisebox helps. Or if pdfTeX is used as TeX compiler, then its feature \vadjust pre can be used to insert something right before the current line:

\begin{tabular}{ll}
  \vadjust pre{\phantomsection}%
  \edef\@currentlabel{$x$}%
  \label{line:x}%
  Line $x$ & $\frac{1}{2}$ \\
  \vadjust pre{\phantomsection}%
  \edef\@currentlabel{$y$}%
  \label{line:y}%
  Line $y$ & $\frac{3}{4}$ \\
\end{tabular}
Heiko Oberdiek
  • 271,626
  • I had just found a solution using \raisebox here, but it aims one line too high when used in a paragraph. Using \leavevmode\vadjust pre{\phantomsection} works well, both inside table, inside paragraph, and at the beginning of a paragraph (thanks to \leavevmode). – Suzanne Soy May 19 '15 at 18:30