[Tex/LaTex] Hyperref to external pdf that will open at particular label

external fileshyperref

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?

Best Answer

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}
Related Question