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.
\@hyper@readexternallink ...#7\\{#3}{#2}{#7}\else 
                                      \ifx \@pdftempa \@pdftempw...
l.10 ...:mylinelabel}{line \ref*{lin:mylinelabel}}

Here is source.tex.



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}}.

Here is dest.tex.


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}}.

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> 
<*> 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


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.