9

Suppose you have a tex file (lets call it B) with some labels, and another one file (call it A) , xr package will allow us to make hyperref from A->B , but I previously found couple cases (I don't know if that done by LaTeX) in which clicking on the link that in file A , will automatically open the file B (if they was in the same directory) and jump to the particular label in that file B, is that possible to be done somehow in LaTeX ?

P.S-1 Currently if we do that external ref. clicking on the link will only jump to the begening of the current file (I mean file A) and will not open anything (unless I'm doing something wrong)

P.S-2 In other question I get this answer:

There is a recent post on the LyX users mailing list about this. It has no answer yet, but you could watch it and see if anything pops up. mail-archive.com/lyx-users@lists.lyx.org/msg93271.html – Torbjørn T.

But that corespondance is very old (back to 2010) , are there is any difference?

Speravir
  • 19,491
TMS
  • 2,193

1 Answers1

11

Yes, this is certainly possible using the hypertarget and href mechanism provided by the hyperref package.

Let's say that fileA.tex has

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

\lipsum

\hypertarget{fileAhypertarget}{Should come to this}

\end{document}

Then you can write fileB.tex as

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

\href{fileA.pdf#fileAhypertarget}{Let's go to file A!}

\end{document}

Note that not all pdf viewers support this- for example evince didn't give the correct behaviour, but acroread did.

Update following the comments

The lipsum package is used to generate sample text- you don't need it for most of your documents, but you'll see it used a lot here on tex exchange just to demonstrate MWEs.

You can automate the hypertarget mechanism for each section in lots of ways- here's one way using the titlesec package to help; note that I've commented out the showlabels package, but you might like to use it during debugging, it's really helpful!

fileA.tex

\documentclass{article}
\usepackage{lipsum}         % for sample text
\usepackage{titlesec}       % to change headings

% very useful during debugging!
%\usepackage[left]{showlabels}
%\showlabels{hypertarget}

% usually load the hyperref package last, 
% see this for reference http://tex.stackexchange.com/questions/1863/which-packages-should-be-loaded-after-hyperref-instead-of-before
\usepackage{hyperref}

% renew \section to set a hypertarget
\titleformat{\section}
{\normalfont\Large\bfseries}
{\thesection}
{1pc}
{\hypertarget{myhypertarget\thesection}}


\begin{document}

\lipsum[1]

\section{First section}
\lipsum

\section{Second section}
\lipsum

\section{Third section}
\lipsum

\end{document}

fileB.tex

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

\href{fileA.pdf#myhypertarget1}{Let's go to Section 1!}

\href{fileA.pdf#myhypertarget2}{Let's go to Section 2!}

\href{fileA.pdf#myhypertarget3}{Let's go to Section 3!}

\end{document}
cmhughes
  • 100,947
  • 1
    Both PDF files must be in the same directory, that’s the only restriction. Otherwise a full path has to be given, and that’s not portable at all. Oh … and I was sure, this was asked already, but can't find the question. (Nonetheless upvoted your answer.) – Speravir Sep 12 '12 at 19:19
  • @Speravir thanks for the feedback/observations. If you find the duplicate, post it as a comment to the question :) – cmhughes Sep 12 '12 at 19:47
  • Ohh, I thought this hadn't still been implemented yet! However, you might mention that not all PDF viewers support this. For instance, evince (3.2.1) opens only the first page of 'fileA.pdf', but acroread (9.5.1) will open the correct page. – jon Sep 12 '12 at 22:27
  • @jon yes, good point. I noticed that evince didn't support it but didn't think to mention it- will update immediately :) – cmhughes Sep 12 '12 at 22:43
  • Worked great, thx! but I have three small issues : 1- I noticed that adding hypertarget to some section for which I already added label generates error! why? Is that means that now I can use only them and no more file-internal cross reference with hyperref? 2- Is there any particular reason for \lipsum you using above? because it worked for me without it 3-Is there anyway to automate adding this hypertarget for every section i made? – TMS Sep 13 '12 at 06:08
  • @TMS see my update :) – cmhughes Sep 13 '12 at 15:37
  • 1
    @Speravir, is it really true that you can't use a relative path like \href{../sibling/fileA.pdf#myhypertarget1}{Let's go to Section 1!}? I can't test, because, when I try this (in Preview.app on Mac OS), I get the message "The file "fileA.pdf" couldn't be opened because you don't have permission to view it." (even though I do). Any idea what's going wrong? – LSpice Sep 01 '13 at 19:09