1

I would like to have a table of contents displaying the sections from my document (article class) and some fake chapters (which I don't want to appear in the main document). I thought I would be able to say "I want only chapters and sections in ToC", and only have sections in the document, and add some fake chapters with addcontentsline. I have tried this, but it fails (error message below mwe):

\documentclass{article}
\begin{document}

\setcounter{tocdepth}{1}
\setcounter{secnumdepth}{-2}
\tableofcontents

\addcontentsline{toc}{chapter}{chapter name}
\section{one section}
foo
\section{another section}
bar

\end{document}

Error:

! LaTeX Error: Something's wrong--perhaps a missing \item.
...
l.2 \contentsline {section}{one section}{1}{}

...

The error looks like there's a numbering issue.

I may be confused by the tocdepth or secnumdepth, or maybe there is another way to achieve what I'd like.

1
  • 2
    chapters are not set up, you probably want \part Commented Mar 22 at 16:49

1 Answer 1

1

article does not provide \chapter and therefore also not chapter entries to the TOC. If you don't want to use part instead of chapter as second argument of \addcontentsline you could use, e.g., package tocbasic to define chapter entries:

\documentclass{article}
\usepackage{tocbasic}
\DeclareTOCStyleEntry{tocline}{chapter}
\begin{document}

\setcounter{tocdepth}{1}
\setcounter{secnumdepth}{-2}
\tableofcontents

\addcontentsline{toc}{chapter}{chapter name}
\section{one section}
foo
\section{another section}
bar

\end{document}

ToC with fake chapters

Note: You can use the package also to reconfigure the section entries, e.g., to get a more hierarchical ToC:

\documentclass{article}
\usepackage{tocbasic}
\DeclareTOCStyleEntry{tocline}{chapter}
\DeclareTOCStyleEntry[indent=1em,beforeskip=0pt]{tocline}{section}
\begin{document}

\setcounter{tocdepth}{1}
\setcounter{secnumdepth}{-2}
\tableofcontents

\addcontentsline{toc}{chapter}{chapter name}
\section{one section}
foo
\section{another section}
bar

\end{document}

hierarchical ToC

1
  • Thank you very much! I should have paid more attention to the fact that there are no chapters with article class. I use part, and it's now OK!
    – rfs
    Commented Mar 22 at 17:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .