# Cross-referencing page numbers between two documents

I have two documents which I will subsequently merge (i.e. I will merge .pdf’s). In order to do that I have two issues to resolve, both have to do with cross-referencing.

First, I use firstpage and LastPage labels in headings and footing to indicate the first and the last pages of an article in the journal. The lines that allow me to do that are located in the preamble of the first document. Since the article consists of two parts (the files I want to merge), I need the lastpage label to refer to the last page of the second document. The same holds for the second document: I need the firstpage label to refer to the first page of the first document.

The second issue has the similar nature as the first one and, perhaps, may have the same solution. It goes without saying that the first page of the second document should have the number greater than the last page of the first document by one. So, I suppose the solution here is to put in the argument of the setcounter command in the beginning of the second document some label that refers to the last page of the first document and +1 (i.e. label+1). Sounds like a simple task but I don’t know how to cope with it.

Important note. The page numbers in the headings and footings should be hyperlinks, whereas (obviously) the first page number in the second document shouldn’t.

Edit. This solution doesn't work, because after implementing it I merged pdf's but clicking the last page number in the header or footer only leads to jumping to the first page of the merged document.

Here is the example of the first document (file1.tex) code:

\documentclass[12pt,a4paper]{article}

\usepackage{cmap}
\usepackage{mathtext}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{indentfirst}
\usepackage{letltxmacro}
\usepackage[blocks]{authblk}
\usepackage{xr-hyper}
\externaldocument{file2}
\usepackage{hyperref}
\frenchspacing
\usepackage{lipsum}

\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\pageref{firstpage1} --- \pageref{lastpage2}}
\fancypagestyle{firststyle}
{
\fancyhf{}
\fancyfootoffset[R]{-4cm}
\fancyfoot[L]{\pageref{firstpage1} --- \pageref{lastpage2}}
}

\begin{document}
\setcounter{page}{1}
\author{И.\,О.~Фамилия1}
\title{Название}
\date{}
\maketitle
\thispagestyle{firststyle}
\label{firstpage1}
\lipsum
\label{lastpage1}
\end{document}


And the second one (file2.tex):

\documentclass[12pt,a4paper]{article}

\usepackage{cmap}
\usepackage{mathtext}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{indentfirst}
\usepackage{letltxmacro}
\usepackage[blocks]{authblk}
\usepackage{xr-hyper}
\externaldocument{file1}
\usepackage{hyperref}
\frenchspacing
\usepackage{lipsum}

\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\pageref{firstpage1} --- \pageref{lastpage2}}
\fancypagestyle{firststyle}
{
\fancyhf{}
\fancyfootoffset[R]{-4cm}
\fancyfoot[L]{\pageref{firstpage1} --- \pageref{lastpage2}}
}

\begin{document}
\setcounter{page}{3}
\author{И.\,О.~Фамилия1}
\title{Название}
\date{}
\maketitle
\label{firstpage2}
\lipsum
\label{lastpage2}
\end{document}

• When you merge pdf's with e.g. pdfpages you will loose the links. You can get external references with the xr-package. – Ulrike Fischer Jun 10 '16 at 9:38
• I've tried to implement this solution tex.stackexchange.com/a/41542/101051, but when I click the last page number in the header or footer my pdf reader jumps to the first page of the document/ – David Jun 10 '16 at 10:42
• Well I won't spent my time to guess what you are doing or to build a complete test suite. – Ulrike Fischer Jun 10 '16 at 10:56
• @UlrikeFischer Sorry, I've edited the question – David Jun 10 '16 at 11:17