Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying something ambitious and I hope there is a way to get it to work.

I have a number of pdf documents that I will eventually pull together in one pdf. One of the documents, a cover page of sorts, to include links to other components in the final pdf.

To do this, I set up three dummy documents:

% doc1
\documentclass{article}
\begin{document}
This is the first document.
\end{document}

% doc2
\documentclass{article}
\begin{document}
This is the second document.
\end{document}

% doc3
\documentclass{article}
\begin{document}
This is the third document.
\end{document}

Then, I construct the coverpage document:

% coverpage
\documentclass{article}
\begin{document}
Here is the link to the first document, \ref{doc1.1}.

Here is the link to the second document, \ref{doc2.1}.

Here is the link to the third documents, \ref{doc3.1}.

\end{document}

And finally, I construct the wrapper document, which calls all the component pdfs, and defines the links:

% main document
\documentclass{article}
\usepackage[final]{pdfpages}
\usepackage{url}
\usepackage{hyperref}

\title{Document set}
\begin{document}
\maketitle
%\tableofcontents

\includepdf[link=true]{coverpage.pdf}
\includepdf[link=true, linkname=doc1]{doc1.pdf}
\includepdf[link=true, linkname=doc2]{doc2.pdf}
\includepdf[link=true, linkname=doc3]{doc3.pdf}

\end{document}

I want the references in coverpage to show up when compiled within main.tex. Obviously, the references I want in the coverpage.tex file live in the main.aux file. I am wondering if there is a way to streamline this process so that it works, or indeed if there is an easier way to do this.

I want to write the coverpage separately instead of within the main document, since it uses its own documentclass which is not how I want the main document to be typeset.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

The difficulties are:

  • When PDF pages are included, the annotations (links) are lost.
  • Even, if they would not be lost, the problem remains, how the inter document links should be converted to inner document links.

The following example uses the positioning tracking feature of pdfTeX/LuaTeX or XeTeX. The procedure is as follows.

First the document coverpage does not set any links, but instead of the links it saves the position of the link text in the .aux file.

\documentclass[a4paper]{article}
\usepackage[pass]{geometry}
\usepackage{zref-savepos}

\newcommand*{\sref}[2]{%
  \begingroup
    \sbox0{#2}%
    \raisebox{-\dp0}{\zsavepos{#1-a}}%
    \usebox0%
    \raisebox{\ht0}{\zsavepos{#1-b}}%
  \endgroup
}

\begin{document}
Here is the link to the \sref{doc1}{first document}.

Here is the link to the \sref{doc2}{second document}.

Here is the link to the \sref{doc3}{third document}.

\end{document}

The first argument of \sref is the link handle/label name, the second the link text that appears on the page.

Package zref-savepos is a wrapper for the \pdfsavepos feature. The positions are numbers with implicit unit sp. The origin is the lower left corner. The .aux file contains:

\relax 
\providecommand\zref@newlabel[2]{}
\zref@newlabel{doc1-a}{\posx{15751804}\posy{46450400}}
\zref@newlabel{doc1-b}{\posx{19907887}\posy{46905511}}
\zref@newlabel{doc2-a}{\posx{15751804}\posy{45663968}}
\zref@newlabel{doc2-b}{\posx{20670654}\posy{46119079}}
\zref@newlabel{doc3-a}{\posx{15751804}\posy{44877536}}
\zref@newlabel{doc3-b}{\posx{20195519}\posy{45332647}}

The main document reads the data in the .aux file using package zref-xr. The label names are prefixed with cover- to avoid name clashes:

\usepackage{zref-xr}
\zexternaldocument[cover-]{coverpage}

Then the coverpage is included with options fitpaper and picturecommand. The first ensures that the included page is not scaled with some unknown scaling factor that invalidates the positions. Make sure that coverpage.tex and main.tex are using the same paper size and secondly the output driver needs to be notified, because this is not done by LaTeX. I have used paper size A4 and \usepackage[pass]{geometry} for coverpage.tex. Option pass does not change the current geometry and the package notifies the output driver (pdfTeX) about the paper size.

Option picturecommand=\coverlinks calls macro \coverlinks that sets the links at the right positions using the imported position labels. The link text itself is just an empty box with the right size using invisible rules.

\documentclass[a4paper]{article}
\usepackage[final]{pdfpages}
\usepackage{url}
\usepackage{hyperref}
\usepackage{zref-xr,zref-savepos}
\zexternaldocument[cover-]{coverpage}
\newcommand*{\coverlinks}{%
  \setlength{\unitlength}{1sp}%
  \coverlink{doc1}%
  \coverlink{doc2}%
  \coverlink{doc3}%
}
\newcommand*{\coverlink}[1]{%
  \put(\zposx{cover-#1-a},\zposy{cover-#1-a}){%
    \makebox(0,0)[lb]{%
      \hyperlink{#1.1}{%
        \rule{0pt}{\dimexpr\zposy{cover-#1-b}sp-\zposy{cover-#1-a}sp}%
        \rule{\dimexpr\zposx{cover-#1-b}sp-\zposx{cover-#1-a}sp}{0pt}%
      }%
    }%
  }%
}

\title{Document set}
\begin{document}
\maketitle
%\tableofcontents

\includepdf[fitpaper,link=true,picturecommand=\coverlinks]{coverpage.pdf}
\includepdf[link=true, linkname=doc1]{doc1.pdf}
\includepdf[link=true, linkname=doc2]{doc2.pdf}
\includepdf[link=true, linkname=doc3]{doc3.pdf}

\end{document}

Main document with links on coverpage

share|improve this answer
    
Thank you so much! I will try out your solution, but major props for your effort. –  fg nu Oct 7 '12 at 3:48
    
That was an awesome hack. Thanks again. –  fg nu Oct 15 '12 at 16:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.