Search code examples
python-sphinxsidebarrestructuredtextread-the-docstoctree

How to make toctree link refer to the separate file like it refers to the subsections


Structure

The following structure of the project:

  • index.rst

    MyProject
    =========
    
    Contents:
    
    .. toctree::
    
       group1
    
  • group1.rst

    Group1
    ------
    
    Subgroup1
    =========
    
    Subgroup1 contents
    
    Subgroup2
    =========
    
    Subgroup2 contents
    

Rendered to (after clicking on Group1 -> Subgroup2):

clicked-on-subsection

As you can see it opens the Group1 page and linked to Subgroup2 section.

Want

I want to have the same on the left side (Group1 openned and Subgroup2 choosed) but on the right side I want to see only Subgroup2 page (page without Subgroup1 content).

I.e. have file group1/subgroup1.rst:

Subgroup2
=========

Subgroup2 contents

Rendered to:

want

How it can be achieved? This is a simple example with the depth 2, what about depth 3-4?


Solution

  • You need to have a file per page of content. Sphinx doesn't break files into multiple pages.

    What works for me is creating toctree directives that reference files containing sub-toctree directives. I like to create the sub-groups in directories, but you could do this within one directory.

    index.rst:

    MyProject
    =========
    
    Contents:
    
    .. toctree::
    
       group1/index
    

    group1/index.rst:

    Group1
    ======
    
    .. toctree::
    
       subgroup1
       subgroup2
    

    group1/subgroup1.rst:

    Subgroup1
    =========
    
    Subgroup1 contents
    

    group1/subgroup2.rst:

    Subgroup2
    =========
    
    Subgroup2 contents