[Tex/LaTex] Lyx Table of Contents not being generated

lyxtable of contents

Here is a minimal working example as exported from Lyx

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{titlesec}
\newcommand{\sectionbreak}{\clearpage}

\makeatother

\usepackage{babel}
\begin{document}

\title{Test}

\author{Me}

\maketitle
\tableofcontents{}

\part*{Part 1}
\section*{Sec 1}
Test
\section*{Sec 2}
Test
\end{document}

I have used a TOC before when using TexWorks etc and know that the compiler has to run several times. How do I get Lyx to generate the TOC?
Thanks

Best Answer

Unnumbered headings (part, section, etc.) are not placed in the table of contents, so you need to use the numbered equivalents instead. In the LaTeX code, the * after \part, \section etc. indicates that it is an unnumbered heading, and the same * is used in LyX.

On the other hand. If you don't want any of the headings to be numbered, but still show up in the ToC, go to Document --> Settings --> Numbering & TOC, and drag the Numbering slider all the way to the left. This will hide the number from the various headings.

enter image description here

In LaTeX terms, this adds \setcounter{secnumdepth}{-2} to the preamble, which turns off numbering for all sectioning levels. (Actually, -1 would have been sufficient I think, don't know why LyX makes it -2.) The different levels of headings are numbered from -1 (part) to 5 (subparagraph), and the secnumdepth counter determines which levels will have numbers displayed. Similarly there is a tocdepth counter which determines which levels are placed in the ToC. In LyX, the value of this counter is also adjusted in Numbering & TOC in the document settings.

A small LaTeX code example:

\documentclass{article}
\begin{document}
\tableofcontents

\section*{A}
Unnumbered, not in ToC.

\section{B}
Numbered, in ToC.

\setcounter{secnumdepth}{0}
\section{C}

Numbered, but number hidden, in ToC.
\end{document}

enter image description here