2

I am having two documents, call them A and B. I want to make several cross-references from file A to file B and by clicking on the hyperlink, I want to open file B at the respective page.

In my preamble of file A, I created a short command to make the hyperlink and the reference (separately):

\usepackage{xr-hyper}
\usepackage{hyperref}
\externaldocument[B-]{B}
\newcommand{\test}[2][Seite]{\href{file:B\#page.#1}{also see in File B S.\pageref{#2}}}

As an example, in the document, I have...

\begin{document}
\test[11]{B-sec:aSectionInFileB}
\end{document}

... which works properly. When I click on the link, file B opens at page 11 as specified in the brackets [].

But the thing is, I don't want to specify the page number manually for every reference, but rather "take it from the \pageref command" so that any changes made to document B do not require me to change the page numbers manually. I already tried out to put \pageref{} into brackets [], but apparently this won't work. I found a solution here, but I think it is even less convenient than doing it manually.

Is there any simple automatic way of doing this?

  • \pageref isn't expandable, that's the main cause, in my point of view – user31729 Dec 21 '15 at 12:51
4

Something like this perhaps?

Using \getpagerefnumber{foo} (package refcount) will provide the page number where the foo label has been placed. A better approach (in my point of view) for the page.3 syntax is to use page=3 etc. as key-value for the optional argument of the \testme command.

I think, it should work as named destination too!

\documentclass{article}


\usepackage{refcount}
\usepackage{xr-hyper}
\externaldocument[B-]{B}
\usepackage{hyperref}

\newcommand{\testme}[2][B]{%
  \href[pdfnewwindow=true,page=\getpagerefnumber{#2}]{file:#1}{also see in File #1 S.\getpagerefnumber{#2}}%
}

\begin{document}
\testme{B-sec:aSectionInFileB}
\end{document}

B.tex:

\documentclass{article}

\usepackage{blindtext}
\begin{document}
\blindtext[10]
\section{A section}\label{sec:aSectionInFileB}
\blindtext[10]

\end{document}
  • excellent, thank you!! btw, do you know how I run file B without closing A? – Nic Dec 21 '15 at 13:35
  • 1
    @Nic: See my edit: Add pdfnewwindow=true to the \href options – user31729 Dec 21 '15 at 13:38

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.