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

\documentclass{book}
\begin{document}
\tableofcontents
\chapter{First}
\section{First first}
\section{First second}
\chapter{Second}
\section{Second first}
\section{Second second}
\end{document}


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)
\documentclass{article}
\newwrite\txtfile
\immediate\openout\txtfile=\toc.txt
\renewcommand\numberline[1]{#1 }
\def\levelch{chapter}
\def\levelsec{section}
\renewcommand\contentsline[3]%
{\def\tmp{#1}%
\ifx\tmp\levelch
\immediate\write\txtfile{Chapter #2}%
\else
\ifx\tmp\levelsec
\immediate\write\txtfile{\space\space Section #2}%
\else
%%% Code for further toc levels
\fi
\fi
}
\begin{document}
\input{\toc.toc}
\end{document}


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
\usepackage[draft]{hyperref}
\begin{document}
\input{myfile.toc}
\end{document}


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.