Export only table of contents
PDF extraction
External tools can be used to extract pages from the PDF file, e.g. pdftk
.
If the table of contents is on pages 5 and 6:
pdftk myreport.pdf cat 5-6 output myreport-toc.pdf
\include
If the main document is separated in several \include
files, then the
\tableofcontents
goes into a separate file myreport-tableofcontents.tex
:
\tableofcontents
\newpage
and in myreport.tex
to include its toc:
\include{myreport-tableofcontents}
Then myreport-toc.tex
only includes the table of contents:
\includeonly{myreport-tableofcontents}
\input{myreport}
With hyperref
and links
If myreport.tex
loads package hyperref
, then the table of contents file myreport.toc
also contains anchor names. That can be used to make inter document links. For example,
the overview file for my bundle on CTAN, oberdiek.pdf includes the table of contents of all the packages in the bundle. The source file oberdiek.tex uses
this technique, see the definition of \tocinclude
in the example below. Of course both myreport.tex
and myreport-toc.tex
need hyperref
.
Also needed:
Any macros used in
myreport.toc
should be provided inmyreport-toc.tex
.The same class, fonts, page layout, … should be used.
In case of the page number, the example uses
zref
to put the data for the page numbers in the.aux
file ofmyreport
. The labeltoc
writes the page number for the start page of the table of contents. By using modulezref-thepage
also the visual appearance of all page numbers in the document are remembered. The.aux
file ofmyreport
is then read byzref-xr
inmyreport-toc.tex
.
File myreport.tex
:
\documentclass{book}
\usepackage{zref-abspage,zref-thepage}
\makeatletter
\newcommand*{\pagevaluelabel}[1]{%
\zref@labelbyprops{toc}{abspage}%
}
\makeatother
\AtBeginDocument{%
\addtocontents{toc}{\string\providecommand\string\pagevaluelabel[1]{}}%
\addtocontents{toc}{\string\pagevaluelabel{toc}}%
}
\usepackage{hyperref}
\setcounter{secnumdepth}{4}
\begin{document}
\frontmatter
Title page
\newpage
\chapter*{Foreword}
\tableofcontents
\mainmatter
\chapter{Chapter}
\section{Section}
\subsection{Subsection}
\subsubsection{Subsubsection}
\end{document}
File myreport-toc.tex
:
\documentclass{book}
\usepackage{hyperref}
\usepackage{zref-xr}
\zexternaldocument[main:]{test}\relax
\makeatletter
\newcommand*{\tocinclude}[1]{%
\chapter*{\contentsname
\@mkboth{\MakeUppercase\contentsname}%
{\MakeUppercase\contentsname}%
}%
\begingroup
\makeatletter
\def\@prj{#1}%
\let\contentsline\foreign@contentsline
\input{\@prj.toc}%
\endgroup
}
\def\foreign@contentsline#1#2#3#4{%
\ifx\\#4\\%
\csname l@#1\endcsname{#2}{#3}%
\else
\ifHy@linktocpage
\csname l@#1\endcsname{{#2}}{%
\hyper@linkfile{#3}{\@prj.pdf}{#4}%
}%
\else
\csname l@#1\endcsname{%
\hyper@linkfile{#2}{\@prj.pdf}{#4}%
}{#3}%
\fi
\fi
}%
\begin{document}
\makeatletter
\zref@refused{main:toc}
\setcounter{page}{%
\numexpr\zref@extractdefault{main:toc}{abspage}{1}\relax
}
\renewcommand*{\thepage}{%
\zref@extractdefault{main:thepage\number\value{page}}%
{page}{\arabic{page}}%
}
\tocinclude{myreport}
\end{document}
First method (with shorttoc
)
The easiest method is to use the command \anothertableofcontents
from the shorttoc
package, which loads and prints the ToC from an arbitrary .toc
file.
If your main document is
my-report.tex
\documentclass{article}
\usepackage{blindtext}
\begin{document}
\tableofcontents
\blinddocument
\end{document}
create a my-report-toc.tex
with the following contents
my-report-toc.tex
\documentclass{article}
\usepackage{shorttoc}
\usepackage{xspace} % this shouldn't be needed in your document
\begin{document}
\anothertableofcontents{my-report}{Contents}{3}
\end{document}
Compiling the latter, the resulting my-report-toc.pdf
will contain the ToC of my-report.pdf
:
Second method (with pdfpages
)
Another method is to use the pdfpages
package to load only the pages of the ToC in a different file. In this case, the pages containing the ToC must not contain anything else.
If your main document is
my-report.tex
\documentclass{article}
\usepackage{blindtext}
\begin{document}
\tableofcontents
\clearpage
\blinddocument
\end{document}
create a my-report-toc.tex
with the following contents (replace pages=1
with the real range of pages of your ToC)
my-report-toc.tex
\documentclass{article}
\usepackage{pdfpages}
\begin{document}
\includepdf[pages=1]{my-report.pdf}
\end{document}
Compiling the latter, the resulting my-report-toc.pdf
will contain the ToC of my-report.pdf
: