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.

2 Answers 2

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}
1
  • 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). May 19, 2015 at 18:30
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.

10
  • 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. May 19, 2015 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, 2015 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. May 19, 2015 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, 2015 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! May 19, 2015 at 18:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .