It's my first question here. Sorry if it doesn't follow the rules.

I have:

I'm writing a manual in the book class with pdflatex (TeX Live under Linux). Each chapter is stored in a separate tex-file (chapter1.tex, chapter2.tex).

I need:

After compiling the whole document I am using \includeonly for obtaining separate pdf-files (thanks to post). Using hyperref and zref-xr packages I get correct hyperlinks between different pdf's corresponding to different chapters, but links in the main table of contents and in the table of contents for each part are broken.

Minimal working example:

main.tex:

\documentclass[12pt,oneside]{book}
\usepackage{minitoc}
\usepackage{hyperref}

\ifdefined\tosplit
\usepackage{zref-xr}
\zxrsetup{toltxlabel}
\fi

\begin{document}
\include{maintoc}
\include{parttoc}
\include{chapter1}
\include{chapter2}
\end{document}


maintoc.tex:

\doparttoc
\tableofcontents


parttoc.tex:

\part{The first and last part}
\parttoc


chapter1.tex:

\ifdefined\tosplit
\zexternaldocument*{chapter2}
\fi

\chapter{The first chapter}
\label{chap:first}
See chapter~\ref{chap:second}.


chapter2.tex:

\ifdefined\tosplit
\zexternaldocument*{chapter1}
\fi

\chapter{The second chapter}
\label{chap:second}
See chapter~\ref{chap:first}.


The sequence of commands I use:

1. pdflatex -jobname=tmp main && mv tmp.pdf main.pdf (x2)
2. pdflatex -jobname=tmp "\def\tosplit{1}\includeonly{maintoc}\input{main}" && mv tmp.pdf maintoc.pdf
3. pdflatex -jobname=tmp "\def\tosplit{1}\includeonly{parttoc}\input{main}" && mv tmp.pdf parttoc.pdf
4. pdflatex -jobname=tmp "\def\tosplit{1}\includeonly{chapter1}\input{main}" && mv tmp.pdf chapter1.pdf
5. pdflatex -jobname=tmp "\def\tosplit{1}\includeonly{chapter2}\input{main}" && mv tmp.pdf chapter2.pdf

I tried advice from the author Heiko Oberdiek of hyperref and zref packages. Honestly, it is not clear for me, but it works unless you have just two pdf's (TOC and the rest), otherwise it doesn't. It always tries to open just one pdf, but I have many of them.

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Looks like a good first question! Let's hope it gets some good answers ;). – cfr Dec 16 '14 at 0:49
• I take it this is intended for a web site. The easiest way would be to write the table of contents manually after the rest of the book, using \href for cross-file links. You can link to previously named targets in the files if you know their names. Look at the toc file for the last name in \contentsline. – John Kormylo Dec 16 '14 at 2:53
• Hmmm, you could do it automatically by creating you own \contentsline macro and \input each of the toc files one at a time. – John Kormylo Dec 16 '14 at 2:57