# external cross-reference to a line number using lineno and xr-hyper

I try to reference a line number in source.tex from a dest.tex document. When (PdfLaTeX-)compiling dest, I get the following error.

! Extra \else.
\ifx \@pdftempa \@pdftempw...
l.10 ...:mylinelabel}{line \ref*{lin:mylinelabel}}
.


Here is source.tex.

\documentclass{article}
\usepackage{lineno}
\usepackage{hyperref}
\newcommand{\llabel}[1]{\hypertarget{lintarget:#1}{}\linelabel{lin:#1}}

\begin{document}
\linenumbers

The label is \llabel{mylinelabel} here.

Reference to line: \ref*{lin:mylinelabel}.

Reference with ref as argument: \href{file:source.pdf\#lintarget:mylinelabel}{line \ref*{lin:mylinelabel}}.
\end{document}


Here is dest.tex.

\documentclass{article}
\usepackage{xr-hyper}
\usepackage{hyperref}
\externaldocument{source}
\newcommand{\lref}[1]{}

\begin{document}
Reference to line in source: \href{file:source.pdf\#lintarget:mylinelabel}{line} \ref*{lin:mylinelabel}.

Reference to line in source with ref as argument: \href{file:source.pdf\#lintarget:mylinelabel}{line \ref*{lin:mylinelabel}}.
\end{document}


The second reference in dest.tex triggers the error. The only difference is that the ref* is inside the href argument. The error seems to stem from some subtle interaction between lineno, xr-hyper, ref* and href: including ref* command in an href argument normally does not cause problem, as seen in source.tex which compiles just fine.

Note that I must use ref* instead of ref, otherwise when compiling dest, I get the following error.

Runaway argument?
{\@firstoffive }\fi . \end {document}
! File ended while scanning use of \Hy@setref@link.
<inserted text>
\par
<*> dest.tex


(I got inspired by this post.)

The problem is that lineno does not add a hyperref anchor to the label. This causes xr-hyper to assume that hyperref is not used. This leads to problems, if the hyperref \ref command tries to use the label. Actually this is also triggered for the \ref* outside of the href: You can see that the period at the end of the line is not printed in the pdf file.

You can fix the xr-hyper problem by adding

\makeatletter
\long\def\myempty{}
\def\XR@@dURL#1#2#3#4#5#6#7\\{%
{#1}{#2}%
\ifx\myempty#6\@empty
{#3}{#4}{\XR@URL}%
\else
\fi
}
\makeatother


before any \externaldocuments in dest.tex. This tells xr-hyper to always add hyperref style references, if the original reference was compatible to hyperref.

• Great! But AFAICS, it messes up \cref, which used to work (not for references to lines, but for references to sections in the external document, for example) – Olivier Cailloux Apr 27 '18 at 12:15
• @OlivierCailloux Fixed it. – Marcel Krüger Apr 27 '18 at 14:22
• Greater! Can this tweak lead to problems? Should I suggest to the hyperref maintainer to modify this in xr-hyper (part of the hyperref bundle)? – Olivier Cailloux Apr 27 '18 at 18:30
• I currently does not see how this could lead to problems, but I haven't tested it much. This would not have to be changed inside xr-hyper, the changed code lies inside hyperref itself. – Marcel Krüger Apr 27 '18 at 22:39