# Automatically produce outline?

I have a .tex document which is divided into \section, \subsection, \subsubsection, etc. I would like to produce a separate second document which is an outline of this first document. What's the best way to do this?

Of course, it would be fairly trivial to write a script to extract the section headings--presumably this exists already?

Or I could just put in a temporary \tableofcontents and then extract this from the resulting PDF.

(I'm using LyX as a TeX front-end, so if this exists in LyX, that would be helpful too. I know there's an outline viewer in LyX, but I don't see a feature to export this outline.)

LaTeX generates a file with the extension .toc which contains the required TOC commands. You could simply include that file into an outline document.

So, let's say, your main document is called main.tex. The file containing the table of contents is then main.toc. Create a new document outline.tex, within that use \input{main.toc}. Use makeatletter and \makeatother for the case that the .toc file might contain internal macros, such as \select@language {english} of babel. Group those actions to keep settings local. For the outline, you might wish to use the same settings respectively preamble like your main document does.

So, this could be such an outline document:

\documentclass{article}
% your preamble settings you wish to keep: fonts etc.
\begin{document}
\begingroup
\section*{\contentsname}
\makeatletter
\input{main.toc}
\makeatother
\endgroup
\end{document}


Looking at two places might further be useful in this matter:

• the definition of \tableofcontents in your document class file
• the definition of \@starttoc within latex.ltx or in a customized class/package.

Of course this works also for lists of tables and figures.

• @Tobin: If it helped, it would be great if you would mark the answer as "accepted" by clicking the tick (check mark) at the left. Mar 15, 2011 at 17:18

Simply using grep gets the job done pretty well:

grep "\section" document.tex

• Escaping is not needed within double quotes. Dec 9, 2010 at 17:58
• I would go for "section{", to also get subsections (yes, I'm too lazy to figure out the right syntax for \\(sub)*section.) Mar 13, 2011 at 17:35

I came here looking for a very simple version of this, one that would just write the outline to stdout. I saw one back around 1986, but I can't find the equivalent now, so I just wrote myself a small shell script. It could be improved, e.g. to do something smart if it gets the wrong number of args, or to deal with \part and \chapter in books (and I guess \subparagraph{}s, if you believe in those), but for today this is good enough.

#!/bin/sh
#Extract an outline from a LaTeX file
grep -e "section{" \
-e "paragraph{" \
$1 \ | sed -e "s/label sxn.*$//" \
-e "s/\\\subsubsection/      /" \
-e "s/\\\subsection/    /"  \
-e"s/\\\section/  /" \
-e "s/\\\paragraph/        /" \
-e"s/\\\label.*\$//" \
| tr -d "{}"