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/[email protected]/msg93271.html – Torbjørn T.

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

1 Answer 1

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}
8
  • 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, 2012 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, 2012 at 19:47
  • I now think, I mixed it up with Using \hyperlink to link text to a page (by page number) whose number is not known in advance or a related question.
    – Speravir
    Sep 12, 2012 at 21:20
  • 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, 2012 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, 2012 at 22:43

You must log in to answer this question.

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