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.

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}
...
share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

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{%
  \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}
share|improve this answer
add comment

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

Of course, this requires that you know how many pages the table of contents takes up.

share|improve this answer
    
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
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.