What would be the preferred way to export the Table of contents I use in a .tex file to a separate .pdf?

Currently I have a my-report.tex file and I wish to pair the result of pdflatex (my-report.pdf) with my-report-toc.pdf, which contains only the table of contents.

Is there a simple way to achieve this?

My .tex file has this kind of layout (simplified):

...
\begin{titlepage}
\includepdf[pages=-]{abstract.pdf}
\end{titlepage}

% -------------------------------------------------
% This should go to a separate .pdf file on export.
\tableofcontents
\pagebreak
% -------------------------------------------------

\section{Section1}
...
\section{Section2}
...
\section{Section3}
...

-

## 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 myexport.tex:

\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 in myreport-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 of myreport. The label toc writes the page number for the start page of the table of contents. By using module zref-thepage also the visual appearance of all page numbers in the document are remembered. The .aux file of myreport is then read by zref-xr in myreport-toc.tex.

File myreport.tex:

\documentclass{book}
\usepackage{zref-abspage,zref-thepage}
\makeatletter
\newcommand*{\pagevaluelabel}[1]{%
\zref@labelbyprops{toc}{abspage}%
}
\makeatother
\AtBeginDocument{%
}
\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
\csname l@#1\endcsname{{#2}}{%
}%
\else
\csname l@#1\endcsname{%
}{#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.

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.

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:

-

You can use pdfseparate to extract a page, or range of pages, from a pdf file.

Assuming the table of contents is only one page long, try pdfseparate -f 2 -l 2 my-report.pdf my-report-toc.pdf

-
There are several ways to extract pages from a pdf. See the answer to tex.stackexchange.com/questions/54157/… – Ethan Bolker Aug 28 '12 at 0:54

Quick and very dirty hack (far from robust) for the lazy ones:

You have to compile the original document twice, which gives my-report.tex. Now modify the document a bit:

modified document:

\begin{document}
...
\begin{titlepage}
\includepdf[pages=-]{abstract.pdf}
\end{titlepage}

% -------------------------------------------------
% This should go to a separate .pdf file on export.
\tableofcontents
\pagebreak
% -------------------------------------------------
\end{document} %THIS LINE IS IMPORTANT!
\section{Section1}
...
\section{Section2}
...
\section{Section3}
...
\end{document}


And compile it once. The table of contents is created, nothing else. This worked for me using pdflatex in the commandline or texmaker.

Edit: Of course you have to do this again, if you changed your document, therefore if you want to generate tables of content frequently this solution is probably the wrong one.

-
This is just deleting the content of your document which is not a robust solution. It's dirty but not quick as you have to do this every time you change the document. – jak123 Nov 17 '15 at 7:46
You are completely right with your remark, but for a quick and simple output of your table of contents (e.g. giving someone the outline of your document for review) its sufficient. – BR123 Nov 17 '15 at 9:30