2

I can create table of contents in two ways:

.. contents::
   :local:
   depth: 1

or as

.. toctree::
    :maxdepth: 1

    index

What is difference? Where I should use the toctree and where the contents?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
matousc
  • 3,698
  • 10
  • 40
  • 65

1 Answers1

11

.. contents is a doctutils directive (the underlying library which defines ReST and associated utilities) and automatically generates a table of contents from headlines within the current topic.

.. toctree is a Sphinx-defined directive in which you explicitly list documents whose TOCs will be listed out.

You'd use .. contents for example within a document to generate an overview of contents within the page, e.g.:

===================
Curing World Hunger
===================

.. contents::
   :depth: 1

Abstract
========

…

Problem description
===================

…

You'd use .. toctree within an index document which contains basically nothing else:

=================
Scientific papers
=================

Below is a list of papers published here:

.. toctree::
   :maxdepth: 2

   curing_hunger
   …

.. toctree takes a list of documents to process, .. contents does not.

deceze
  • 510,633
  • 85
  • 743
  • 889