I have a Tex file which \input's many different Tex files. Each one of these files have different Chapter and Section declarations and is currently generating a nice Table of Contents.

What I'd like to do is somehow generate a text file with the Chapter and Section's, perhaps something like this:

Chapter 1 Name
  Section 1.1 Name
  Section 1.2 Name
Chapter 2 Name
  Section 2.1 Name
  Section 2.2 Name

The goal of this is to conveniently send people my ToC without having to copy and paste and reformat.

  • Why don't you just send them the PDF table of contents? Otherwise, maybe sed/gawk on the .toc. But I don't see the point. – cfr Jan 2 '17 at 4:11

Here is a LaTeX-based solution, worked out for chapter and section entries in the table of contents.

First, generate the toc file, say myfile.toc. E.g., if myfile.tex contains

\section{First first}
\section{First second}
\section{Second first}
\section{Second second}

then latex myfile (or any other LaTeX incarnation) will generate the file myfile.toc containing

\contentsline {chapter}{\numberline {1}First}{3}
\contentsline {section}{\numberline {1.1}First first}{3}
\contentsline {section}{\numberline {1.2}First second}{3}
\contentsline {chapter}{\numberline {2}Second}{5}
\contentsline {section}{\numberline {2.1}Second first}{5}
\contentsline {section}{\numberline {2.2}Second second}{5}

Now run LaTeX on the following document, call it e.g. toc2txt.tex. Before doing so, modify the first line such that it contains the name of your file.

\newcommand\toc{myfile} % <<< Replace myfile with the name of your tex/toc file (without extension)
\renewcommand\numberline[1]{#1 }
     \immediate\write\txtfile{Chapter #2}%
       \immediate\write\txtfile{\space\space Section #2}%
       %%% Code for further toc levels

It will read myfile.toc and will produce a file myfile.txt with the following contents:

Chapter 1 First
  Section 1.1 First first
  Section 1.2 First second
Chapter 2 Second
  Section 2.1 Second first
  Section 2.2 Second second
  • If hyperref produced the toc, there's a fourth argument to \contentsline that should be ignored. I don't know if there's a convenient way to test that, or if the answer should just be "if you used hyperref, then \contentsline[4], else \contentsline[3]". – Teepeemm Jan 2 '17 at 20:25

Many pdf viewers are able to extract particular pages, so one possibility would be to just extract the pages with your table of contents from the final output pdf. But if you want a more TeXish approach, you can just create a new toc.tex and input the TOC file:

\documentclass{book} % match your original documentclass

hyperref changes how the contents are produced. If your original file uses the hyperref package, then this should as well. If not, then not. The draft option prevents hyperref from trying to link to pages that don't exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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